Skip to content

Commit

Permalink
feat(node): add isPrimitive (denoland/deno#4673)
Browse files Browse the repository at this point in the history
  • Loading branch information
disizali authored and caspervonb committed Jan 31, 2021
1 parent 32b412a commit 50b8211
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions node/util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
},
});

0 comments on commit 50b8211

Please sign in to comment.