Skip to content

Commit

Permalink
✨ Add not function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 28, 2021
1 parent d41389f commit ac95f4c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/api/falsy/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->



## Falsy 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 Falsy values

<b>Signature:</b>

```typescript
type Falsy = false | "" | 0 | null | undefined;
```
2 changes: 2 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
| [lt](./lt/) | Returns <code>true</code> if the first argument is less than the second; otherwise <code>false</code> |
| [lte](./lte/) | Returns <code>true</code> if the first argument is less than or equal to the second; otherwise <code>false</code> |
| [multiply](./multiply/) | Multiplies first argument and second argument. |
| [not](./not/) | <b><i>(BETA)</i></b> Returns the <code>!</code> of its argument. |
| [or](./or/) | Returns true if one or both of its arguments are true; otherwise false. |
| [prepend](./prepend/) | Returns a new list with the given value at the front, followed by the contents of the list. |
| [product](./product/) | Multiplies together all the elements of a list. |
Expand All @@ -65,6 +66,7 @@
| [AnyFn](./anyfn/) | Type of any function |
| [Empty](./empty/) | <b><i>(BETA)</i></b> Alias for Empty values |
| [endsWith](./endswith/) | |
| [Falsy](./falsy/) | <b><i>(BETA)</i></b> Alias for Falsy values |
| [Ord](./ord/) | Abbreviation for Ordinal |
| [Primitive](./primitive/) | Alias for Primitive values types |

36 changes: 36 additions & 0 deletions docs/api/not/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->



## not 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 the `!` of its argument.

<b>Signature:</b>

```typescript
not: <T>(val: T) => T extends Falsy ? true : boolean
```

## Remarks

The Definition of Falsy - `''` - `false` - `0` - `NaN` - `undefined` - `null`

## Example


```ts
not('') // true
not(false) // true
not(0) // true
not(NaN) // true
not(undefined) // true
not(null) // true

not({}) // false

```

3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export { lowerCase } from './src/lowerCase.ts'
export { lt } from './src/lt.ts'
export { lte } from './src/lte.ts'
export { multiply } from './src/multiply.ts'
export { not } from './src/not.ts'
export { or } from './src/or.ts'
export { prepend } from './src/prepend.ts'
export { product } from './src/product.ts'
Expand All @@ -45,7 +46,7 @@ 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, Empty, Ord, Primitive } from './src/types.ts'
export type { AnyFn, Empty, Falsy, Ord, Primitive } from './src/types.ts'
export { upperCase } from './src/upperCase.ts'
export { values } from './src/values.ts'
export { xor } from './src/xor.ts'
33 changes: 33 additions & 0 deletions src/not.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Falsy } from './types/index.ts'
/**
* Returns the `!` of its argument.
*
* @param val - input any value
* @returns The result of `!val`
*
* @remarks
* The Definition of Falsy
* - `''`
* - `false`
* - `0`
* - `NaN`
* - `undefined`
* - `null`
*
* @example
* ```ts
* not('') // true
* not(false) // true
* not(0) // true
* not(NaN) // true
* not(undefined) // true
* not(null) // true
*
* not({}) // false
* ```
*
* @beta
*/
const not = <T>(val: T): T extends Falsy ? true : boolean =>
!val as T extends Falsy ? true : boolean
export { not }
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@ export type Ord = string | number | bigint | boolean | Date
// eslint-disable-next-line @typescript-eslint/ban-types
export type Empty = '' | [] | {}

/**
* Alias for Falsy values
*
* @beta
*/
export type Falsy = false | '' | 0 | null | undefined

export type InferArray<T> = T extends (infer R)[] ? R : never
export type Whitespace = ' ' | '\t' | '\n'
23 changes: 23 additions & 0 deletions test/not.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { not } from '@/not'

describe('not', () => {
const table: [unknown, boolean][] = [
['', true],
[undefined, true],
[null, true],
[0, true],
[NaN, true],
[false, true],
[[], false],
[{}, false],
['hello', false],
[Infinity, false],
[1, false],
[-1, false],
[true, false]
]

it.each(table)('not(%s) -> %s', (val, expected) => {
expect(not(val)).toBe(expected)
})
})

0 comments on commit ac95f4c

Please sign in to comment.