Skip to content

Commit

Permalink
✨ Add isEmpty function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 28, 2021
1 parent e5403b9 commit d842fd7
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 1 deletion.
17 changes: 17 additions & 0 deletions docs/api/empty/index.md
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 = "" | [
] | {};
```
2 changes: 2 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
| [inc](./inc/) | Increments its argument. |
| [isBigint](./isbigint/) | Whatever argument is type of <code>bigint</code> or not. |
| [isBoolean](./isboolean/) | Whatever argument is type of <code>boolean</code> or not. |
| [isEmpty](./isempty/) | <b><i>(BETA)</i></b> Returns <code>true</code> if the given value is its type's empty value; otherwise <code>false</code>. |
| [isFunction](./isfunction/) | Whatever argument is type of <code>function</code> or not. |
| [isNill](./isnill/) | Whatever argument is type of <code>undefined</code> or <code>null</code>. |
| [isNull](./isnull/) | Whatever argument is type of <code>null</code> or not. |
Expand Down Expand Up @@ -59,6 +60,7 @@
| Type Alias | Description |
| --- | --- |
| [AnyFn](./anyfn/) | Type of any function |
| [Empty](./empty/) | <b><i>(BETA)</i></b> Alias for Empty values |
| [endsWith](./endswith/) | |
| [Ord](./ord/) | Abbreviation for Ordinal |
| [Primitive](./primitive/) | Alias for Primitive values types |
Expand Down
34 changes: 34 additions & 0 deletions docs/api/isempty/index.md
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

```

3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { identity } from './src/identity.ts'
export { inc } from './src/inc.ts'
export { isBigint } from './src/isBigint.ts'
export { isBoolean } from './src/isBoolean.ts'
export { isEmpty } from './src/isEmpty.ts'
export { isFunction } from './src/isFunction.ts'
export { isNill } from './src/isNill.ts'
export { isNull } from './src/isNull.ts'
Expand All @@ -42,6 +43,6 @@ export { sum } from './src/sum.ts'
export { T } from './src/T.ts'
export { tail } from './src/tail.ts'
export { trim } from './src/trim.ts'
export type { AnyFn, Ord, Primitive } from './src/types.ts'
export type { AnyFn, Empty, Ord, Primitive } from './src/types.ts'
export { upperCase } from './src/upperCase.ts'
export { xor } from './src/xor.ts'
44 changes: 44 additions & 0 deletions src/isEmpty.ts
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 }
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ export type Primitive =
* @public
*/
export type Ord = string | number | bigint | boolean | Date

/**
* Alias for Empty values
*
* @beta
*/
export type Empty = '' | [] | {}

export type InferArray<T> = T extends (infer R)[] ? R : never
export type Whitespace = ' ' | '\t' | '\n'
47 changes: 47 additions & 0 deletions test/isEmpty.spec.ts
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)
})
})

0 comments on commit d842fd7

Please sign in to comment.