@orkestrel/contract 0.0.1
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/LICENSE +21 -0
- package/README.md +46 -0
- package/dist/src/core/combinators.d.ts +384 -0
- package/dist/src/core/compilers.d.ts +119 -0
- package/dist/src/core/constants.d.ts +20 -0
- package/dist/src/core/helpers.d.ts +112 -0
- package/dist/src/core/index.d.ts +8 -0
- package/dist/src/core/index.js +2026 -0
- package/dist/src/core/index.js.map +1 -0
- package/dist/src/core/parsers.d.ts +230 -0
- package/dist/src/core/shapers.d.ts +178 -0
- package/dist/src/core/types.d.ts +372 -0
- package/dist/src/core/validators.d.ts +250 -0
- package/package.json +69 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import type { AnyAsyncFunction, AnyConstructor, AnyFunction, JSONPrimitive, JSONValue, ZeroArgAsyncFunction, ZeroArgFunction } from './types.js';
|
|
2
|
+
/** Determine whether a value is `null`. */
|
|
3
|
+
export declare function isNull(value: unknown): value is null;
|
|
4
|
+
/** Determine whether a value is `undefined`. */
|
|
5
|
+
export declare function isUndefined(value: unknown): value is undefined;
|
|
6
|
+
/** Determine whether a value is defined (neither `null` nor `undefined`). */
|
|
7
|
+
export declare function isDefined<T>(value: T | null | undefined): value is T;
|
|
8
|
+
/** Determine whether a value is a string. */
|
|
9
|
+
export declare function isString(value: unknown): value is string;
|
|
10
|
+
/**
|
|
11
|
+
* Determine whether a value is a number.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* Includes `NaN` and `±Infinity` — use {@link isFiniteNumber} to exclude them.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isNumber(value: unknown): value is number;
|
|
17
|
+
/** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`). */
|
|
18
|
+
export declare function isFiniteNumber(value: unknown): value is number;
|
|
19
|
+
/** Determine whether a value is a finite integer (excludes `NaN`, `±Infinity`, and fractional numbers). */
|
|
20
|
+
export declare function isInteger(value: unknown): value is number;
|
|
21
|
+
/** Determine whether a value is a boolean. */
|
|
22
|
+
export declare function isBoolean(value: unknown): value is boolean;
|
|
23
|
+
/** Determine whether a value is exactly `true`. */
|
|
24
|
+
export declare function isTrue(value: unknown): value is true;
|
|
25
|
+
/** Determine whether a value is exactly `false`. */
|
|
26
|
+
export declare function isFalse(value: unknown): value is false;
|
|
27
|
+
/** Determine whether a value is a bigint. */
|
|
28
|
+
export declare function isBigInt(value: unknown): value is bigint;
|
|
29
|
+
/** Determine whether a value is a symbol. */
|
|
30
|
+
export declare function isSymbol(value: unknown): value is symbol;
|
|
31
|
+
/** Determine whether a value is callable. */
|
|
32
|
+
export declare function isFunction(value: unknown): value is AnyFunction;
|
|
33
|
+
/** Determine whether a value is a string or `null`. */
|
|
34
|
+
export declare function isNullableString(value: unknown): value is string | null;
|
|
35
|
+
/** Determine whether a value is a number or `null` (the number may be `NaN` / `±Infinity`). */
|
|
36
|
+
export declare function isNullableNumber(value: unknown): value is number | null;
|
|
37
|
+
/** Determine whether a value is a boolean or `null`. */
|
|
38
|
+
export declare function isNullableBoolean(value: unknown): value is boolean | null;
|
|
39
|
+
/** Determine whether a value is a `Date`. */
|
|
40
|
+
export declare function isDate(value: unknown): value is Date;
|
|
41
|
+
/** Determine whether a value is a `RegExp`. */
|
|
42
|
+
export declare function isRegExp(value: unknown): value is RegExp;
|
|
43
|
+
/** Determine whether a value is an `Error`. */
|
|
44
|
+
export declare function isError(value: unknown): value is Error;
|
|
45
|
+
/** Determine whether a value is a native `Promise` (use {@link isPromiseLike} for any thenable). */
|
|
46
|
+
export declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
47
|
+
/**
|
|
48
|
+
* Determine whether a value is promise-like — an object exposing callable
|
|
49
|
+
* `then`, `catch`, and `finally` methods.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* Accepts any object with all three methods, not only native `Promise`
|
|
53
|
+
* instances. Use {@link isPromise} when you specifically need `instanceof Promise`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function isPromiseLike<T = unknown>(value: unknown): value is Promise<T> | (PromiseLike<T> & {
|
|
56
|
+
catch: unknown;
|
|
57
|
+
finally: unknown;
|
|
58
|
+
});
|
|
59
|
+
/** Determine whether a value is an `ArrayBuffer`. */
|
|
60
|
+
export declare function isArrayBuffer(value: unknown): value is ArrayBuffer;
|
|
61
|
+
/**
|
|
62
|
+
* Determine whether a value is a `SharedArrayBuffer`.
|
|
63
|
+
*
|
|
64
|
+
* @remarks
|
|
65
|
+
* Guards the global existence of `SharedArrayBuffer` first — safe where it is
|
|
66
|
+
* absent or disabled (e.g. a context that is not cross-origin isolated).
|
|
67
|
+
*/
|
|
68
|
+
export declare function isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer;
|
|
69
|
+
/**
|
|
70
|
+
* Determine whether a value implements the iterable protocol (`Symbol.iterator`).
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* Strings are explicitly included: a string has a callable `Symbol.iterator`
|
|
74
|
+
* but is not an object, so the generic object path alone would miss it.
|
|
75
|
+
*/
|
|
76
|
+
export declare function isIterable<T = unknown>(value: unknown): value is Iterable<T>;
|
|
77
|
+
/** Determine whether a value implements the async iterable protocol (`Symbol.asyncIterator`). */
|
|
78
|
+
export declare function isAsyncIterable<T = unknown>(value: unknown): value is AsyncIterable<T>;
|
|
79
|
+
/**
|
|
80
|
+
* Determine whether a value is a non-null object.
|
|
81
|
+
*
|
|
82
|
+
* @remarks
|
|
83
|
+
* `true` for arrays, class instances, plain objects, `Map`, `Set`, etc. — use
|
|
84
|
+
* {@link isRecord} when you need a plain-record check.
|
|
85
|
+
*/
|
|
86
|
+
export declare function isObject(value: unknown): value is object;
|
|
87
|
+
/**
|
|
88
|
+
* Determine whether a value is a plain record (object literal or null-prototype),
|
|
89
|
+
* not an array or class instance.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* Use instead of {@link isObject} to distinguish a plain `{}` /
|
|
93
|
+
* `Object.create(null)` from arrays, `Date`, `Map`, etc. The prototype-chain
|
|
94
|
+
* test is realm-agnostic: rather than comparing against the current realm's
|
|
95
|
+
* `Object.prototype` (which a plain object from another `vm.Context`, iframe,
|
|
96
|
+
* or worker would fail), it accepts any value whose prototype is `null`, OR
|
|
97
|
+
* whose prototype's own prototype is `null` — the shape every plain object
|
|
98
|
+
* has in every realm, since `Object.prototype` itself always sits one step
|
|
99
|
+
* above `null`. Arrays and class instances are still rejected: an array's
|
|
100
|
+
* prototype chain runs through `Array.prototype` before `null`, and a class
|
|
101
|
+
* instance's runs through the class's own prototype. The whole body runs
|
|
102
|
+
* inside `attempt` (AGENTS §14) so a revoked `Proxy` or a hostile
|
|
103
|
+
* `getPrototypeOf` trap cannot escape as a thrown error.
|
|
104
|
+
*/
|
|
105
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
106
|
+
/** Determine whether a value is a `Map`. */
|
|
107
|
+
export declare function isMap<K = unknown, V = unknown>(value: unknown): value is ReadonlyMap<K, V>;
|
|
108
|
+
/** Determine whether a value is a `Set`. */
|
|
109
|
+
export declare function isSet<T = unknown>(value: unknown): value is ReadonlySet<T>;
|
|
110
|
+
/** Determine whether a value is a `WeakMap`. */
|
|
111
|
+
export declare function isWeakMap(value: unknown): value is WeakMap<object, unknown>;
|
|
112
|
+
/** Determine whether a value is a `WeakSet`. */
|
|
113
|
+
export declare function isWeakSet(value: unknown): value is WeakSet<object>;
|
|
114
|
+
/** Determine whether a value is an array. */
|
|
115
|
+
export declare function isArray<T = unknown>(value: unknown): value is readonly T[];
|
|
116
|
+
/** Determine whether a value is a `DataView`. */
|
|
117
|
+
export declare function isDataView(value: unknown): value is DataView<ArrayBufferLike>;
|
|
118
|
+
/** Determine whether a value is an `ArrayBufferView` (any typed array or `DataView`). */
|
|
119
|
+
export declare function isArrayBufferView(value: unknown): value is ArrayBufferView;
|
|
120
|
+
/** Determine whether a value is an `Int8Array`. */
|
|
121
|
+
export declare function isInt8Array(value: unknown): value is Int8Array;
|
|
122
|
+
/** Determine whether a value is a `Uint8Array`. */
|
|
123
|
+
export declare function isUint8Array(value: unknown): value is Uint8Array;
|
|
124
|
+
/** Determine whether a value is a `Uint8ClampedArray`. */
|
|
125
|
+
export declare function isUint8ClampedArray(value: unknown): value is Uint8ClampedArray;
|
|
126
|
+
/** Determine whether a value is an `Int16Array`. */
|
|
127
|
+
export declare function isInt16Array(value: unknown): value is Int16Array;
|
|
128
|
+
/** Determine whether a value is a `Uint16Array`. */
|
|
129
|
+
export declare function isUint16Array(value: unknown): value is Uint16Array;
|
|
130
|
+
/** Determine whether a value is an `Int32Array`. */
|
|
131
|
+
export declare function isInt32Array(value: unknown): value is Int32Array;
|
|
132
|
+
/** Determine whether a value is a `Uint32Array`. */
|
|
133
|
+
export declare function isUint32Array(value: unknown): value is Uint32Array;
|
|
134
|
+
/** Determine whether a value is a `Float32Array`. */
|
|
135
|
+
export declare function isFloat32Array(value: unknown): value is Float32Array;
|
|
136
|
+
/** Determine whether a value is a `Float64Array`. */
|
|
137
|
+
export declare function isFloat64Array(value: unknown): value is Float64Array;
|
|
138
|
+
/**
|
|
139
|
+
* Determine whether a value is a `BigInt64Array`.
|
|
140
|
+
*
|
|
141
|
+
* @remarks
|
|
142
|
+
* Guards the global existence of `BigInt64Array` first — safe in environments
|
|
143
|
+
* that pre-date the BigInt typed-array additions.
|
|
144
|
+
*/
|
|
145
|
+
export declare function isBigInt64Array(value: unknown): value is BigInt64Array;
|
|
146
|
+
/**
|
|
147
|
+
* Determine whether a value is a `BigUint64Array`.
|
|
148
|
+
*
|
|
149
|
+
* @remarks
|
|
150
|
+
* Guards the global existence of `BigUint64Array` first — safe in environments
|
|
151
|
+
* that pre-date the BigInt typed-array additions.
|
|
152
|
+
*/
|
|
153
|
+
export declare function isBigUint64Array(value: unknown): value is BigUint64Array;
|
|
154
|
+
/** Determine whether a value is the empty string `''`. */
|
|
155
|
+
export declare function isEmptyString(value: unknown): value is '';
|
|
156
|
+
/** Determine whether a value is an empty array. */
|
|
157
|
+
export declare function isEmptyArray(value: unknown): value is readonly [];
|
|
158
|
+
/** Determine whether a value is an empty plain object (no own string or enumerable symbol keys). */
|
|
159
|
+
export declare function isEmptyObject(value: unknown): value is Record<string | symbol, never>;
|
|
160
|
+
/** Determine whether a value is an empty `Map`. */
|
|
161
|
+
export declare function isEmptyMap(value: unknown): value is ReadonlyMap<never, never>;
|
|
162
|
+
/** Determine whether a value is an empty `Set`. */
|
|
163
|
+
export declare function isEmptySet(value: unknown): value is ReadonlySet<never>;
|
|
164
|
+
/** Determine whether a value is a non-empty string (at least one character). */
|
|
165
|
+
export declare function isNonEmptyString(value: unknown): value is string;
|
|
166
|
+
/** Determine whether a value is a non-empty array (at least one element). */
|
|
167
|
+
export declare function isNonEmptyArray<T = unknown>(value: unknown): value is readonly [T, ...T[]];
|
|
168
|
+
/** Determine whether a value is a non-empty plain object (at least one own string or enumerable symbol key). */
|
|
169
|
+
export declare function isNonEmptyObject(value: unknown): value is Record<string | symbol, unknown>;
|
|
170
|
+
/** Determine whether a value is a non-empty `Map` (at least one entry). */
|
|
171
|
+
export declare function isNonEmptyMap<K = unknown, V = unknown>(value: unknown): value is ReadonlyMap<K, V>;
|
|
172
|
+
/** Determine whether a value is a non-empty `Set` (at least one element). */
|
|
173
|
+
export declare function isNonEmptySet<T = unknown>(value: unknown): value is ReadonlySet<T>;
|
|
174
|
+
/** Determine whether a value is a function that declares zero parameters (`Function.length === 0`). */
|
|
175
|
+
export declare function isZeroArg(value: unknown): value is ZeroArgFunction;
|
|
176
|
+
/**
|
|
177
|
+
* Determine whether a value is a native `async function`.
|
|
178
|
+
*
|
|
179
|
+
* @remarks
|
|
180
|
+
* Uses `constructor.name === 'AsyncFunction'` — not `instanceof`, which is
|
|
181
|
+
* unreliable across realms. The `?.` keeps the guard total (§14): a function
|
|
182
|
+
* whose `constructor` was nulled yields `undefined`, never a thrown `null.name`.
|
|
183
|
+
*/
|
|
184
|
+
export declare function isAsyncFunction(value: unknown): value is AnyAsyncFunction;
|
|
185
|
+
/** Determine whether a value is a generator function (`function*`). */
|
|
186
|
+
export declare function isGeneratorFunction(value: unknown): value is (...args: unknown[]) => Generator<unknown, unknown, unknown>;
|
|
187
|
+
/** Determine whether a value is an async generator function (`async function*`). */
|
|
188
|
+
export declare function isAsyncGeneratorFunction(value: unknown): value is (...args: unknown[]) => AsyncGenerator<unknown, unknown, unknown>;
|
|
189
|
+
/** Determine whether a value is a zero-argument async function. */
|
|
190
|
+
export declare function isZeroArgAsync(value: unknown): value is ZeroArgAsyncFunction;
|
|
191
|
+
/** Determine whether a value is a zero-argument generator function. */
|
|
192
|
+
export declare function isZeroArgGenerator(value: unknown): value is () => Generator<unknown, unknown, unknown>;
|
|
193
|
+
/** Determine whether a value is a zero-argument async generator function. */
|
|
194
|
+
export declare function isZeroArgAsyncGenerator(value: unknown): value is () => AsyncGenerator<unknown, unknown, unknown>;
|
|
195
|
+
/**
|
|
196
|
+
* Determine whether a value can be used as a `new`-target constructor.
|
|
197
|
+
*
|
|
198
|
+
* @remarks
|
|
199
|
+
* Probes with `Reflect.construct(String, [], value)`: a real constructor
|
|
200
|
+
* succeeds, while arrow functions, plain functions, and non-functions throw
|
|
201
|
+
* and yield `false`. Never throws. Backs the `instanceOf` combinator.
|
|
202
|
+
*/
|
|
203
|
+
export declare function isConstructor(value: unknown): value is AnyConstructor<object>;
|
|
204
|
+
/**
|
|
205
|
+
* Determine whether a value is a cycle-safe JSON value.
|
|
206
|
+
*
|
|
207
|
+
* @remarks
|
|
208
|
+
* Total guard: never throws, returns `false` for cycles, functions, `Date`
|
|
209
|
+
* instances, class instances, `NaN`, and `±Infinity`. Arrays and plain records
|
|
210
|
+
* are walked with an ancestor set so recursive input fails instead of hanging.
|
|
211
|
+
* The whole walk runs inside `attempt` (AGENTS §14): a hostile getter on a
|
|
212
|
+
* record property, or a revoked `Proxy` anywhere in the structure, is caught
|
|
213
|
+
* and yields `false` instead of escaping as a thrown error.
|
|
214
|
+
*
|
|
215
|
+
* @param value - The value to test
|
|
216
|
+
* @returns `true` when the value has a JSON representation
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* isJSONValue({ nested: [1, 'x', null] }) // true
|
|
221
|
+
* isJSONValue(Number.NaN) // false
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
export declare function isJSONValue(value: unknown): value is JSONValue;
|
|
225
|
+
/**
|
|
226
|
+
* Determine whether a value is a primitive JSON value.
|
|
227
|
+
*
|
|
228
|
+
* @remarks
|
|
229
|
+
* The flat leaf of any JSON document: `null`, a string, a **finite** number, or
|
|
230
|
+
* a boolean. Uses {@link isFiniteNumber} (not {@link isNumber}) because real JSON
|
|
231
|
+
* carries no `NaN` / `±Infinity` — `JSON.stringify(NaN)` is `'null'`.
|
|
232
|
+
*
|
|
233
|
+
* The recursive {@link isJSONValue} guard is shipped and stays total with
|
|
234
|
+
* cycle-safe walking. Dedicated `isJSONObject` / `isJSONSchema` validators and
|
|
235
|
+
* the broad `JSONSchemaDefinition` remain omitted; compose narrower shapes with
|
|
236
|
+
* the combinators and gate untrusted strings with `parseJSON` / `parseJSONAs`.
|
|
237
|
+
*
|
|
238
|
+
* @param value - The value to test
|
|
239
|
+
* @returns `true` when `value` is `null`, a string, a finite number, or a boolean
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* isJSONPrimitive(null) // true
|
|
244
|
+
* isJSONPrimitive('hi') // true
|
|
245
|
+
* isJSONPrimitive(42) // true
|
|
246
|
+
* isJSONPrimitive(Number.NaN) // false — not representable in JSON
|
|
247
|
+
* isJSONPrimitive({}) // false
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
export declare function isJSONPrimitive(value: unknown): value is JSONPrimitive;
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@orkestrel/contract",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Zero-dependency contract toolkit — type guards, combinators, parsers, and a shape DSL that compiles one contract shape into a guard, parser, JSON Schema, and generator. The foundation package of the @orkestrel line.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"contracts",
|
|
7
|
+
"json-schema",
|
|
8
|
+
"parsers",
|
|
9
|
+
"type-guards",
|
|
10
|
+
"typescript",
|
|
11
|
+
"validation",
|
|
12
|
+
"zero-dependency"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/orkestrel/contract#readme",
|
|
15
|
+
"bugs": "https://github.com/orkestrel/contract/issues",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/orkestrel/contract.git"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"main": "./dist/src/core/index.js",
|
|
28
|
+
"types": "./dist/src/core/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/src/core/index.d.ts",
|
|
32
|
+
"import": "./dist/src/core/index.js",
|
|
33
|
+
"default": "./dist/src/core/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"clean": "node -e \"try{require('node:fs').rmSync('dist',{recursive:true,force:true})}catch{}\"",
|
|
42
|
+
"copy": "node -e \"const fs=require('node:fs'),p=require('node:path'),a=process.argv[1],b=process.argv[2];fs.mkdirSync(p.dirname(b),{recursive:true});fs.cpSync(a,b,{force:true});console.log('Copied: '+a+' to '+b)\"",
|
|
43
|
+
"tmp:txt": "node -e \"const fs=require('node:fs'),p=require('node:path');function walk(d){for(const e of fs.readdirSync(d,{withFileTypes:true})){const f=p.join(d,e.name);if(e.isDirectory()){walk(f)}else if(!e.name.endsWith('.md')&&!e.name.endsWith('.txt')){const t=f+'.txt';if(!fs.existsSync(t)){fs.renameSync(f,t)}else{console.warn('Skipping '+f+' — target exists: '+t)}}}}try{walk('tmp')}catch(e){if(e.code!=='ENOENT')throw e}\"",
|
|
44
|
+
"lint": "oxlint --config .oxlintrc.json --fix .",
|
|
45
|
+
"check": "tsc --noEmit --project tsconfig.json",
|
|
46
|
+
"check:src": "npm run check:src:core",
|
|
47
|
+
"check:src:core": "tsc --noEmit -p configs/src/tsconfig.core.json",
|
|
48
|
+
"format": "oxfmt --config .oxfmtrc.json --write .",
|
|
49
|
+
"format:check": "oxfmt --config .oxfmtrc.json --check .",
|
|
50
|
+
"lint:check": "oxlint --config .oxlintrc.json .",
|
|
51
|
+
"test": "npm run test:src",
|
|
52
|
+
"test:src": "vitest run --config vite.config.ts --reporter=dot --project src:core",
|
|
53
|
+
"build": "npm run clean && npm run build:src",
|
|
54
|
+
"build:src": "npm run build:src:core",
|
|
55
|
+
"build:src:core": "vite build --config configs/src/vite.core.config.ts && tsc -p configs/src/tsconfig.core.json",
|
|
56
|
+
"prepublishOnly": "npm run format:check && npm run lint:check && npm run check && npm run check:src && npm run build && npm test"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/node": "^26.1.1",
|
|
60
|
+
"oxfmt": "^0.58.0",
|
|
61
|
+
"oxlint": "^1.73.0",
|
|
62
|
+
"typescript": "^6.0.3",
|
|
63
|
+
"vite": "^8.1.4",
|
|
64
|
+
"vitest": "^4.1.10"
|
|
65
|
+
},
|
|
66
|
+
"engines": {
|
|
67
|
+
"node": ">=24"
|
|
68
|
+
}
|
|
69
|
+
}
|