-
-
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
147a199
commit 1090447
Showing
6 changed files
with
186 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { and } from '../and.ts' | ||
import { ifElse } from '../ifElse.ts' | ||
import { isNil } from '../isNil.ts' | ||
import { N } from '../N.ts' | ||
|
||
/** | ||
* Safe getter for `constructor.name`. | ||
* @param val - Any value | ||
* @returns If `val` is `null` or `undefined`, empty string; otherwise `constructor.name` | ||
* | ||
* @example | ||
* ```ts | ||
* constructorName(null) // '' | ||
* constructorName(undefined) // '' | ||
* constructorName({}) // 'Object' | ||
* constructorName('') // 'String' | ||
* ``` | ||
* | ||
* @internal | ||
*/ | ||
const constructorName = (val: unknown): string => | ||
ifElse( | ||
and(N(isNil(val)), () => (val as Record<string, unknown>).constructor), | ||
() => (val as Record<string, unknown>).constructor.name ?? '', | ||
'' | ||
) | ||
|
||
export { constructorName } |
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,27 @@ | ||
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license. | ||
|
||
import { constructorName } from './_/constructorName.ts' | ||
import { JSON_OBJECT } from './constants/index.ts' | ||
|
||
/** | ||
* Whatever argument is JSON Object or not. | ||
* | ||
* @param val - Input any value | ||
* @returns if `val` is JSON Object `true` otherwise; `false` | ||
* | ||
* @example | ||
* ```ts | ||
* isJSONObject({ hoge: 'huga'}) // true | ||
* isJSONObject(Object()) // true | ||
* isJSONObject(new Object()) // true | ||
* | ||
* isJSONObject([]) // false | ||
* isJSONObject(new Set()) // false | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const isJSONObject = (val: unknown): val is Record<PropertyKey, unknown> => | ||
constructorName(val) === JSON_OBJECT | ||
|
||
export { isJSONObject } |
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,68 @@ | ||
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license. | ||
import { assertEquals } from '../../dev_deps.ts' | ||
import { constructorName } from '../../src/_/constructorName.ts' | ||
import { isSymbol } from '../../src/isSymbol.ts' | ||
|
||
Deno.test('constructorName', () => { | ||
function fn() { | ||
true | ||
} | ||
class Class {} | ||
const table: [unknown, string][] = [ | ||
[Object, 'Function'], | ||
[new Object(), 'Object'], | ||
[Object(), 'Object'], | ||
[{}, 'Object'], | ||
[{ hoge: 'huga' }, 'Object'], | ||
[new Number(), 'Number'], | ||
[Number(), 'Number'], | ||
[Number, 'Function'], | ||
[1, 'Number'], | ||
['', 'String'], | ||
[String(), 'String'], | ||
[String, 'Function'], | ||
[new String(), 'String'], | ||
[new Boolean(), 'Boolean'], | ||
[true, 'Boolean'], | ||
[Boolean, 'Function'], | ||
[new Boolean(), 'Boolean'], | ||
[Boolean(), 'Boolean'], | ||
[undefined, ''], | ||
[null, ''], | ||
[Symbol(), 'Symbol'], | ||
[Symbol, 'Function'], | ||
[1n, 'BigInt'], | ||
[BigInt, 'Function'], | ||
[Array, 'Function'], | ||
[Array([]), 'Array'], | ||
[new Array([]), 'Array'], | ||
[[], 'Array'], | ||
[Date, 'Function'], | ||
[new Date(0), 'Date'], | ||
[Date(), 'String'], | ||
[/a/, 'RegExp'], | ||
[RegExp, 'Function'], | ||
[new RegExp(''), 'RegExp'], | ||
[Error, 'Function'], | ||
[Error(''), 'Error'], | ||
[new Error(), 'Error'], | ||
[TypeError, 'Function'], | ||
[TypeError(), 'TypeError'], | ||
[new TypeError(), 'TypeError'], | ||
[Set, 'Function'], | ||
[new Set(), 'Set'], | ||
[Map, 'Function'], | ||
[new Map(), 'Map'], | ||
[() => true, 'Function'], | ||
[fn, 'Function'], | ||
[Class, 'Function'], | ||
[new Class(), 'Class'] | ||
] | ||
table.forEach(([val, expected]) => { | ||
assertEquals( | ||
constructorName(val), | ||
expected, | ||
`constructorName(${isSymbol(val) ? 'symbol' : val}) -> ${expected}` | ||
) | ||
}) | ||
}) |
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,61 @@ | ||
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license. | ||
import { assertEquals } from '../dev_deps.ts' | ||
import { isJSONObject } from '../src/isJSONObject.ts' | ||
import { isSymbol } from '../src/isSymbol.ts' | ||
import { | ||
BIG1, | ||
DATE, | ||
EMPTY_ARRAY, | ||
EMPTY_OBJECT, | ||
EMPTY_STRING, | ||
MAP, | ||
ONE, | ||
SET, | ||
SYMBOL, | ||
VOID_FN, | ||
VOID_PROMISE, | ||
WEAK_MAP, | ||
WEAK_SET, | ||
ZERO | ||
} from './index.ts' | ||
|
||
Deno.test('isJSONObject', () => { | ||
const table: [unknown, boolean][] = [ | ||
[EMPTY_OBJECT, true], | ||
[Object(), true], | ||
[{ nest: {} }, true], | ||
[new Object(), true], | ||
[new Object({ hoge: 'huga' }), true], | ||
[new Object({ hoge: [], huga: {} }), true], | ||
[Object, false], | ||
[NaN, false], | ||
[Infinity, false], | ||
[-Infinity, false], | ||
[null, false], | ||
[undefined, false], | ||
[ZERO, false], | ||
[ONE, false], | ||
[EMPTY_STRING, false], | ||
['test', false], | ||
[false, false], | ||
[true, false], | ||
[BIG1, false], | ||
[SYMBOL, false], | ||
[EMPTY_ARRAY, false], | ||
[[[]], false], | ||
[MAP, false], | ||
[SET, false], | ||
[WEAK_MAP, false], | ||
[WEAK_SET, false], | ||
[VOID_FN, false], | ||
[VOID_PROMISE, false], | ||
[DATE, false] | ||
] | ||
table.forEach(([val, expected]) => { | ||
assertEquals( | ||
isJSONObject(val), | ||
expected, | ||
`isJSONObject(${isSymbol(val) ? 'symbol' : val}) -> ${expected}` | ||
) | ||
}) | ||
}) |