From 4d6294a92e713d096812d1f7da9bc616376e7356 Mon Sep 17 00:00:00 2001 From: redhoodsu Date: Fri, 14 Jul 2023 19:50:49 +0800 Subject: [PATCH] feat: add isStrBlank --- DOC.md | 23 +++++++++++++++++++++++ DOC_CN.md | 23 +++++++++++++++++++++++ benchmark/isStrBlank.js | 19 +++++++++++++++++++ cspell.json | 1 + i18n/isStrBlank.md | 8 ++++++++ index.json | 14 ++++++++++++++ src/isStrBlank.js | 40 ++++++++++++++++++++++++++++++++++++++++ test/isStrBlank.js | 6 ++++++ 8 files changed, 134 insertions(+) create mode 100644 benchmark/isStrBlank.js create mode 100644 i18n/isStrBlank.md create mode 100644 src/isStrBlank.js create mode 100644 test/isStrBlank.js diff --git a/DOC.md b/DOC.md index 528d0f2f..460af3ba 100644 --- a/DOC.md +++ b/DOC.md @@ -8359,6 +8359,29 @@ Check if value is a string primitive. isStr('licia'); // -> true ``` +## isStrBlank + +Check if string is blank. + +
+Type Definition +
+function isStrBlank(str: string): boolean;
+
+
+ +|Name |Desc | +|------|-----------------------| +|str |String to check | +|return|True if string is blank| + +```javascript +isStrBlank('licia'); // -> false +isStrBlank(''); // -> true +isStrBlank(' '); // -> true +isStrBlank('\r\n '); // -> true +``` + ## isStream Check if value is a Node.js stream. diff --git a/DOC_CN.md b/DOC_CN.md index 6edd9554..21384273 100644 --- a/DOC_CN.md +++ b/DOC_CN.md @@ -8352,6 +8352,29 @@ isSorted([3, 2, 1]); // -> false isStr('licia'); // -> true ``` +## isStrBlank + +检查字符串是否为空。 + +
+类型定义 +
+function isStrBlank(str: string): boolean;
+
+
+ +|参数名|说明| +|-----|---| +|str|要检查的字符串| +|返回值|如果字符串为空,返回真| + +```javascript +isStrBlank('licia'); // -> false +isStrBlank(''); // -> true +isStrBlank(' '); // -> true +isStrBlank('\r\n '); // -> true +``` + ## isStream 检查值是否是 Node.js Stream 类型。 diff --git a/benchmark/isStrBlank.js b/benchmark/isStrBlank.js new file mode 100644 index 00000000..4e698717 --- /dev/null +++ b/benchmark/isStrBlank.js @@ -0,0 +1,19 @@ +const trim = util.trim; +const randomId = util.randomId; +const each = util.each; + +const tests = [ + ' ', + randomId(1000), + ` ${randomId(1000)} `, + ` ${randomId(1000, ' \n\r\t\f\v')}` +]; + +benchmark({ + trim() { + each(tests, test => trim(test) === ''); + }, + licia() { + each(tests, test => isStrBlank(test)); + } +}); diff --git a/cspell.json b/cspell.json index d3634c17..4ebb9b3e 100644 --- a/cspell.json +++ b/cspell.json @@ -238,6 +238,7 @@ "isSet", "isSorted", "isStr", + "isStrBlank", "isStream", "isSymbol", "isTypedArr", diff --git a/i18n/isStrBlank.md b/i18n/isStrBlank.md new file mode 100644 index 00000000..1ede2b96 --- /dev/null +++ b/i18n/isStrBlank.md @@ -0,0 +1,8 @@ +## CN + +检查字符串是否为空。 + +|参数名|说明| +|-----|---| +|str|要检查的字符串| +|返回值|如果字符串为空,返回真| diff --git a/index.json b/index.json index 90a16ac9..40746303 100644 --- a/index.json +++ b/index.json @@ -4034,6 +4034,20 @@ "browser" ] }, + "isStrBlank": { + "benchmark": true, + "dependencies": [], + "description": "Check if string is blank.", + "env": [ + "node", + "browser", + "miniprogram" + ], + "test": [ + "node", + "browser" + ] + }, "isStream": { "dependencies": [ "isObj", diff --git a/src/isStrBlank.js b/src/isStrBlank.js new file mode 100644 index 00000000..77447ebf --- /dev/null +++ b/src/isStrBlank.js @@ -0,0 +1,40 @@ +/* Check if string is blank. + * + * |Name |Desc | + * |------|-----------------------| + * |str |String to check | + * |return|True if string is blank| + */ + +/* example + * isStrBlank('licia'); // -> false + * isStrBlank(''); // -> true + * isStrBlank(' '); // -> true + * isStrBlank('\r\n '); // -> true + */ + +/* module + * env: all + */ + +/* typescript + * export declare function isStrBlank(str: string): boolean; + */ + +exports = function(str) { + for (let i = 0, len = str.length; i < len; i++) { + const c = str[i]; + if ( + c !== ' ' && + c !== '\n' && + c !== '\r' && + c !== '\t' && + c !== '\f' && + c !== '\v' + ) { + return false; + } + } + + return true; +}; diff --git a/test/isStrBlank.js b/test/isStrBlank.js new file mode 100644 index 00000000..d51a764d --- /dev/null +++ b/test/isStrBlank.js @@ -0,0 +1,6 @@ +tests([ + ['licia', false], + ['', true], + [' ', true], + ['\r\n ', true] +]);