Skip to content

Commit f5d7a05

Browse files
committed
feat: string utils
1 parent 4765c52 commit f5d7a05

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { isBlank } from '../is-blank.js';
3+
4+
describe('is-blank', () => {
5+
it.each([undefined, null, '', ' ', '\n'])(
6+
'returns true when string is `%s`q',
7+
async (text: null | undefined | string) => {
8+
expect(isBlank(text)).toBe(true);
9+
}
10+
);
11+
12+
it.each(['a', ' a', 'a ', ' a '])(
13+
'returns false when string is `%s`',
14+
async (text: string) => {
15+
expect(isBlank(text)).toBe(false);
16+
}
17+
);
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { isEmpty } from '../is-empty.js';
3+
4+
describe('is-empty', () => {
5+
it.each([undefined, null, ''])(
6+
'returns true when string is `%s`',
7+
async (text: null | undefined | string) => {
8+
expect(isEmpty(text)).toBe(true);
9+
}
10+
);
11+
12+
it.each(['a', ' a', 'a ', ' a ', ' ', '\n'])(
13+
'returns false when string is `%s`',
14+
async (text: string) => {
15+
expect(isEmpty(text)).toBe(false);
16+
}
17+
);
18+
});

electron/common/string/is-blank.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { isEmpty } from './is-empty.js';
2+
3+
/**
4+
* Returns true if the text is undefined, null, or is empty when trimmed.
5+
* Whitespace characters are ignored.
6+
*
7+
* We use a type guard in result to hint that if this function returns false
8+
* then the value cannot be null or undefined.
9+
*/
10+
export function isBlank(
11+
text: string | null | undefined
12+
): text is null | undefined {
13+
return isEmpty(text?.trim());
14+
}

electron/common/string/is-empty.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Returns true if the text is undefined, null, or empty string ('').
3+
* Whitespace characters are considered non-empty.
4+
*
5+
* We use a type guard in result to hint that if this function returns false
6+
* then the value cannot be null or undefined.
7+
*/
8+
export function isEmpty(
9+
text: string | null | undefined
10+
): text is null | undefined {
11+
return !text || text === '';
12+
}

0 commit comments

Comments
 (0)