Skip to content

Commit

Permalink
feat: add isStrBlank
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Jul 14, 2023
1 parent f811223 commit 4d6294a
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 0 deletions.
23 changes: 23 additions & 0 deletions DOC.md
Expand Up @@ -8359,6 +8359,29 @@ Check if value is a string primitive.
isStr('licia'); // -> true
```

## isStrBlank

Check if string is blank.

<details>
<summary>Type Definition</summary>
<pre>
<code class="language-typescript">function isStrBlank(str: string): boolean;</code>
</pre>
</details>

|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.
Expand Down
23 changes: 23 additions & 0 deletions DOC_CN.md
Expand Up @@ -8352,6 +8352,29 @@ isSorted([3, 2, 1]); // -> false
isStr('licia'); // -> true
```

## isStrBlank

检查字符串是否为空。

<details>
<summary>类型定义</summary>
<pre>
<code class="language-typescript">function isStrBlank(str: string): boolean;</code>
</pre>
</details>

|参数名|说明|
|-----|---|
|str|要检查的字符串|
|返回值|如果字符串为空,返回真|

```javascript
isStrBlank('licia'); // -> false
isStrBlank(''); // -> true
isStrBlank(' '); // -> true
isStrBlank('\r\n '); // -> true
```

## isStream

检查值是否是 Node.js Stream 类型。
Expand Down
19 changes: 19 additions & 0 deletions 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));
}
});
1 change: 1 addition & 0 deletions cspell.json
Expand Up @@ -238,6 +238,7 @@
"isSet",
"isSorted",
"isStr",
"isStrBlank",
"isStream",
"isSymbol",
"isTypedArr",
Expand Down
8 changes: 8 additions & 0 deletions i18n/isStrBlank.md
@@ -0,0 +1,8 @@
## CN

检查字符串是否为空。

|参数名|说明|
|-----|---|
|str|要检查的字符串|
|返回值|如果字符串为空,返回真|
14 changes: 14 additions & 0 deletions index.json
Expand Up @@ -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",
Expand Down
40 changes: 40 additions & 0 deletions 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;
};
6 changes: 6 additions & 0 deletions test/isStrBlank.js
@@ -0,0 +1,6 @@
tests([
['licia', false],
['', true],
[' ', true],
['\r\n ', true]
]);

0 comments on commit 4d6294a

Please sign in to comment.