@xyo-network/object 2.74.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.
Files changed (63) hide show
  1. package/LICENSE +840 -0
  2. package/README.md +15 -0
  3. package/dist/AnyObject.d.mts +2 -0
  4. package/dist/AnyObject.d.mts.map +1 -0
  5. package/dist/AnyObject.d.ts +2 -0
  6. package/dist/AnyObject.d.ts.map +1 -0
  7. package/dist/AsObjectFactory.d.mts +8 -0
  8. package/dist/AsObjectFactory.d.mts.map +1 -0
  9. package/dist/AsObjectFactory.d.ts +8 -0
  10. package/dist/AsObjectFactory.d.ts.map +1 -0
  11. package/dist/EmptyObject.d.mts +4 -0
  12. package/dist/EmptyObject.d.mts.map +1 -0
  13. package/dist/EmptyObject.d.ts +4 -0
  14. package/dist/EmptyObject.d.ts.map +1 -0
  15. package/dist/IsObjectFactory.d.mts +10 -0
  16. package/dist/IsObjectFactory.d.mts.map +1 -0
  17. package/dist/IsObjectFactory.d.ts +10 -0
  18. package/dist/IsObjectFactory.d.ts.map +1 -0
  19. package/dist/ObjectWrapper.d.mts +8 -0
  20. package/dist/ObjectWrapper.d.mts.map +1 -0
  21. package/dist/ObjectWrapper.d.ts +8 -0
  22. package/dist/ObjectWrapper.d.ts.map +1 -0
  23. package/dist/StringKeyObject.d.mts +4 -0
  24. package/dist/StringKeyObject.d.mts.map +1 -0
  25. package/dist/StringKeyObject.d.ts +4 -0
  26. package/dist/StringKeyObject.d.ts.map +1 -0
  27. package/dist/WithAdditional.d.mts +3 -0
  28. package/dist/WithAdditional.d.mts.map +1 -0
  29. package/dist/WithAdditional.d.ts +3 -0
  30. package/dist/WithAdditional.d.ts.map +1 -0
  31. package/dist/asObject.d.mts +5 -0
  32. package/dist/asObject.d.mts.map +1 -0
  33. package/dist/asObject.d.ts +5 -0
  34. package/dist/asObject.d.ts.map +1 -0
  35. package/dist/index.d.mts +11 -0
  36. package/dist/index.d.mts.map +1 -0
  37. package/dist/index.d.ts +11 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +131 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/index.mjs +99 -0
  42. package/dist/index.mjs.map +1 -0
  43. package/dist/isObject.d.mts +2 -0
  44. package/dist/isObject.d.mts.map +1 -0
  45. package/dist/isObject.d.ts +2 -0
  46. package/dist/isObject.d.ts.map +1 -0
  47. package/dist/isType.d.mts +4 -0
  48. package/dist/isType.d.mts.map +1 -0
  49. package/dist/isType.d.ts +4 -0
  50. package/dist/isType.d.ts.map +1 -0
  51. package/package.json +62 -0
  52. package/src/AnyObject.ts +2 -0
  53. package/src/AsObjectFactory.ts +49 -0
  54. package/src/EmptyObject.ts +1 -0
  55. package/src/IsObjectFactory.ts +34 -0
  56. package/src/ObjectWrapper.ts +12 -0
  57. package/src/StringKeyObject.ts +1 -0
  58. package/src/WithAdditional.ts +3 -0
  59. package/src/asObject.ts +5 -0
  60. package/src/index.ts +10 -0
  61. package/src/isObject.ts +6 -0
  62. package/src/isType.ts +27 -0
  63. package/typedoc.json +5 -0
@@ -0,0 +1,49 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+
3
+ import { ObjectTypeCheck, ObjectTypeConfig } from './IsObjectFactory'
4
+
5
+ export const AsObjectFactory = {
6
+ // eslint-disable-next-line @typescript-eslint/ban-types
7
+ create: <T extends object>(typeCheck: ObjectTypeCheck<T>) => {
8
+ function func<TObject extends T>(
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ obj: object | undefined | null,
11
+ config?: ObjectTypeConfig,
12
+ ): TObject | undefined
13
+ function func<TObject extends T>(
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ obj: object | undefined | null,
16
+ assert: string | (() => string),
17
+ config?: ObjectTypeConfig,
18
+ ): TObject
19
+ function func<TObject extends T>(
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ obj: object | undefined | null,
22
+ assertOrConfig?: string | (() => string) | ObjectTypeConfig,
23
+ config?: ObjectTypeConfig,
24
+ ): TObject | undefined {
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ const noUndefined = (resolvedAssert: any): resolvedAssert is T => {
27
+ return !!resolvedAssert
28
+ }
29
+
30
+ if (obj === undefined) {
31
+ return undefined
32
+ }
33
+
34
+ if (obj === null) {
35
+ return undefined
36
+ }
37
+ const resolvedAssert = typeof assertOrConfig === 'object' ? undefined : assertOrConfig
38
+ const resolvedConfig = typeof assertOrConfig === 'object' ? assertOrConfig : config
39
+ const result = typeCheck(obj, resolvedConfig) ? obj : undefined
40
+
41
+ if (noUndefined(resolvedAssert)) {
42
+ return assertEx(result, typeof resolvedAssert === 'function' ? resolvedAssert() : resolvedAssert) as TObject
43
+ } else {
44
+ return result as TObject
45
+ }
46
+ }
47
+ return func
48
+ },
49
+ }
@@ -0,0 +1 @@
1
+ export type EmptyObject = { [key: string | number | symbol]: never }
@@ -0,0 +1,34 @@
1
+ import { Logger } from '@xyo-network/logger'
2
+
3
+ import { isType, ObjectTypeShape } from './isType'
4
+
5
+ export interface ObjectTypeConfig {
6
+ log?: boolean | Logger
7
+ }
8
+
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
10
+ export type ObjectTypeCheck<T extends {} = {}> = (obj: any, config?: ObjectTypeConfig) => obj is T
11
+
12
+ // eslint-disable-next-line @typescript-eslint/ban-types
13
+ export class IsObjectFactory<T extends {}> {
14
+ create(shape?: ObjectTypeShape, additionalChecks?: ObjectTypeCheck[]): ObjectTypeCheck<T> {
15
+ return (obj, { log } = {}): obj is T => {
16
+ if (!obj || typeof obj !== 'object') {
17
+ return false
18
+ }
19
+ return (
20
+ //do primary check
21
+ Object.entries(shape ?? {}).reduce((prev, [key, type]) => {
22
+ const result = isType(obj[key], type)
23
+ if (!result && log) {
24
+ const logger = typeof log === 'object' ? log : console
25
+ logger.warn(`isType Failed: ${key}: ${type}`)
26
+ }
27
+ return prev && result
28
+ }, true) &&
29
+ //perform additional checks
30
+ (additionalChecks?.reduce((prev, check) => prev && check(obj, { log }), true) ?? true)
31
+ )
32
+ }
33
+ }
34
+ }
@@ -0,0 +1,12 @@
1
+ import { EmptyObject } from './EmptyObject'
2
+ import { StringKeyObject } from './StringKeyObject'
3
+
4
+ export abstract class ObjectWrapper<T extends EmptyObject = EmptyObject> {
5
+ readonly obj: T
6
+ constructor(obj: T) {
7
+ this.obj = obj
8
+ }
9
+ protected get stringKeyObj() {
10
+ return this.obj as StringKeyObject
11
+ }
12
+ }
@@ -0,0 +1 @@
1
+ export type StringKeyObject = { [key: string]: unknown }
@@ -0,0 +1,3 @@
1
+ import { AnyObject } from './AnyObject'
2
+
3
+ export type WithAdditional<T extends AnyObject, TAdditional extends AnyObject | void = void> = TAdditional extends AnyObject ? T & TAdditional : T
@@ -0,0 +1,5 @@
1
+ import { AsObjectFactory } from './AsObjectFactory'
2
+ import { isObject } from './isObject'
3
+
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ export const asObject = (() => AsObjectFactory.create(<T>(obj: T): obj is T => isObject(obj)))()
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from './AnyObject'
2
+ export * from './asObject'
3
+ export * from './AsObjectFactory'
4
+ export * from './EmptyObject'
5
+ export * from './isObject'
6
+ export * from './IsObjectFactory'
7
+ export * from './isType'
8
+ export * from './ObjectWrapper'
9
+ export * from './StringKeyObject'
10
+ export * from './WithAdditional'
@@ -0,0 +1,6 @@
1
+ import { isType } from './isType'
2
+
3
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
+ export const isObject = (value: any): value is Record<string | number | symbol, any> => {
5
+ return isType(value, 'object')
6
+ }
package/src/isType.ts ADDED
@@ -0,0 +1,27 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ export type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function'
4
+
5
+ export type ObjectTypeShape = Record<string | number | symbol, FieldType>
6
+
7
+ export const isType = (value: unknown, expectedType: FieldType) => {
8
+ const typeofValue = typeof value
9
+ switch (expectedType) {
10
+ case 'array':
11
+ return Array.isArray(value)
12
+ case 'null':
13
+ return value === null
14
+ case 'undefined':
15
+ return value === undefined
16
+ case 'object': {
17
+ //nulls resolve to objects, so exclude them
18
+ if (value === null) {
19
+ return false
20
+ }
21
+ //arrays resolve to objects, so exclude them
22
+ return typeofValue === 'object' && !Array.isArray(value)
23
+ }
24
+ default:
25
+ return typeofValue === expectedType
26
+ }
27
+ }
package/typedoc.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://typedoc.org/schema.json",
3
+ "entryPoints": ["./src/index.ts"],
4
+ "tsconfig": "./tsconfig.typedoc.json"
5
+ }