diff --git a/std/node/util.ts b/std/node/util.ts index 9879da5133..73cbbf8238 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 05cec15df6..b4e6ea41f3 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)); + }, +});