-
-
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
401328c
commit 28dc49a
Showing
4 changed files
with
77 additions
and
0 deletions.
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
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,35 @@ | ||
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license. | ||
import { Falsy } from './types/index.ts' | ||
/** | ||
* Abbreviation for Not Not. 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 | ||
* NN('') // false | ||
* NN(false) // false | ||
* NN(0) // false | ||
* NN(NaN) // false | ||
* NN(undefined) // false | ||
* NN(null) // false | ||
* | ||
* NN({}) // true | ||
* NN([]) // true | ||
* ``` | ||
* | ||
* @public | ||
*/ | ||
const NN = <T>(val: T): T extends Falsy ? false : boolean => | ||
!!val as T extends Falsy ? false : boolean | ||
export { NN } |
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,40 @@ | ||
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license. | ||
import { assertEquals } from '../deps.ts' | ||
import { NN } from '../src/NN.ts' | ||
import { assertEqual } from './asserts.ts' | ||
|
||
Deno.test('NN', () => { | ||
const table: [unknown, boolean][] = [ | ||
['', false], | ||
[undefined, false], | ||
[null, false], | ||
[0, false], | ||
[NaN, false], | ||
[false, false], | ||
[[], true], | ||
[{}, true], | ||
['hello', true], | ||
[Infinity, true], | ||
[1, true], | ||
[-1, true], | ||
[true, true] | ||
] | ||
|
||
table.forEach(([val, expected]) => { | ||
assertEquals(NN(val), expected, `NN(${val}) -> ${expected}`) | ||
}) | ||
|
||
assertEqual<false>(NN('' as const)) | ||
assertEqual<false>(NN(0 as const)) | ||
assertEqual<false>(NN(false as const)) | ||
assertEqual<false>(NN(undefined)) | ||
assertEqual<false>(NN(null)) | ||
assertEqual<boolean>(NN(String)) | ||
assertEqual<boolean>(NN(Number)) | ||
assertEqual<boolean>(NN(NaN)) | ||
assertEqual<boolean>(NN(BigInt)) | ||
assertEqual<boolean>(NN(Symbol)) | ||
assertEqual<boolean>(NN(Date)) | ||
assertEqual<boolean>(NN(Object)) | ||
assertEqual<boolean>(NN(Array)) | ||
}) |