@klyper/types 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -0
- package/dist/index.cjs +124 -0
- package/dist/index.d.ts +104 -0
- package/dist/index.js +95 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @klyper/types
|
|
2
|
+
|
|
3
|
+
Shared TypeScript type aliases and runtime type guards for Klyper packages.
|
|
4
|
+
|
|
5
|
+
Use this package when a module needs small, dependency-free helpers for narrowing
|
|
6
|
+
unknown values or reusing common utility types.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install @klyper/types
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
This package is published as an ES module package and also exposes a CommonJS
|
|
15
|
+
entry through `main`.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { isPlainObject, isString, type } from "@klyper/types";
|
|
21
|
+
|
|
22
|
+
const value: unknown = { title: "Klyper" };
|
|
23
|
+
|
|
24
|
+
if (isPlainObject(value) && isString(value.title)) {
|
|
25
|
+
value.title.toUpperCase();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type(value); // "object"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The runtime helpers accept `unknown` values and return TypeScript type predicates
|
|
32
|
+
where possible.
|
|
33
|
+
|
|
34
|
+
## Runtime API
|
|
35
|
+
|
|
36
|
+
### Type detection
|
|
37
|
+
|
|
38
|
+
| Export | Description |
|
|
39
|
+
| --- | --- |
|
|
40
|
+
| `type(value)` | Returns a normalized logical type name. `null` and `undefined` both return `"null"`. |
|
|
41
|
+
|
|
42
|
+
Recognized logical type names include `"array"`, `"asyncfunction"`, `"bigint"`,
|
|
43
|
+
`"boolean"`, `"date"`, `"error"`, `"function"`, `"map"`, `"null"`, `"number"`,
|
|
44
|
+
`"object"`, `"promise"`, `"regexp"`, `"set"`, `"string"`, `"symbol"`,
|
|
45
|
+
`"weakmap"`, and `"weakset"`.
|
|
46
|
+
|
|
47
|
+
### Type guards
|
|
48
|
+
|
|
49
|
+
| Export | Narrows to | Notes |
|
|
50
|
+
| --- | --- | --- |
|
|
51
|
+
| `isArray(value)` | `Array<any>` | Checks array values. |
|
|
52
|
+
| `isAsyncFunction(value)` | `TypeAnyAsyncFunction` | Checks functions declared with `async`. |
|
|
53
|
+
| `isBigInt(value)` | `bigint` | Checks bigint primitives. |
|
|
54
|
+
| `isBoolean(value)` | `boolean` | Checks boolean primitives. |
|
|
55
|
+
| `isConstructor(value)` | `TypeAnyConstructor` | Checks functions whose prototype constructor points back to the function. |
|
|
56
|
+
| `isDate(value)` | `Date` | Checks `Date` instances. |
|
|
57
|
+
| `isDef<T>(value)` | `T` | Returns `false` for `null` and `undefined`. |
|
|
58
|
+
| `isError(value)` | `Error` | Checks `Error` instances. |
|
|
59
|
+
| `isFiniteNumber(value)` | `number` | Checks finite number primitives. |
|
|
60
|
+
| `isFloat(value)` | `number` | Checks finite numbers with a fractional part. |
|
|
61
|
+
| `isFunction(value)` | `TypeAnyFunction` | Checks callable values. |
|
|
62
|
+
| `isInteger(value)` | `number` | Checks integer number primitives. |
|
|
63
|
+
| `isMap<K, V>(value)` | `Map<K, V>` | Checks `Map` instances. |
|
|
64
|
+
| `isNil(value)` | `null \| undefined` | Checks only `null` and `undefined`. |
|
|
65
|
+
| `isNonEmptyArray<T>(value)` | `[T, ...Array<T>]` | Checks arrays with at least one item. |
|
|
66
|
+
| `isNull(value)` | `null` | Checks only `null`. |
|
|
67
|
+
| `isNumber(value)` | `number` | Checks number primitives, including `NaN` and infinities. |
|
|
68
|
+
| `isNumberLike(value)` | `boolean` | Checks strings that represent finite decimal numbers. Scientific, binary, hex, `NaN`, and `Infinity` strings are rejected. |
|
|
69
|
+
| `isObject(value)` | `TypeAnyObject` | Checks values whose normalized logical type is `"object"`, including object literals, `Object.create(null)`, and class instances. Arrays, maps, sets, dates, and regexps are excluded. |
|
|
70
|
+
| `isPlainObject(value)` | `TypeAnyObject` | Checks object literals and `Object.create(null)` values. Class instances are excluded. |
|
|
71
|
+
| `isPromise<T>(value)` | `Promise<T>` | Checks `Promise` instances, not arbitrary thenables. |
|
|
72
|
+
| `isRegExp(value)` | `RegExp` | Checks regular expressions. |
|
|
73
|
+
| `isScalar(value)` | `TypeScalar` | Checks boolean, number, and string values. |
|
|
74
|
+
| `isSet<T>(value)` | `Set<T>` | Checks `Set` instances. |
|
|
75
|
+
| `isString(value)` | `string` | Checks string primitives. |
|
|
76
|
+
| `isSymbol(value)` | `symbol` | Checks symbol primitives. |
|
|
77
|
+
| `isUndefined(value)` | `undefined` | Checks only `undefined`. |
|
|
78
|
+
|
|
79
|
+
## Type Aliases
|
|
80
|
+
|
|
81
|
+
Common exported type aliases include:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import type {
|
|
85
|
+
TypeAnyAsyncFunction,
|
|
86
|
+
TypeAnyConstructor,
|
|
87
|
+
TypeAnyFunction,
|
|
88
|
+
TypeAnyObject,
|
|
89
|
+
TypeCallback,
|
|
90
|
+
TypeNullable,
|
|
91
|
+
TypeName,
|
|
92
|
+
TypeObjectOptional,
|
|
93
|
+
TypeObjectRequired,
|
|
94
|
+
TypeScalar,
|
|
95
|
+
} from "@klyper/types";
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Notable groups:
|
|
99
|
+
|
|
100
|
+
- Array aliases: `TypeArray`, `TypeAnyArray`, `TypeFunctionArray`,
|
|
101
|
+
`TypeNumberArray`, `TypeObjectArray`, and `TypeStringArray`.
|
|
102
|
+
- Object aliases: `TypeObject`, `TypeAnyObject`, `TypeBooleanObject`,
|
|
103
|
+
`TypeFunctionObject`, `TypeNumberObject`, and `TypeStringObject`.
|
|
104
|
+
- Object shape helpers: `TypeObjectOptional`, `TypeObjectRequired`,
|
|
105
|
+
`TypeObjectKeys`, and `TypeObjectKeysFunction`.
|
|
106
|
+
- Callback aliases: `TypeCallback`, `TypeCallbackArray`, `TypeCallbackMap`,
|
|
107
|
+
and `TypeCallbackObject`.
|
|
108
|
+
- Value helpers: `TypeKey`, `TypeNullable`, `TypeOptional`, and `TypeScalar`.
|
|
109
|
+
|
|
110
|
+
Primitive guards such as `isString`, `isNumber`, `isBoolean`, `isBigInt`, and
|
|
111
|
+
`isSymbol` only accept primitives. Boxed wrappers such as `new String("x")`,
|
|
112
|
+
`new Number(1)`, and `new Boolean(false)` return `false`.
|
|
113
|
+
|
|
114
|
+
## Commands
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
npm run build --workspace @klyper/types
|
|
118
|
+
npm run check --workspace @klyper/types
|
|
119
|
+
npm run lint --workspace @klyper/types
|
|
120
|
+
npm test --workspace @klyper/types
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
`build` uses Rollup for the package output. `check` runs TypeScript without emitting files.
|
|
124
|
+
|
|
125
|
+
## Validation
|
|
126
|
+
|
|
127
|
+
Run the package checks before publishing or changing exports:
|
|
128
|
+
|
|
129
|
+
```sh
|
|
130
|
+
npm run check --workspace @klyper/types
|
|
131
|
+
npm test --workspace @klyper/types
|
|
132
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @klyper/types v0.3.0
|
|
3
|
+
* Klyper types package.
|
|
4
|
+
* (c) 2026 Andrew Caires
|
|
5
|
+
* @license: MIT
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
const typeNames = {
|
|
11
|
+
"[object Array]": "array",
|
|
12
|
+
"[object AsyncFunction]": "asyncfunction",
|
|
13
|
+
"[object BigInt]": "bigint",
|
|
14
|
+
"[object Boolean]": "boolean",
|
|
15
|
+
"[object Date]": "date",
|
|
16
|
+
"[object Error]": "error",
|
|
17
|
+
"[object Function]": "function",
|
|
18
|
+
"[object Map]": "map",
|
|
19
|
+
"[object Number]": "number",
|
|
20
|
+
"[object Object]": "object",
|
|
21
|
+
"[object Promise]": "promise",
|
|
22
|
+
"[object RegExp]": "regexp",
|
|
23
|
+
"[object Set]": "set",
|
|
24
|
+
"[object String]": "string",
|
|
25
|
+
"[object Symbol]": "symbol",
|
|
26
|
+
"[object WeakMap]": "weakmap",
|
|
27
|
+
"[object WeakSet]": "weakset",
|
|
28
|
+
};
|
|
29
|
+
const type = (test) => {
|
|
30
|
+
if (test == null) {
|
|
31
|
+
return "null";
|
|
32
|
+
}
|
|
33
|
+
return typeNames[Object.prototype.toString.call(test)] ?? "object";
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const isArray = (test) => type(test) == "array";
|
|
37
|
+
|
|
38
|
+
const isAsyncFunction = (test) => type(test) == "asyncfunction";
|
|
39
|
+
|
|
40
|
+
const isBigInt = (test) => typeof test == "bigint";
|
|
41
|
+
|
|
42
|
+
const isBoolean = (test) => typeof test == "boolean";
|
|
43
|
+
|
|
44
|
+
const isFunction = (test) => typeof test == "function";
|
|
45
|
+
|
|
46
|
+
const isConstructor = (test) => isFunction(test) && !!test.prototype && test.prototype.constructor === test;
|
|
47
|
+
|
|
48
|
+
const isDate = (test) => type(test) == "date";
|
|
49
|
+
|
|
50
|
+
const isNil = (test) => test === null || test === undefined;
|
|
51
|
+
|
|
52
|
+
const isDef = (test) => !isNil(test);
|
|
53
|
+
|
|
54
|
+
const isError = (test) => type(test) == "error";
|
|
55
|
+
|
|
56
|
+
const isFiniteNumber = (test) => Number.isFinite(test);
|
|
57
|
+
|
|
58
|
+
const isFloat = (test) => isFiniteNumber(test) && !Number.isInteger(test);
|
|
59
|
+
|
|
60
|
+
const isInteger = (test) => Number.isInteger(test);
|
|
61
|
+
|
|
62
|
+
const isMap = (test) => type(test) == "map";
|
|
63
|
+
|
|
64
|
+
const isNonEmptyArray = (test) => isArray(test) && test.length > 0;
|
|
65
|
+
|
|
66
|
+
const isNull = (test) => test === null;
|
|
67
|
+
|
|
68
|
+
const isNumber = (test) => typeof test == "number";
|
|
69
|
+
|
|
70
|
+
const NUMBER_PATTERN = /^-?[0-9]+(\.[0-9]+)?$/;
|
|
71
|
+
const isNumberLike = (value) => NUMBER_PATTERN.test(value);
|
|
72
|
+
|
|
73
|
+
const isObject = (test) => type(test) == "object";
|
|
74
|
+
|
|
75
|
+
const isPlainObject = (test) => {
|
|
76
|
+
if (!isObject(test) || type(test) != "object") {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const prototype = Object.getPrototypeOf(test);
|
|
80
|
+
return prototype === null || prototype === Object.prototype;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const isPromise = (test) => type(test) == "promise";
|
|
84
|
+
|
|
85
|
+
const isRegExp = (test) => type(test) == "regexp";
|
|
86
|
+
|
|
87
|
+
const isString = (test) => typeof test == "string";
|
|
88
|
+
|
|
89
|
+
const isScalar = (test) => isBoolean(test) || isNumber(test) || isString(test);
|
|
90
|
+
|
|
91
|
+
const isSet = (test) => type(test) == "set";
|
|
92
|
+
|
|
93
|
+
const isSymbol = (test) => typeof test == "symbol";
|
|
94
|
+
|
|
95
|
+
const isUndefined = (test) => test === undefined;
|
|
96
|
+
|
|
97
|
+
exports.isArray = isArray;
|
|
98
|
+
exports.isAsyncFunction = isAsyncFunction;
|
|
99
|
+
exports.isBigInt = isBigInt;
|
|
100
|
+
exports.isBoolean = isBoolean;
|
|
101
|
+
exports.isConstructor = isConstructor;
|
|
102
|
+
exports.isDate = isDate;
|
|
103
|
+
exports.isDef = isDef;
|
|
104
|
+
exports.isError = isError;
|
|
105
|
+
exports.isFiniteNumber = isFiniteNumber;
|
|
106
|
+
exports.isFloat = isFloat;
|
|
107
|
+
exports.isFunction = isFunction;
|
|
108
|
+
exports.isInteger = isInteger;
|
|
109
|
+
exports.isMap = isMap;
|
|
110
|
+
exports.isNil = isNil;
|
|
111
|
+
exports.isNonEmptyArray = isNonEmptyArray;
|
|
112
|
+
exports.isNull = isNull;
|
|
113
|
+
exports.isNumber = isNumber;
|
|
114
|
+
exports.isNumberLike = isNumberLike;
|
|
115
|
+
exports.isObject = isObject;
|
|
116
|
+
exports.isPlainObject = isPlainObject;
|
|
117
|
+
exports.isPromise = isPromise;
|
|
118
|
+
exports.isRegExp = isRegExp;
|
|
119
|
+
exports.isScalar = isScalar;
|
|
120
|
+
exports.isSet = isSet;
|
|
121
|
+
exports.isString = isString;
|
|
122
|
+
exports.isSymbol = isSymbol;
|
|
123
|
+
exports.isUndefined = isUndefined;
|
|
124
|
+
exports.type = type;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @klyper/types v0.3.0
|
|
3
|
+
* Klyper types package.
|
|
4
|
+
* (c) 2026 Andrew Caires
|
|
5
|
+
* @license: MIT
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
declare const isArray: <T = unknown>(test: unknown) => test is Array<T>;
|
|
9
|
+
|
|
10
|
+
type TypeAnyConstructor<T = any> = new (...args: Array<any>) => T;
|
|
11
|
+
type TypeAnyAsyncFunction<T = any> = (...args: Array<any>) => Promise<T>;
|
|
12
|
+
type TypeAnyFunction<T = any> = (...args: Array<any>) => T;
|
|
13
|
+
type TypeArray<T> = Array<T>;
|
|
14
|
+
type TypeAnyArray = TypeArray<any>;
|
|
15
|
+
type TypeFunctionArray = TypeArray<TypeAnyFunction>;
|
|
16
|
+
type TypeNumberArray = TypeArray<number>;
|
|
17
|
+
type TypeObjectArray<T = any> = TypeArray<TypeObject<T>>;
|
|
18
|
+
type TypeStringArray = TypeArray<string>;
|
|
19
|
+
type TypeKey = number | string | symbol;
|
|
20
|
+
type TypeNullable<T> = T | null;
|
|
21
|
+
type TypeScalar = boolean | number | string;
|
|
22
|
+
type TypeOptional<T> = T | null | undefined;
|
|
23
|
+
type TypeObject<T, K extends keyof any = string> = Record<K, T>;
|
|
24
|
+
type TypeAnyObject = TypeObject<any>;
|
|
25
|
+
type TypeBooleanObject = TypeObject<boolean>;
|
|
26
|
+
type TypeFunctionObject = TypeObject<TypeAnyFunction>;
|
|
27
|
+
type TypeNumberObject = TypeObject<number>;
|
|
28
|
+
type TypeStringObject = TypeObject<string>;
|
|
29
|
+
type TypeObjectOptional<T, K extends keyof T = keyof T> = {
|
|
30
|
+
[P in keyof T as P extends K ? never : P]: T[P];
|
|
31
|
+
} & {
|
|
32
|
+
[P in keyof T as P extends K ? P : never]?: T[P];
|
|
33
|
+
};
|
|
34
|
+
type TypeObjectRequired<T, K extends keyof T> = {
|
|
35
|
+
[P in K]-?: T[P];
|
|
36
|
+
} & {
|
|
37
|
+
[P in Exclude<keyof T, K>]: T[P];
|
|
38
|
+
};
|
|
39
|
+
type TypeObjectKeys<T, F = any> = {
|
|
40
|
+
[K in keyof T]: T[K] extends F ? K : never;
|
|
41
|
+
}[keyof T];
|
|
42
|
+
type TypeObjectKeysFunction<T> = TypeObjectKeys<T, TypeAnyFunction>;
|
|
43
|
+
type TypeCallback<T, K, O, R> = (value: T, key: K, object: O) => R;
|
|
44
|
+
type TypeCallbackArray<T> = TypeCallback<T, number, TypeArray<T>, any>;
|
|
45
|
+
type TypeCallbackMap<T> = TypeCallback<T, number, TypeArray<T>, T>;
|
|
46
|
+
type TypeCallbackObject<T> = TypeCallback<T, string, TypeObject<T>, any>;
|
|
47
|
+
|
|
48
|
+
declare const isAsyncFunction: (test: unknown) => test is TypeAnyAsyncFunction;
|
|
49
|
+
|
|
50
|
+
declare const isBigInt: (test: unknown) => test is bigint;
|
|
51
|
+
|
|
52
|
+
declare const isBoolean: (test: unknown) => test is boolean;
|
|
53
|
+
|
|
54
|
+
declare const isConstructor: (test: unknown) => test is TypeAnyConstructor;
|
|
55
|
+
|
|
56
|
+
declare const isDate: (test: unknown) => test is Date;
|
|
57
|
+
|
|
58
|
+
declare const isDef: <T = unknown>(test: T | null | undefined) => test is T;
|
|
59
|
+
|
|
60
|
+
declare const isError: (test: unknown) => test is Error;
|
|
61
|
+
|
|
62
|
+
declare const isFiniteNumber: (test: unknown) => test is number;
|
|
63
|
+
|
|
64
|
+
declare const isFloat: (test: unknown) => test is number;
|
|
65
|
+
|
|
66
|
+
declare const isFunction: (test: unknown) => test is TypeAnyFunction;
|
|
67
|
+
|
|
68
|
+
declare const isInteger: (test: unknown) => test is number;
|
|
69
|
+
|
|
70
|
+
declare const isMap: <K = any, V = any>(test: unknown) => test is Map<K, V>;
|
|
71
|
+
|
|
72
|
+
declare const isNil: (test: unknown) => test is null | undefined;
|
|
73
|
+
|
|
74
|
+
declare const isNonEmptyArray: <T = unknown>(test: unknown) => test is [T, ...Array<T>];
|
|
75
|
+
|
|
76
|
+
declare const isNull: (test: unknown) => test is null;
|
|
77
|
+
|
|
78
|
+
declare const isNumber: (test: unknown) => test is number;
|
|
79
|
+
|
|
80
|
+
declare const isNumberLike: (value: string) => boolean;
|
|
81
|
+
|
|
82
|
+
declare const isObject: (test: unknown) => test is TypeAnyObject;
|
|
83
|
+
|
|
84
|
+
declare const isPlainObject: (test: unknown) => test is TypeAnyObject;
|
|
85
|
+
|
|
86
|
+
declare const isPromise: <T = unknown>(test: unknown) => test is Promise<T>;
|
|
87
|
+
|
|
88
|
+
declare const isRegExp: (test: unknown) => test is RegExp;
|
|
89
|
+
|
|
90
|
+
declare const isScalar: (test: unknown) => test is TypeScalar;
|
|
91
|
+
|
|
92
|
+
declare const isSet: <T = any>(test: unknown) => test is Set<T>;
|
|
93
|
+
|
|
94
|
+
declare const isString: (test: unknown) => test is string;
|
|
95
|
+
|
|
96
|
+
declare const isSymbol: (test: unknown) => test is symbol;
|
|
97
|
+
|
|
98
|
+
declare const isUndefined: (test: unknown) => test is undefined;
|
|
99
|
+
|
|
100
|
+
type TypeName = "array" | "asyncfunction" | "bigint" | "boolean" | "date" | "error" | "function" | "map" | "null" | "number" | "object" | "promise" | "regexp" | "set" | "string" | "symbol" | "weakmap" | "weakset";
|
|
101
|
+
declare const type: (test: unknown) => TypeName;
|
|
102
|
+
|
|
103
|
+
export { isArray, isAsyncFunction, isBigInt, isBoolean, isConstructor, isDate, isDef, isError, isFiniteNumber, isFloat, isFunction, isInteger, isMap, isNil, isNonEmptyArray, isNull, isNumber, isNumberLike, isObject, isPlainObject, isPromise, isRegExp, isScalar, isSet, isString, isSymbol, isUndefined, type };
|
|
104
|
+
export type { TypeAnyArray, TypeAnyAsyncFunction, TypeAnyConstructor, TypeAnyFunction, TypeAnyObject, TypeArray, TypeBooleanObject, TypeCallback, TypeCallbackArray, TypeCallbackMap, TypeCallbackObject, TypeFunctionArray, TypeFunctionObject, TypeKey, TypeName, TypeNullable, TypeNumberArray, TypeNumberObject, TypeObject, TypeObjectArray, TypeObjectKeys, TypeObjectKeysFunction, TypeObjectOptional, TypeObjectRequired, TypeOptional, TypeScalar, TypeStringArray, TypeStringObject };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @klyper/types v0.3.0
|
|
3
|
+
* Klyper types package.
|
|
4
|
+
* (c) 2026 Andrew Caires
|
|
5
|
+
* @license: MIT
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const typeNames = {
|
|
9
|
+
"[object Array]": "array",
|
|
10
|
+
"[object AsyncFunction]": "asyncfunction",
|
|
11
|
+
"[object BigInt]": "bigint",
|
|
12
|
+
"[object Boolean]": "boolean",
|
|
13
|
+
"[object Date]": "date",
|
|
14
|
+
"[object Error]": "error",
|
|
15
|
+
"[object Function]": "function",
|
|
16
|
+
"[object Map]": "map",
|
|
17
|
+
"[object Number]": "number",
|
|
18
|
+
"[object Object]": "object",
|
|
19
|
+
"[object Promise]": "promise",
|
|
20
|
+
"[object RegExp]": "regexp",
|
|
21
|
+
"[object Set]": "set",
|
|
22
|
+
"[object String]": "string",
|
|
23
|
+
"[object Symbol]": "symbol",
|
|
24
|
+
"[object WeakMap]": "weakmap",
|
|
25
|
+
"[object WeakSet]": "weakset",
|
|
26
|
+
};
|
|
27
|
+
const type = (test) => {
|
|
28
|
+
if (test == null) {
|
|
29
|
+
return "null";
|
|
30
|
+
}
|
|
31
|
+
return typeNames[Object.prototype.toString.call(test)] ?? "object";
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const isArray = (test) => type(test) == "array";
|
|
35
|
+
|
|
36
|
+
const isAsyncFunction = (test) => type(test) == "asyncfunction";
|
|
37
|
+
|
|
38
|
+
const isBigInt = (test) => typeof test == "bigint";
|
|
39
|
+
|
|
40
|
+
const isBoolean = (test) => typeof test == "boolean";
|
|
41
|
+
|
|
42
|
+
const isFunction = (test) => typeof test == "function";
|
|
43
|
+
|
|
44
|
+
const isConstructor = (test) => isFunction(test) && !!test.prototype && test.prototype.constructor === test;
|
|
45
|
+
|
|
46
|
+
const isDate = (test) => type(test) == "date";
|
|
47
|
+
|
|
48
|
+
const isNil = (test) => test === null || test === undefined;
|
|
49
|
+
|
|
50
|
+
const isDef = (test) => !isNil(test);
|
|
51
|
+
|
|
52
|
+
const isError = (test) => type(test) == "error";
|
|
53
|
+
|
|
54
|
+
const isFiniteNumber = (test) => Number.isFinite(test);
|
|
55
|
+
|
|
56
|
+
const isFloat = (test) => isFiniteNumber(test) && !Number.isInteger(test);
|
|
57
|
+
|
|
58
|
+
const isInteger = (test) => Number.isInteger(test);
|
|
59
|
+
|
|
60
|
+
const isMap = (test) => type(test) == "map";
|
|
61
|
+
|
|
62
|
+
const isNonEmptyArray = (test) => isArray(test) && test.length > 0;
|
|
63
|
+
|
|
64
|
+
const isNull = (test) => test === null;
|
|
65
|
+
|
|
66
|
+
const isNumber = (test) => typeof test == "number";
|
|
67
|
+
|
|
68
|
+
const NUMBER_PATTERN = /^-?[0-9]+(\.[0-9]+)?$/;
|
|
69
|
+
const isNumberLike = (value) => NUMBER_PATTERN.test(value);
|
|
70
|
+
|
|
71
|
+
const isObject = (test) => type(test) == "object";
|
|
72
|
+
|
|
73
|
+
const isPlainObject = (test) => {
|
|
74
|
+
if (!isObject(test) || type(test) != "object") {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const prototype = Object.getPrototypeOf(test);
|
|
78
|
+
return prototype === null || prototype === Object.prototype;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const isPromise = (test) => type(test) == "promise";
|
|
82
|
+
|
|
83
|
+
const isRegExp = (test) => type(test) == "regexp";
|
|
84
|
+
|
|
85
|
+
const isString = (test) => typeof test == "string";
|
|
86
|
+
|
|
87
|
+
const isScalar = (test) => isBoolean(test) || isNumber(test) || isString(test);
|
|
88
|
+
|
|
89
|
+
const isSet = (test) => type(test) == "set";
|
|
90
|
+
|
|
91
|
+
const isSymbol = (test) => typeof test == "symbol";
|
|
92
|
+
|
|
93
|
+
const isUndefined = (test) => test === undefined;
|
|
94
|
+
|
|
95
|
+
export { isArray, isAsyncFunction, isBigInt, isBoolean, isConstructor, isDate, isDef, isError, isFiniteNumber, isFloat, isFunction, isInteger, isMap, isNil, isNonEmptyArray, isNull, isNumber, isNumberLike, isObject, isPlainObject, isPromise, isRegExp, isScalar, isSet, isString, isSymbol, isUndefined, type };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@klyper/types",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Klyper types package.",
|
|
5
|
+
"author": "Andrew Caires",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/andrewcaires/klyper.git"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.cjs",
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/index.cjs",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "npm run clean && npm run check && npm run bundle",
|
|
29
|
+
"bundle": "rollup --bundleConfigAsCjs -c ../../rollup.config.js",
|
|
30
|
+
"check": "npm run lint && npm run typecheck",
|
|
31
|
+
"clean": "rm -rf dist",
|
|
32
|
+
"lint": "eslint .",
|
|
33
|
+
"lint:fix": "eslint . --fix",
|
|
34
|
+
"prepublishOnly": "npm run build && npm test",
|
|
35
|
+
"test": "node --import tsx --test \"src/**/*.test.ts\"",
|
|
36
|
+
"test:watch": "node --import tsx --test --watch \"src/**/*.test.ts\"",
|
|
37
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|