@xylabs/object
Advanced tools
Comparing version 4.4.17 to 4.4.18
import type { EmptyObject } from './EmptyObject.ts'; | ||
import type { DeepOmitStartsWith } from './OmitStartsWith.ts'; | ||
export type OmitByPredicate<T extends EmptyObject = Record<string, unknown>> = (value: T[keyof T], key: keyof T) => boolean; | ||
export declare const omitBy: <T extends EmptyObject>(obj: T, predicate: OmitByPredicate, maxDepth?: number) => Partial<T>; | ||
export declare const omitByPrefix: <T extends EmptyObject, P extends string>(payload: T, prefix: P, maxDepth?: number) => DeepOmitStartsWith<T, P>; | ||
//# sourceMappingURL=omitBy.d.ts.map |
@@ -1,3 +0,6 @@ | ||
export type PickByPredicate<T extends object> = (value: T[keyof T], key: keyof T) => boolean; | ||
export declare const pickBy: <T extends object>(obj: T, predicate: PickByPredicate<T>) => Partial<T>; | ||
import type { EmptyObject } from './EmptyObject.ts'; | ||
import type { DeepPickStartsWith } from './PickStartsWith.ts'; | ||
export type PickByPredicate<T extends EmptyObject = Record<string, unknown>> = (value: T[keyof T], key: keyof T) => boolean; | ||
export declare const pickBy: <T extends EmptyObject>(obj: T, predicate: PickByPredicate, maxDepth?: number) => Partial<T>; | ||
export declare const pickByPrefix: <T extends EmptyObject, P extends string>(payload: T, prefix: P, maxDepth?: number) => DeepPickStartsWith<T, P>; | ||
//# sourceMappingURL=pickBy.d.ts.map |
export type PickStartsWith<T, Prefix extends string> = { | ||
[K in keyof T as K extends `${Prefix}${string}` ? K : never]: T[K]; | ||
}; | ||
export type PickOmitStartsWith<T, Prefix extends string> = T extends (infer U)[] ? PickOmitStartsWith<U, Prefix>[] : T extends object ? { | ||
[K in keyof T as K extends string ? K extends `${Prefix}${string}` ? K : never : K]: PickOmitStartsWith<T[K], Prefix>; | ||
export type DeepPickStartsWith<T, Prefix extends string> = T extends (infer U)[] ? DeepPickStartsWith<U, Prefix>[] : T extends object ? { | ||
[K in keyof T as K extends string ? K extends `${Prefix}${string}` ? K : never : K]: DeepPickStartsWith<T[K], Prefix>; | ||
} : T; | ||
//# sourceMappingURL=PickStartsWith.d.ts.map |
{ | ||
"name": "@xylabs/object", | ||
"version": "4.4.17", | ||
"version": "4.4.18", | ||
"description": "Primary SDK for using XYO Protocol 2.0", | ||
@@ -32,6 +32,6 @@ "homepage": "https://xyo.network", | ||
"dependencies": { | ||
"@xylabs/assert": "^4.4.17", | ||
"@xylabs/logger": "^4.4.17", | ||
"@xylabs/promise": "^4.4.17", | ||
"@xylabs/typeof": "^4.4.17" | ||
"@xylabs/assert": "^4.4.18", | ||
"@xylabs/logger": "^4.4.18", | ||
"@xylabs/promise": "^4.4.18", | ||
"@xylabs/typeof": "^4.4.18" | ||
}, | ||
@@ -38,0 +38,0 @@ "devDependencies": { |
@@ -0,3 +1,6 @@ | ||
import { assertEx } from '@xylabs/assert' | ||
import type { EmptyObject } from './EmptyObject.ts' | ||
import type { JsonObject } from './JsonObject.ts' | ||
import type { DeepOmitStartsWith } from './OmitStartsWith.ts' | ||
@@ -46,1 +49,10 @@ export type OmitByPredicate<T extends EmptyObject = Record<string, unknown>> = (value: T[keyof T], key: keyof T) => boolean | ||
} | ||
const omitByPrefixPredicate = (prefix: string) => (_: unknown, key: string) => { | ||
assertEx(typeof key === 'string', () => `Invalid key type [${key}, ${typeof key}]`) | ||
return key.startsWith(prefix) | ||
} | ||
export const omitByPrefix = <T extends EmptyObject, P extends string>(payload: T, prefix: P, maxDepth = 100): DeepOmitStartsWith<T, P> => { | ||
return omitBy(payload, omitByPrefixPredicate(prefix), maxDepth) as unknown as DeepOmitStartsWith<T, P> | ||
} |
@@ -1,8 +0,25 @@ | ||
export type PickByPredicate<T extends object> = (value: T[keyof T], key: keyof T) => boolean | ||
import { assertEx } from '@xylabs/assert' | ||
export const pickBy = <T extends object>( | ||
import type { EmptyObject } from './EmptyObject.ts' | ||
import type { JsonObject } from './JsonObject.ts' | ||
import type { DeepPickStartsWith } from './PickStartsWith.ts' | ||
export type PickByPredicate<T extends EmptyObject = Record<string, unknown>> = (value: T[keyof T], key: keyof T) => boolean | ||
const pickByArray = <T>( | ||
obj: T[], | ||
predicate: PickByPredicate, | ||
maxDepth: number, | ||
): T[] => { | ||
return obj.map((value) => { | ||
return (value !== null && typeof value === 'object') ? pickBy(value, predicate, maxDepth) : value | ||
}) as T[] | ||
} | ||
const pickByObject = <T extends EmptyObject>( | ||
obj: T, | ||
predicate: PickByPredicate<T>, | ||
predicate: PickByPredicate, | ||
maxDepth: number, | ||
): Partial<T> => { | ||
const result: Partial<T> = {} | ||
const result: JsonObject = {} | ||
@@ -13,3 +30,3 @@ for (const key in obj) { | ||
if (predicate(value, key)) { | ||
result[key] = value | ||
result[key] = ((value !== null && typeof value === 'object') ? pickBy(value, predicate, maxDepth - 1) : value) as JsonObject | ||
} | ||
@@ -19,3 +36,24 @@ } | ||
return result | ||
return result as T | ||
} | ||
export const pickBy = <T extends EmptyObject>( | ||
obj: T, | ||
predicate: PickByPredicate, | ||
maxDepth = 1, | ||
): Partial<T> => { | ||
if (maxDepth <= 0) { | ||
return obj | ||
} | ||
return Array.isArray(obj) ? pickByArray(obj, predicate, maxDepth - 1) as T : pickByObject(obj, predicate, maxDepth - 1) as T | ||
} | ||
const pickByPrefixPredicate = (prefix: string) => (_: unknown, key: string) => { | ||
assertEx(typeof key === 'string', () => `Invalid key type [${key}, ${typeof key}]`) | ||
return key.startsWith(prefix) | ||
} | ||
export const pickByPrefix = <T extends EmptyObject, P extends string>(payload: T, prefix: P, maxDepth = 100): DeepPickStartsWith<T, P> => { | ||
return pickBy(payload, pickByPrefixPredicate(prefix), maxDepth) as unknown as DeepPickStartsWith<T, P> | ||
} |
@@ -5,4 +5,4 @@ export type PickStartsWith<T, Prefix extends string> = { | ||
export type PickOmitStartsWith<T, Prefix extends string> = T extends (infer U)[] | ||
? PickOmitStartsWith<U, Prefix>[] // Special handling for arrays | ||
export type DeepPickStartsWith<T, Prefix extends string> = T extends (infer U)[] | ||
? DeepPickStartsWith<U, Prefix>[] // Special handling for arrays | ||
: T extends object | ||
@@ -14,4 +14,4 @@ ? { | ||
: never | ||
: K]: PickOmitStartsWith<T[K], Prefix>; | ||
: K]: DeepPickStartsWith<T[K], Prefix>; | ||
} | ||
: T |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128730
1143
Updated@xylabs/assert@^4.4.18
Updated@xylabs/logger@^4.4.18
Updated@xylabs/promise@^4.4.18
Updated@xylabs/typeof@^4.4.18