Skip to content

Commit

Permalink
feat: add isShadowRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Oct 9, 2024
1 parent d42d3e8 commit 94d216e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
22 changes: 22 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -8971,6 +8971,28 @@ isSet(new Set()); // -> true
isSet(new WeakSet()); // -> false
```

## isShadowRoot

Check if value is a ShadowRoot object.

<details>
<summary>Type Definition</summary>

```typescript
function isShadowRoot(val: any): val is ShadowRoot;
```

</details>

|Name |Desc |
|------|-----------------------------|
|val |Value to check |
|return|True if value is a ShadowRoot|

```javascript
isShadowRoot(document.body); // -> false
```

## isSorted

Check if an array is sorted.
Expand Down
22 changes: 22 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8964,6 +8964,28 @@ isSet(new Set()); // -> true
isSet(new WeakSet()); // -> false
```

## isShadowRoot

检查值是否是 ShadowRoot 对象。

<details>
<summary>类型定义</summary>

```typescript
function isShadowRoot(val: any): val is ShadowRoot;
```

</details>

|参数名|说明|
|-----|---|
|val|要检查的值|
|返回值|如果是 ShadowRoot,返回真|

```javascript
isShadowRoot(document.body); // -> false
```

## isSorted

检查数组是否有序。
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
"isRetina",
"isRunning",
"isSet",
"isShadowRoot",
"isSorted",
"isStr",
"isStrBlank",
Expand Down
8 changes: 8 additions & 0 deletions i18n/isShadowRoot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## CN

检查值是否是 ShadowRoot 对象。

|参数名|说明|
|-----|---|
|val|要检查的值|
|返回值|如果是 ShadowRoot,返回真|
10 changes: 10 additions & 0 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4067,6 +4067,16 @@
"browser"
]
},
"isShadowRoot": {
"dependencies": [],
"description": "Check if value is a ShadowRoot object.",
"env": [
"browser"
],
"test": [
"browser"
]
},
"isSorted": {
"dependencies": [],
"description": "Check if an array is sorted.",
Expand Down
27 changes: 27 additions & 0 deletions src/isShadowRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Check if value is a ShadowRoot object.
*
* |Name |Desc |
* |------|-----------------------------|
* |val |Value to check |
* |return|True if value is a ShadowRoot|
*/

/* example
* isShadowRoot(document.body); // -> false
*/

/* module
* env: browser
*/

/* typescript
* export declare function isShadowRoot(val: any): val is ShadowRoot;
*/

exports = function(val) {
if (window.ShadowRoot) {
return val instanceof ShadowRoot;
}

return false;
};
12 changes: 12 additions & 0 deletions test/isShadowRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
it('true', function() {
expect(
isShadowRoot(
document.createElement('div').attachShadow({ mode: 'open' })
)
).to.be.true;
});

it('false', function() {
expect(isShadowRoot(document.body)).to.be.false;
expect(isShadowRoot(document.createElement('div'))).to.be.false;
});

0 comments on commit 94d216e

Please sign in to comment.