diff --git a/DOC.md b/DOC.md index d7a4c013..528d0f2f 100644 --- a/DOC.md +++ b/DOC.md @@ -11931,6 +11931,26 @@ Strip ansi codes from a string. stripAnsi('\u001b[4mcake\u001b[0m'); // -> 'cake' ``` +## stripBom + +Strip UTF-8 byte order mark. + +
+Type Definition +
+function stripBom(str: string): string;
+
+
+ +|Name |Desc | +|------|---------------| +|str |String to strip| +|return|Result string | + +```javascript +stripBom('\uFEFFlicia'); // -> 'licia' +``` + ## stripCmt Strip comments from source code. diff --git a/DOC_CN.md b/DOC_CN.md index 59b0a711..d62c89c7 100644 --- a/DOC_CN.md +++ b/DOC_CN.md @@ -11922,6 +11922,26 @@ stringifyAll(function test() {}); // -> '{"value":"function test() {}","type":"F stripAnsi('\u001b[4mcake\u001b[0m'); // -> 'cake' ``` +## stripBom + +清除 UTF-8 BOM。 + +
+类型定义 +
+function stripBom(str: string): string;
+
+
+ +|参数名|说明| +|-----|---| +|str|源字符串| +|返回值|目标字符串| + +```javascript +stripBom('\uFEFFlicia'); // -> 'licia' +``` + ## stripCmt 清除源码中的注释。 diff --git a/cspell.json b/cspell.json index f398da75..d3634c17 100644 --- a/cspell.json +++ b/cspell.json @@ -396,6 +396,7 @@ "stringify", "stringifyAll", "stripAnsi", + "stripBom", "stripCmt", "stripColor", "stripHtmlTag", diff --git a/i18n/stripBom.md b/i18n/stripBom.md new file mode 100644 index 00000000..c3e39e3c --- /dev/null +++ b/i18n/stripBom.md @@ -0,0 +1,8 @@ +## CN + +清除 UTF-8 BOM。 + +|参数名|说明| +|-----|---| +|str|源字符串| +|返回值|目标字符串| diff --git a/index.json b/index.json index d20a7cda..9a415aa5 100644 --- a/index.json +++ b/index.json @@ -6171,6 +6171,19 @@ "browser" ] }, + "stripBom": { + "dependencies": [], + "description": "Strip UTF-8 byte order mark.", + "env": [ + "node", + "browser", + "miniprogram" + ], + "test": [ + "node", + "browser" + ] + }, "stripCmt": { "dependencies": [], "description": "Strip comments from source code.", diff --git a/src/stripBom.js b/src/stripBom.js new file mode 100644 index 00000000..069f3559 --- /dev/null +++ b/src/stripBom.js @@ -0,0 +1,26 @@ +/* Strip UTF-8 byte order mark. + * + * |Name |Desc | + * |------|---------------| + * |str |String to strip| + * |return|Result string | + */ + +/* example + * stripBom('\uFEFFlicia'); // -> 'licia' + */ + +/* module + * env: all + */ + +/* typescript + * export declare function stripBom(str: string): string; + */ + +exports = function(str) { + if (str.charCodeAt(0) === 0xfeff) { + return str.slice(1); + } + return str; +}; diff --git a/test/stripBom.js b/test/stripBom.js new file mode 100644 index 00000000..52e98080 --- /dev/null +++ b/test/stripBom.js @@ -0,0 +1 @@ +test(['\uFEFFlicia', 'licia']);