diff options
author | Ali Hasani <a.hassssani@gmail.com> | 2020-04-09 03:14:39 +0430 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-08 18:44:39 -0400 |
commit | 90d683127147975a08df45bac0de6b719ca07ce4 (patch) | |
tree | 879bdffc6832443d784754334e45a5a048d7c4c6 /std/node | |
parent | 68bde7a0c693794ff778c09154833ac0e9d177b4 (diff) |
feat(std/node): add isPrimitive (#4673)
Diffstat (limited to 'std/node')
-rw-r--r-- | std/node/util.ts | 6 | ||||
-rw-r--r-- | std/node/util_test.ts | 24 |
2 files changed, 30 insertions, 0 deletions
diff --git a/std/node/util.ts b/std/node/util.ts index 9879da513..73cbbf823 100644 --- a/std/node/util.ts +++ b/std/node/util.ts @@ -46,6 +46,12 @@ export function isRegExp(value: unknown): boolean { return value instanceof RegExp; } +export function isPrimitive(value: unknown): boolean { + return ( + value === null || (typeof value !== "object" && typeof value !== "function") + ); +} + export function validateIntegerRange( value: number, name: string, diff --git a/std/node/util_test.ts b/std/node/util_test.ts index 05cec15df..b4e6ea41f 100644 --- a/std/node/util_test.ts +++ b/std/node/util_test.ts @@ -124,3 +124,27 @@ test({ assert(!util.isArray(null)); }, }); + +test({ + name: "[util] isPrimitive", + fn() { + const stringType = "hasti"; + const booleanType = true; + const integerType = 2; + const symbolType = Symbol("anything"); + + const functionType = function doBest(): void {}; + const objectType = { name: "ali" }; + const arrayType = [1, 2, 3]; + + assert(util.isPrimitive(stringType)); + assert(util.isPrimitive(booleanType)); + assert(util.isPrimitive(integerType)); + assert(util.isPrimitive(symbolType)); + assert(util.isPrimitive(null)); + assert(util.isPrimitive(undefined)); + assert(!util.isPrimitive(functionType)); + assert(!util.isPrimitive(arrayType)); + assert(!util.isPrimitive(objectType)); + }, +}); |