@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.
- package/LICENSE +840 -0
- package/README.md +15 -0
- package/dist/AnyObject.d.mts +2 -0
- package/dist/AnyObject.d.mts.map +1 -0
- package/dist/AnyObject.d.ts +2 -0
- package/dist/AnyObject.d.ts.map +1 -0
- package/dist/AsObjectFactory.d.mts +8 -0
- package/dist/AsObjectFactory.d.mts.map +1 -0
- package/dist/AsObjectFactory.d.ts +8 -0
- package/dist/AsObjectFactory.d.ts.map +1 -0
- package/dist/EmptyObject.d.mts +4 -0
- package/dist/EmptyObject.d.mts.map +1 -0
- package/dist/EmptyObject.d.ts +4 -0
- package/dist/EmptyObject.d.ts.map +1 -0
- package/dist/IsObjectFactory.d.mts +10 -0
- package/dist/IsObjectFactory.d.mts.map +1 -0
- package/dist/IsObjectFactory.d.ts +10 -0
- package/dist/IsObjectFactory.d.ts.map +1 -0
- package/dist/ObjectWrapper.d.mts +8 -0
- package/dist/ObjectWrapper.d.mts.map +1 -0
- package/dist/ObjectWrapper.d.ts +8 -0
- package/dist/ObjectWrapper.d.ts.map +1 -0
- package/dist/StringKeyObject.d.mts +4 -0
- package/dist/StringKeyObject.d.mts.map +1 -0
- package/dist/StringKeyObject.d.ts +4 -0
- package/dist/StringKeyObject.d.ts.map +1 -0
- package/dist/WithAdditional.d.mts +3 -0
- package/dist/WithAdditional.d.mts.map +1 -0
- package/dist/WithAdditional.d.ts +3 -0
- package/dist/WithAdditional.d.ts.map +1 -0
- package/dist/asObject.d.mts +5 -0
- package/dist/asObject.d.mts.map +1 -0
- package/dist/asObject.d.ts +5 -0
- package/dist/asObject.d.ts.map +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +131 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +99 -0
- package/dist/index.mjs.map +1 -0
- package/dist/isObject.d.mts +2 -0
- package/dist/isObject.d.mts.map +1 -0
- package/dist/isObject.d.ts +2 -0
- package/dist/isObject.d.ts.map +1 -0
- package/dist/isType.d.mts +4 -0
- package/dist/isType.d.mts.map +1 -0
- package/dist/isType.d.ts +4 -0
- package/dist/isType.d.ts.map +1 -0
- package/package.json +62 -0
- package/src/AnyObject.ts +2 -0
- package/src/AsObjectFactory.ts +49 -0
- package/src/EmptyObject.ts +1 -0
- package/src/IsObjectFactory.ts +34 -0
- package/src/ObjectWrapper.ts +12 -0
- package/src/StringKeyObject.ts +1 -0
- package/src/WithAdditional.ts +3 -0
- package/src/asObject.ts +5 -0
- package/src/index.ts +10 -0
- package/src/isObject.ts +6 -0
- package/src/isType.ts +27 -0
- 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 }
|
package/src/asObject.ts
ADDED
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'
|
package/src/isObject.ts
ADDED
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
|
+
}
|