@xylabs/typeof 4.3.4 → 4.3.6

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.
@@ -0,0 +1,3 @@
1
+ export type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function';
2
+ export type ObjectTypeShape = Record<string | number | symbol, FieldType>;
3
+ //# sourceMappingURL=ObjectTypeShape.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ObjectTypeShape.d.ts","sourceRoot":"","sources":["../../src/ObjectTypeShape.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,WAAW,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;AAE1H,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,SAAS,CAAC,CAAA"}
@@ -1,5 +1,7 @@
1
1
  export * from './ifDefined.ts';
2
2
  export * from './ifTypeOf.ts';
3
+ export * from './isType.ts';
4
+ export * from './ObjectTypeShape.ts';
3
5
  export * from './typeOf.ts';
4
6
  export * from './TypeOfTypes.ts';
5
7
  export * from './validateType.ts';
@@ -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,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,aAAa,CAAA;AAC3B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA"}
@@ -26,6 +26,31 @@ var ifTypeOf = (typeName, value, trueFunc, isFunc) => {
26
26
  }
27
27
  };
28
28
 
29
+ // src/isType.ts
30
+ var isType = (value, expectedType) => {
31
+ const typeofValue = typeof value;
32
+ switch (expectedType) {
33
+ case "array": {
34
+ return Array.isArray(value);
35
+ }
36
+ case "null": {
37
+ return value === null;
38
+ }
39
+ case "undefined": {
40
+ return value === void 0;
41
+ }
42
+ case "object": {
43
+ if (value === null) {
44
+ return false;
45
+ }
46
+ return typeofValue === "object" && !Array.isArray(value);
47
+ }
48
+ default: {
49
+ return typeofValue === expectedType;
50
+ }
51
+ }
52
+ };
53
+
29
54
  // src/validateType.ts
30
55
  var validateType = (typeName, value, optional = false) => {
31
56
  switch (typeOf(value)) {
@@ -43,6 +68,7 @@ var validateType = (typeName, value, optional = false) => {
43
68
  export {
44
69
  ifDefined,
45
70
  ifTypeOf,
71
+ isType,
46
72
  typeOf,
47
73
  validateType
48
74
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/typeOf.ts","../../src/ifDefined.ts","../../src/ifTypeOf.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 { 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;;;ACNO,IAAM,eAAe,CAAI,UAAuB,OAAU,WAAW,UAAoC;AAC9G,UAAQ,OAAO,KAAK,GAAG;AAAA,IACrB,KAAK,UAAU;AACb,aAAO,CAAC,OAAO,CAAC,CAAC;AAAA,IACnB;AAAA,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":[]}
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;;;ACvBO,IAAM,eAAe,CAAI,UAAuB,OAAU,WAAW,UAAoC;AAC9G,UAAQ,OAAO,KAAK,GAAG;AAAA,IACrB,KAAK,UAAU;AACb,aAAO,CAAC,OAAO,CAAC,CAAC;AAAA,IACnB;AAAA,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":[]}
@@ -0,0 +1,3 @@
1
+ import type { FieldType } from './ObjectTypeShape.ts';
2
+ export declare const isType: (value: unknown, expectedType: FieldType) => boolean;
3
+ //# sourceMappingURL=isType.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isType.d.ts","sourceRoot":"","sources":["../../src/isType.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AAErD,eAAO,MAAM,MAAM,UAAW,OAAO,gBAAgB,SAAS,YAwB7D,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/typeof",
3
- "version": "4.3.4",
3
+ "version": "4.3.6",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "typeof",
@@ -36,8 +36,8 @@
36
36
  "module": "dist/neutral/index.mjs",
37
37
  "types": "dist/neutral/index.d.ts",
38
38
  "devDependencies": {
39
- "@xylabs/ts-scripts-yarn3": "^4.2.3",
40
- "@xylabs/tsconfig": "^4.2.3",
39
+ "@xylabs/ts-scripts-yarn3": "^4.2.4",
40
+ "@xylabs/tsconfig": "^4.2.4",
41
41
  "typescript": "^5.6.3"
42
42
  },
43
43
  "engines": {
@@ -0,0 +1,3 @@
1
+ export type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function'
2
+
3
+ export type ObjectTypeShape = Record<string | number | symbol, FieldType>
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './ifDefined.ts'
2
2
  export * from './ifTypeOf.ts'
3
+ export * from './isType.ts'
4
+ export * from './ObjectTypeShape.ts'
3
5
  export * from './typeOf.ts'
4
6
  export * from './TypeOfTypes.ts'
5
7
  export * from './validateType.ts'
package/src/isType.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type { FieldType } from './ObjectTypeShape.ts'
2
+
3
+ export const isType = (value: unknown, expectedType: FieldType) => {
4
+ const typeofValue = typeof value
5
+ switch (expectedType) {
6
+ case 'array': {
7
+ return Array.isArray(value)
8
+ }
9
+ case 'null': {
10
+ return value === null
11
+ }
12
+ case 'undefined': {
13
+ return value === undefined
14
+ }
15
+ case 'object': {
16
+ // nulls resolve to objects, so exclude them
17
+ if (value === null) {
18
+ return false
19
+ }
20
+ // arrays resolve to objects, so exclude them
21
+ return typeofValue === 'object' && !Array.isArray(value)
22
+ }
23
+ default: {
24
+ return typeofValue === expectedType
25
+ }
26
+ }
27
+ }
package/typedoc.json CHANGED
@@ -1,5 +1,4 @@
1
1
  {
2
2
  "$schema": "https://typedoc.org/schema.json",
3
- "entryPoints": ["./src/index.ts"],
4
- "tsconfig": "./tsconfig.typedoc.json"
3
+ "entryPoints": ["./src/index.ts"]
5
4
  }