@xylabs/typeof 4.4.18 → 4.4.19
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/dist/neutral/Typed.d.ts +12 -0
- package/dist/neutral/Typed.d.ts.map +1 -0
- package/dist/neutral/index.d.ts +1 -0
- package/dist/neutral/index.d.ts.map +1 -1
- package/dist/neutral/index.mjs +42 -0
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Typed.ts +66 -0
- package/src/index.ts +1 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined;
|
|
2
|
+
export type TypedKey<T extends string | void = void> = T extends string ? T : string | number | symbol;
|
|
3
|
+
export type TypedObject = {
|
|
4
|
+
[key: TypedKey]: TypedValue;
|
|
5
|
+
} | object;
|
|
6
|
+
export type TypedArray = TypedValue[];
|
|
7
|
+
export declare const isTypedKey: (value: unknown) => value is TypedKey;
|
|
8
|
+
export declare const isTypedValue: (value: unknown) => value is TypedValue;
|
|
9
|
+
export declare const isTypedArray: (value: unknown) => value is TypedArray;
|
|
10
|
+
export declare const isValidTypedFieldPair: (pair: [key: unknown, value: unknown]) => pair is [key: TypedKey, value: TypedValue];
|
|
11
|
+
export declare const isTypedObject: (value: unknown) => value is TypedObject;
|
|
12
|
+
//# sourceMappingURL=Typed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Typed.d.ts","sourceRoot":"","sources":["../../src/Typed.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,WAAW,GAAG,UAAU,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAA;AAC7H,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,IAAI,IAAI,CAAC,SAAS,MAAM,GAAG,CAAC,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;AACtG,MAAM,MAAM,WAAW,GAAG;IAAE,CAAC,GAAG,EAAE,QAAQ,GAAG,UAAU,CAAA;CAAE,GAAG,MAAM,CAAA;AAClE,MAAM,MAAM,UAAU,GAAG,UAAU,EAAE,CAAA;AAErC,eAAO,MAAM,UAAU,UAAW,OAAO,KAAG,KAAK,IAAI,QAYpD,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,KAAG,KAAK,IAAI,UAWtD,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,KAAG,KAAK,IAAI,UAEtD,CAAA;AAED,eAAO,MAAM,qBAAqB,SAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,KAAG,IAAI,IAAI,CAAC,GAAG,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,CAGrH,CAAA;AAED,eAAO,MAAM,aAAa,UAAW,OAAO,KAAG,KAAK,IAAI,WAMvD,CAAA"}
|
package/dist/neutral/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,sBAAsB,CAAA;AACpC,cAAc,aAAa,CAAA;AAC3B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,sBAAsB,CAAA;AACpC,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -51,6 +51,43 @@ var isType = (value, expectedType) => {
|
|
|
51
51
|
}
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
+
// src/Typed.ts
|
|
55
|
+
var isTypedKey = (value) => {
|
|
56
|
+
switch (typeof value) {
|
|
57
|
+
case "string":
|
|
58
|
+
case "bigint":
|
|
59
|
+
case "number":
|
|
60
|
+
case "symbol": {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
default: {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var isTypedValue = (value) => {
|
|
69
|
+
switch (typeof value) {
|
|
70
|
+
case "string":
|
|
71
|
+
case "number":
|
|
72
|
+
case "boolean": {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
default: {
|
|
76
|
+
return value === null || isTypedObject(value) || isTypedArray(value);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var isTypedArray = (value) => {
|
|
81
|
+
return Array.isArray(value) && !value.some((item) => !isTypedValue(item));
|
|
82
|
+
};
|
|
83
|
+
var isValidTypedFieldPair = (pair) => {
|
|
84
|
+
const [key, value] = pair;
|
|
85
|
+
return isTypedKey(key) && isTypedValue(value);
|
|
86
|
+
};
|
|
87
|
+
var isTypedObject = (value) => {
|
|
88
|
+
return isType(value, "object") && !Object.entries(value).some((item) => !isValidTypedFieldPair(item));
|
|
89
|
+
};
|
|
90
|
+
|
|
54
91
|
// src/validateType.ts
|
|
55
92
|
var validateType = (typeName, value, optional = false) => {
|
|
56
93
|
switch (typeOf(value)) {
|
|
@@ -69,6 +106,11 @@ export {
|
|
|
69
106
|
ifDefined,
|
|
70
107
|
ifTypeOf,
|
|
71
108
|
isType,
|
|
109
|
+
isTypedArray,
|
|
110
|
+
isTypedKey,
|
|
111
|
+
isTypedObject,
|
|
112
|
+
isTypedValue,
|
|
113
|
+
isValidTypedFieldPair,
|
|
72
114
|
typeOf,
|
|
73
115
|
validateType
|
|
74
116
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/typeOf.ts","../../src/ifDefined.ts","../../src/ifTypeOf.ts","../../src/isType.ts","../../src/validateType.ts"],"sourcesContent":["import type { TypeOfTypes } from './TypeOfTypes.ts'\n\nexport const typeOf = <T>(item: T): TypeOfTypes => {\n return Array.isArray(item) ? 'array' : typeof item\n}\n","import { typeOf } from './typeOf.ts'\n\nexport const ifDefined = <T>(value: T, func: (value: T) => void) => {\n switch (typeOf(value)) {\n case 'undefined':\n case 'null': {\n break\n }\n default: {\n func(value)\n return value\n }\n }\n}\n","import { typeOf } from './typeOf.ts'\nimport type { TypeOfTypes } from './TypeOfTypes.ts'\n\nexport const ifTypeOf = <T, R>(typeName: TypeOfTypes, value: unknown, trueFunc: (value: T) => R, isFunc?: (value: T) => boolean) => {\n switch (typeOf(value)) {\n case typeName: {\n return !isFunc || isFunc(value as T) ? trueFunc(value as T) : undefined\n }\n }\n}\n","import type { FieldType } from './ObjectTypeShape.ts'\n\nexport const isType = (value: unknown, expectedType: FieldType) => {\n const typeofValue = typeof value\n switch (expectedType) {\n case 'array': {\n return Array.isArray(value)\n }\n case 'null': {\n return value === null\n }\n case 'undefined': {\n return value === undefined\n }\n case 'object': {\n // nulls resolve to objects, so exclude them\n if (value === null) {\n return false\n }\n // arrays resolve to objects, so exclude them\n return typeofValue === 'object' && !Array.isArray(value)\n }\n default: {\n return typeofValue === expectedType\n }\n }\n}\n","import { typeOf } from './typeOf.ts'\nimport type { TypeOfTypes } from './TypeOfTypes.ts'\n\nexport const validateType = <T>(typeName: TypeOfTypes, value: T, optional = false): [T | undefined, Error[]] => {\n switch (typeOf(value)) {\n case typeName: {\n return [value, []]\n }\n default: {\n if (optional && typeOf(value) === 'undefined') {\n return [value, []]\n }\n return [undefined, [new Error(`value type is not '${typeName}:${typeof value}'`)]]\n }\n }\n}\n"],"mappings":";AAEO,IAAM,SAAS,CAAI,SAAyB;AACjD,SAAO,MAAM,QAAQ,IAAI,IAAI,UAAU,OAAO;AAChD;;;ACFO,IAAM,YAAY,CAAI,OAAU,SAA6B;AAClE,UAAQ,OAAO,KAAK,GAAG;AAAA,IACrB,KAAK;AAAA,IACL,KAAK,QAAQ;AACX;AAAA,IACF;AAAA,IACA,SAAS;AACP,WAAK,KAAK;AACV,aAAO;AAAA,IACT;AAAA,EACF;AACF;;;ACVO,IAAM,WAAW,CAAO,UAAuB,OAAgB,UAA2B,WAAmC;AAClI,UAAQ,OAAO,KAAK,GAAG;AAAA,IACrB,KAAK,UAAU;AACb,aAAO,CAAC,UAAU,OAAO,KAAU,IAAI,SAAS,KAAU,IAAI;AAAA,IAChE;AAAA,EACF;AACF;;;ACPO,IAAM,SAAS,CAAC,OAAgB,iBAA4B;AACjE,QAAM,cAAc,OAAO;AAC3B,UAAQ,cAAc;AAAA,IACpB,KAAK,SAAS;AACZ,aAAO,MAAM,QAAQ,KAAK;AAAA,IAC5B;AAAA,IACA,KAAK,QAAQ;AACX,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,aAAa;AAChB,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,UAAU;AAEb,UAAI,UAAU,MAAM;AAClB,eAAO;AAAA,MACT;AAEA,aAAO,gBAAgB,YAAY,CAAC,MAAM,QAAQ,KAAK;AAAA,IACzD;AAAA,IACA,SAAS;AACP,aAAO,gBAAgB;AAAA,IACzB;AAAA,EACF;AACF;;;
|
|
1
|
+
{"version":3,"sources":["../../src/typeOf.ts","../../src/ifDefined.ts","../../src/ifTypeOf.ts","../../src/isType.ts","../../src/Typed.ts","../../src/validateType.ts"],"sourcesContent":["import type { TypeOfTypes } from './TypeOfTypes.ts'\n\nexport const typeOf = <T>(item: T): TypeOfTypes => {\n return Array.isArray(item) ? 'array' : typeof item\n}\n","import { typeOf } from './typeOf.ts'\n\nexport const ifDefined = <T>(value: T, func: (value: T) => void) => {\n switch (typeOf(value)) {\n case 'undefined':\n case 'null': {\n break\n }\n default: {\n func(value)\n return value\n }\n }\n}\n","import { typeOf } from './typeOf.ts'\nimport type { TypeOfTypes } from './TypeOfTypes.ts'\n\nexport const ifTypeOf = <T, R>(typeName: TypeOfTypes, value: unknown, trueFunc: (value: T) => R, isFunc?: (value: T) => boolean) => {\n switch (typeOf(value)) {\n case typeName: {\n return !isFunc || isFunc(value as T) ? trueFunc(value as T) : undefined\n }\n }\n}\n","import type { FieldType } from './ObjectTypeShape.ts'\n\nexport const isType = (value: unknown, expectedType: FieldType) => {\n const typeofValue = typeof value\n switch (expectedType) {\n case 'array': {\n return Array.isArray(value)\n }\n case 'null': {\n return value === null\n }\n case 'undefined': {\n return value === undefined\n }\n case 'object': {\n // nulls resolve to objects, so exclude them\n if (value === null) {\n return false\n }\n // arrays resolve to objects, so exclude them\n return typeofValue === 'object' && !Array.isArray(value)\n }\n default: {\n return typeofValue === expectedType\n }\n }\n}\n","import { isType } from './isType.ts'\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\nexport type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined\nexport type TypedKey<T extends string | void = void> = T extends string ? T : string | number | symbol\nexport type TypedObject = { [key: TypedKey]: TypedValue } | object\nexport type TypedArray = TypedValue[]\n\nexport const isTypedKey = (value: unknown): value is TypedKey => {\n switch (typeof value) {\n case 'string':\n case 'bigint':\n case 'number':\n case 'symbol': {\n return true\n }\n default: {\n return false\n }\n }\n}\n\nexport const isTypedValue = (value: unknown): value is TypedValue => {\n switch (typeof value) {\n case 'string':\n case 'number':\n case 'boolean': {\n return true\n }\n default: {\n return value === null || isTypedObject(value) || isTypedArray(value)\n }\n }\n}\n\nexport const isTypedArray = (value: unknown): value is TypedArray => {\n return Array.isArray(value) && !value.some(item => !isTypedValue(item))\n}\n\nexport const isValidTypedFieldPair = (pair: [key: unknown, value: unknown]): pair is [key: TypedKey, value: TypedValue] => {\n const [key, value] = pair\n return isTypedKey(key) && isTypedValue(value)\n}\n\nexport const isTypedObject = (value: unknown): value is TypedObject => {\n return (\n isType(value, 'object')\n // check if all keys are strings\n && !Object.entries(value as object).some(item => !isValidTypedFieldPair(item))\n )\n}\n\n// Object Type Test\n/*\ninterface TestObject {\n value: number\n}\n\nconst x: TestObject = { value: 1 }\n\nconst f = (p: TypedValue): void => {\n console.log(p)\n}\n\nf(x)\n*/\n","import { typeOf } from './typeOf.ts'\nimport type { TypeOfTypes } from './TypeOfTypes.ts'\n\nexport const validateType = <T>(typeName: TypeOfTypes, value: T, optional = false): [T | undefined, Error[]] => {\n switch (typeOf(value)) {\n case typeName: {\n return [value, []]\n }\n default: {\n if (optional && typeOf(value) === 'undefined') {\n return [value, []]\n }\n return [undefined, [new Error(`value type is not '${typeName}:${typeof value}'`)]]\n }\n }\n}\n"],"mappings":";AAEO,IAAM,SAAS,CAAI,SAAyB;AACjD,SAAO,MAAM,QAAQ,IAAI,IAAI,UAAU,OAAO;AAChD;;;ACFO,IAAM,YAAY,CAAI,OAAU,SAA6B;AAClE,UAAQ,OAAO,KAAK,GAAG;AAAA,IACrB,KAAK;AAAA,IACL,KAAK,QAAQ;AACX;AAAA,IACF;AAAA,IACA,SAAS;AACP,WAAK,KAAK;AACV,aAAO;AAAA,IACT;AAAA,EACF;AACF;;;ACVO,IAAM,WAAW,CAAO,UAAuB,OAAgB,UAA2B,WAAmC;AAClI,UAAQ,OAAO,KAAK,GAAG;AAAA,IACrB,KAAK,UAAU;AACb,aAAO,CAAC,UAAU,OAAO,KAAU,IAAI,SAAS,KAAU,IAAI;AAAA,IAChE;AAAA,EACF;AACF;;;ACPO,IAAM,SAAS,CAAC,OAAgB,iBAA4B;AACjE,QAAM,cAAc,OAAO;AAC3B,UAAQ,cAAc;AAAA,IACpB,KAAK,SAAS;AACZ,aAAO,MAAM,QAAQ,KAAK;AAAA,IAC5B;AAAA,IACA,KAAK,QAAQ;AACX,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAA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AAA,IACA,SAAS;AACP,UAAI,YAAY,OAAO,KAAK,MAAM,aAAa;AAC7C,eAAO,CAAC,OAAO,CAAC,CAAC;AAAA,MACnB;AACA,aAAO,CAAC,QAAW,CAAC,IAAI,MAAM,sBAAsB,QAAQ,IAAI,OAAO,KAAK,GAAG,CAAC,CAAC;AAAA,IACnF;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
package/src/Typed.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isType } from './isType.ts'
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
4
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined
|
|
5
|
+
export type TypedKey<T extends string | void = void> = T extends string ? T : string | number | symbol
|
|
6
|
+
export type TypedObject = { [key: TypedKey]: TypedValue } | object
|
|
7
|
+
export type TypedArray = TypedValue[]
|
|
8
|
+
|
|
9
|
+
export const isTypedKey = (value: unknown): value is TypedKey => {
|
|
10
|
+
switch (typeof value) {
|
|
11
|
+
case 'string':
|
|
12
|
+
case 'bigint':
|
|
13
|
+
case 'number':
|
|
14
|
+
case 'symbol': {
|
|
15
|
+
return true
|
|
16
|
+
}
|
|
17
|
+
default: {
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const isTypedValue = (value: unknown): value is TypedValue => {
|
|
24
|
+
switch (typeof value) {
|
|
25
|
+
case 'string':
|
|
26
|
+
case 'number':
|
|
27
|
+
case 'boolean': {
|
|
28
|
+
return true
|
|
29
|
+
}
|
|
30
|
+
default: {
|
|
31
|
+
return value === null || isTypedObject(value) || isTypedArray(value)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const isTypedArray = (value: unknown): value is TypedArray => {
|
|
37
|
+
return Array.isArray(value) && !value.some(item => !isTypedValue(item))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const isValidTypedFieldPair = (pair: [key: unknown, value: unknown]): pair is [key: TypedKey, value: TypedValue] => {
|
|
41
|
+
const [key, value] = pair
|
|
42
|
+
return isTypedKey(key) && isTypedValue(value)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const isTypedObject = (value: unknown): value is TypedObject => {
|
|
46
|
+
return (
|
|
47
|
+
isType(value, 'object')
|
|
48
|
+
// check if all keys are strings
|
|
49
|
+
&& !Object.entries(value as object).some(item => !isValidTypedFieldPair(item))
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Object Type Test
|
|
54
|
+
/*
|
|
55
|
+
interface TestObject {
|
|
56
|
+
value: number
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const x: TestObject = { value: 1 }
|
|
60
|
+
|
|
61
|
+
const f = (p: TypedValue): void => {
|
|
62
|
+
console.log(p)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
f(x)
|
|
66
|
+
*/
|
package/src/index.ts
CHANGED