diff --git a/DOC.md b/DOC.md index 0534e95f..b71f5a1e 100644 --- a/DOC.md +++ b/DOC.md @@ -8971,6 +8971,28 @@ isSet(new Set()); // -> true isSet(new WeakSet()); // -> false ``` +## isShadowRoot + +Check if value is a ShadowRoot object. + +
+Type Definition + +```typescript +function isShadowRoot(val: any): val is ShadowRoot; +``` + +
+ +|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. diff --git a/DOC_CN.md b/DOC_CN.md index fd3361db..0acf0f1d 100644 --- a/DOC_CN.md +++ b/DOC_CN.md @@ -8964,6 +8964,28 @@ isSet(new Set()); // -> true isSet(new WeakSet()); // -> false ``` +## isShadowRoot + +检查值是否是 ShadowRoot 对象。 + +
+类型定义 + +```typescript +function isShadowRoot(val: any): val is ShadowRoot; +``` + +
+ +|参数名|说明| +|-----|---| +|val|要检查的值| +|返回值|如果是 ShadowRoot,返回真| + +```javascript +isShadowRoot(document.body); // -> false +``` + ## isSorted 检查数组是否有序。 diff --git a/cspell.json b/cspell.json index c81bd7b8..3f272ac9 100644 --- a/cspell.json +++ b/cspell.json @@ -239,6 +239,7 @@ "isRetina", "isRunning", "isSet", + "isShadowRoot", "isSorted", "isStr", "isStrBlank", diff --git a/i18n/isShadowRoot.md b/i18n/isShadowRoot.md new file mode 100644 index 00000000..743c1d72 --- /dev/null +++ b/i18n/isShadowRoot.md @@ -0,0 +1,8 @@ +## CN + +检查值是否是 ShadowRoot 对象。 + +|参数名|说明| +|-----|---| +|val|要检查的值| +|返回值|如果是 ShadowRoot,返回真| diff --git a/index.json b/index.json index 01d70e7c..4214a6bd 100644 --- a/index.json +++ b/index.json @@ -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.", diff --git a/src/isShadowRoot.js b/src/isShadowRoot.js new file mode 100644 index 00000000..dc6641ec --- /dev/null +++ b/src/isShadowRoot.js @@ -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; +}; diff --git a/test/isShadowRoot.js b/test/isShadowRoot.js new file mode 100644 index 00000000..01e19d3f --- /dev/null +++ b/test/isShadowRoot.js @@ -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; +});