-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5403b9
commit d842fd7
Showing
7 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
|
||
|
||
## Empty type | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Alias for Empty values | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
type Empty = "" | [ | ||
] | {}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
|
||
|
||
## isEmpty variable | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Returns `true` if the given value is its type's empty value; otherwise `false`<!-- -->. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
isEmpty: (val: unknown) => val is Empty | ||
``` | ||
|
||
## Remarks | ||
|
||
The definition of Empty - `''` - `{}` - `[]` | ||
|
||
## Example | ||
|
||
|
||
```ts | ||
isEmpty('') // true | ||
isEmpty({}) // true | ||
isEmpty([]) // true | ||
|
||
isEmpty('hello world') // false | ||
isEmpty(1000) // false | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { and } from './and.ts' | ||
import { isObject } from './isObject.ts' | ||
import { isString } from './isString.ts' | ||
import { length } from './length.ts' | ||
import { Empty } from './types/index.ts' | ||
|
||
/** | ||
* Returns `true` if the given value is its type's empty value; otherwise `false`. | ||
* | ||
* @param val - input any value | ||
* @returns The result of empty or not | ||
* | ||
* @remarks | ||
* The definition of Empty | ||
* - `''` | ||
* - `{}` | ||
* - `[]` | ||
* | ||
* @example | ||
* ```ts | ||
* isEmpty('') // true | ||
* isEmpty({}) // true | ||
* isEmpty([]) // true | ||
* | ||
* isEmpty('hello world') // false | ||
* isEmpty(1000) // false | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
const isEmpty = (val: unknown): val is Empty => { | ||
if (isString(val)) return !length(val) | ||
else if (Array.isArray(val)) return !length(val) | ||
else if (isObject(val)) | ||
return and( | ||
!length(Object.keys(val as Record<string, unknown>)), | ||
(val as Record<string, unknown>).constructor === Object | ||
) | ||
else return false | ||
} | ||
|
||
export { isEmpty } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
BIG1, | ||
DATE, | ||
EMPTY_ARRAY, | ||
EMPTY_OBJECT, | ||
EMPTY_STRING, | ||
MAP, | ||
ONE, | ||
SET, | ||
SYMBOL, | ||
VOID_FN, | ||
VOID_PROMISE, | ||
WEAK_MAP, | ||
WEAK_SET, | ||
ZERO | ||
} from '@test' | ||
|
||
import { isEmpty } from '@/isEmpty' | ||
describe('isEmpty', () => { | ||
const table: [unknown, boolean][] = [ | ||
[BIG1, false], | ||
[ZERO, false], | ||
[ONE, false], | ||
[EMPTY_STRING, true], | ||
[EMPTY_OBJECT, true], | ||
[EMPTY_ARRAY, true], | ||
[MAP, false], | ||
[SET, false], | ||
['test', false], | ||
[false, false], | ||
[true, false], | ||
[SYMBOL, false], | ||
[null, false], | ||
[undefined, false], | ||
[{ nest: {} }, false], | ||
[[[]], false], | ||
[WEAK_MAP, false], | ||
[WEAK_SET, false], | ||
[VOID_FN, false], | ||
[VOID_PROMISE, false], | ||
[DATE, false] | ||
] | ||
|
||
it.each(table)('isEmpty(%s) -> %s', (val, expected) => { | ||
expect(isEmpty(val)).toEqual(expected) | ||
}) | ||
}) |