Collection of various checks for asserting types and values at runtime.
📦 Scoped @xan105
packages are for my own personal use but feel free to use them.
Check Windows/Linux 64-bit/32-bit executable
import { is64bit, is32bit } from "@xan105/is";
const is64 = await is64bit("path/to/executable");
const is32 = await is32bit("path/to/executable");
Check is PNG file
import { isPNG } from "@xan105/is";
const is = await isPNG("path/to/img");
Check winver
import * as check from "@xan105/is";
check.isWin10orGreater();
check.isWin11();
check.isWindows();
check.isWin64(); //64-bits
//etc...
Check Linux
import * as check from "@xan105/is";
check.isDebian();
check.isDebianLike(); //Debian + derivatives (eg: Ubuntu)
check.isGnome();
check.isWayland();
Check type
import * as check from "@xan105/is";
check.isStringNotEmpty("hello world");
check.isIntegerWithinRange(1,0,2);
check.isArrayOfString(["a","b"]);
//etc...
Check Object like
import { isObjLike, isString, isNumber } from "@xan105/is";
const family = {
name: "Xan",
age: 26,
child: {
name: "Xanette",
age: 15
}
};
isObjLike(family,{
name: isString,
age: isNumber,
child: {
name: isString,
age: isNumber
}
});
Perform the same check but throw an error instead.
import { shouldWin10orGreater } from "@xan105/is/assert";
shouldWin10orGreater();
import { assert } from "@xan105/is";
assert.shouldStringNotEmpty("hello world");
assert.shouldIntegerWithinRange(1,0,2, "Custom error message");
assert.shouldArrayOfString(["a","b"], new Error("custom error", { cause: err }));
//etc...
Return the given value when the condition is true otherwise null.
import { asString, asInteger } from "@xan105/is/opt";
function(option = {}){
const options = {
param1: asString(option.param1) || "hello world",
param2: asInteger(option.param2) ?? 0
};
}
npm install @xan105/is
Compatibility
- Node ✔️
import ... from "https://esm.sh/@xan105/is"
Please see https://esm.sh/ for more details.
💡 assert
and opt
are under their respective namespace.
import { assert } from "@xan105/is";
assert.shouldWin10orGreater();
import { shouldWin10orGreater } from "@xan105/is/assert";
shouldWin10orGreater();
binary
Check if it's a 64-bits (x86_64) Windows or Linux binary.
Same as above but for a 32-bits (x86) Windows or Linux binary.
type: array
Same as isArrayOfObjLike()
but at least one element in the array must pass the test instead of all
alias: isArrayOfBuffer(value: unknown): boolean
alias: isSizeArrayOfBuffer
type: number
type: object
as in a "plain obj" and not a JS obj so {}, new Object() and Object.create(null).
Check if an obj is like the specified schema.
Where schema is an obj containing a set of required property name and its corresponding check function.
If the obj has these properties and they are validated by said corresponding function then this will return true otherwise false.
Example:
const family = {
name: "Xan",
age: 26,
child: {
name: "Xanette",
age: 15,
height: 164,
weight: 42
}
};
isObjLike(family,{
name: isString,
age: isNumber,
child: {
name: isStringNotEmpty,
age: [ isIntegerWithinRange, [0,100] ],
height: isNumber,
weight: [ isNumber, [] ]
}
});
The check funtion should only return a boolean.
Otherwise or if the function throws then false will be assumed.
NB: Function that use @xan105/error will bypass this and still throw (this is by design).
The check funtion should be defined as follow: something: [function, [args,...] ]
If you don't have any args then an empty array: something: [function, [] ]
Or you can pass the function as is (shortcut): something: function
Note that something: [function]
is invalid !
💡 You can flag a property to be optional by using {optional: true}
.
If the property is missing it will be skipped.
eg: something: [function, [], {optional: true}]
Plain object assigned as property within another:
{
foo: {
bar: "foo"
},
bar: {
foo: "bar"
}
}
type: string
If pattern is a string, this function will look for a corresponding known regex pattern.
As of this writing, these are:
hex
: HexadecimalSRI
: Subresource Integrity
type: other
alias: isBuffer(value: unknown): boolean
os: Windows
alias: isWin32(): boolean
alias: isWin64(): boolean
os: Linux
alias: isRaspbian(): Promise<boolean>
misc
Perform the same checks as above but throw an error instead.
This replace the cumbersome and often repetitive "if(unexpected) throw Error" pattern.
💡 Every assertion has an optional error
parameter to override the default Error.
You can either use
- an instance of/inherit from the
Error
class - or a string to just change the default message.
eg:
assert.shouldIntegerWithinRange(1,0,2, "Custom error message");
assert.shouldArrayOfString(["a","b"], new Error("custom error", { cause: err }));
binary
type: array
shouldArrayOfStringLike(value: unknown, pattern: RegExp | string, error?: Error | string | null): void
shouldSizeArrayOfStringLike(value: unknown, length: number, pattern: RegExp | string, error?: Error | string | null): void
shouldArrayOfNumberWithinRange(value: unknown, min: number, max: number, error?: Error | string): void
shouldSizeArrayOfNumberWithinRange(value: unknown, length: number, min: number, max: number, error?: Error | string): void
shouldSizeArrayOfIntegerPositiveOrZero(value: unknown, length: number, error?: Error | string): void
shouldArrayOfIntegerWithinRange(value: unknown, min: number, max: number, error?: Error | string): void
shouldSizeArrayOfIntegerWithinRange(value: unknown, length: number, min: number, max: number, error?: Error | string): void
shouldSizeArrayOfObjWithProperties(value: unknown, length: number, prop: string[], error?: Error | string): void
shouldSizeArrayOfObjLike(value: unknown, length: number, schema: object, error?: Error | string): void
alias: shouldArrayOfBuffer(value: unknown, error?: Error | string): void
alias: shouldSizeArrayOfBuffer(value: unknown, length: number, error?: Error | string): void
type: number
type: object
type: string
type: other
alias: #### shouldBuffer(value: unknown, error?: Error | string): void
os: Windows
alias: shouldWin32(error?: Error | string): void
alias: shouldWin64(error?: Error | string): void
os: Linux
alias: shouldRaspbian(error?: Error | string): Promise<void>
misc
Return the given value when the condition is true otherwise null.
Works great with operator such as ||
and ??
eg:
function(option = {}){
const options = {
param1: asString(option.param1) || "hello world",
param2: asInteger(option.param2) ?? 0
};
}
type: array
`asSizeArrayOfStringLike(value: unknown, length: number, pattern: RegExp | string): string[] | null;
asSizeArrayOfNumberWithinRange(value: unknown, length: number, min: number, max: number): number[] | null
asSizeArrayOfIntegerWithinRange(value: unknown, length: number, min: number, max: number): number[] | null
This will return every element matching the given schema.
Unlike asArrayOfObjLike
which return the array only if all elements pass the test.
alias: asArrayOfBuffer(value: unknown): Uint8Array[] | Buffer[] | null
alias: asSizeArrayOfBuffer(value: unknown, length: number): Uint8Array[] | Buffer[] | null
type: number
type: object
type: string
type: other
alias: asBuffer(value: unknown): Uint8Array | Buffer | null