@karmaniverous/entity-tools
Advanced tools
Comparing version 0.4.4 to 0.4.5
@@ -64,3 +64,3 @@ /** | ||
* | ||
* @typeParam T - The `Record` type. | ||
* @typeParam O - The `Record` type. | ||
* | ||
@@ -71,4 +71,4 @@ * @returns The `Record` type with only specified properties. | ||
*/ | ||
type Exactify<T extends Record<string, unknown>> = { | ||
[P in keyof T as string extends P ? never : number extends P ? never : symbol extends P ? never : P]: T[P]; | ||
type Exactify<O extends object> = { | ||
[P in keyof O as string extends P ? never : number extends P ? never : symbol extends P ? never : P]: O[P]; | ||
}; | ||
@@ -156,2 +156,50 @@ | ||
/** | ||
* The base EntityMap type. All EntityMaps should extend this type. | ||
* | ||
* @category Entities | ||
*/ | ||
type EntityMap = Record<string, Entity>; | ||
/** | ||
* Returns the keys of an {@link Entity | `Entity`} type. | ||
* | ||
* @typeParam E - The {@link Entity | `Entity`} type. | ||
* | ||
* @category Entities | ||
* @protected | ||
*/ | ||
type EntityKeys<E extends Entity> = E extends E ? keyof E : never; | ||
/** | ||
* Returns the value type of a property `P` of an {@link Entity | `Entity`} type. | ||
* | ||
* @typeParam E - The {@link Entity | `Entity`} type. | ||
* @typeParam P - The property key. | ||
* | ||
* @category Entities | ||
* @protected | ||
*/ | ||
type EntityValue<E extends Entity, P extends PropertyKey> = E extends Record<P, infer V> | Partial<Record<P, infer V>> ? V : never; | ||
/** | ||
* Returns a union of exactified entity types in an {@link EntityMap | `EntityMap`}. | ||
* | ||
* @typeParam M - The {@link EntityMap | `EntityMap`} type. | ||
* | ||
* @category Entities | ||
* @protected | ||
*/ | ||
type EntityMapValues<M extends EntityMap> = { | ||
[P in keyof M]: Exactify<M[P]>; | ||
}[keyof Exactify<M>]; | ||
/** | ||
* Flattens an {@link EntityMap | `EntityMap`} into a single object with matching key types unionized. | ||
* | ||
* @typeParam M - The {@link EntityMap | `EntityMap`} to flatten. | ||
* | ||
* @category Entities | ||
*/ | ||
type FlattenEntityMap<M extends EntityMap> = { | ||
[K in EntityKeys<EntityMapValues<M>>]: EntityValue<EntityMapValues<M>, K>; | ||
}; | ||
/** | ||
* Makes specified properties of T optional. | ||
@@ -167,2 +215,48 @@ * | ||
/** | ||
* Returns `true` if there is no intersection between `First` the elements of `Rest`. | ||
* | ||
* @typeParam First - A `string` type | ||
* @typeParam Rest - A tuple of `string` types | ||
* | ||
* @returns `true` if there is no intersection between `First` the elements of `Rest`, otherwise a custom error type. | ||
* | ||
* @example | ||
* ```ts | ||
* type ReturnsTrue = AllDisjoint<'a', ['b' | 'c', 'd']>; | ||
* // true | ||
* | ||
* type ReturnsError = AllDisjoint<'c', ['b' | 'c', 'c']>; | ||
* // { __error__: 'overlaps on c' } | ||
* ``` | ||
* | ||
* @category Utilities | ||
* @protected | ||
*/ | ||
type AllDisjoint<First extends string, Rest extends string[]> = Rest extends [infer Head, ...infer Tail] ? Head extends string ? [First & Head] extends [never] ? AllDisjoint<First, Tail extends string[] ? Tail : []> : { | ||
__error__: `overlaps on ${First}`; | ||
} : true : true; | ||
/** | ||
* Returns `true` if there is no intersection between the elements of `T`. | ||
* | ||
* @typeParam T - The tuple of string types to check for mutual exclusivity. | ||
* | ||
* @returns `true` if there is no intersection between the elements of `T`, otherwise a custom error type. | ||
* | ||
* @example | ||
* ```ts | ||
* type ReturnsTrue = MutuallyExclusive<['a', 'b' | 'c', 'd']>; | ||
* // true | ||
* | ||
* type ReturnsError = MutuallyExclusive<['a', 'b' | 'c', 'c']>; | ||
* // { __error__: 'overlaps on c' } | ||
* ``` | ||
* | ||
* @category Utilities | ||
*/ | ||
type MutuallyExclusive<T extends string[]> = T extends [ | ||
infer Head, | ||
...infer Tail | ||
] ? Head extends string ? Tail extends string[] ? AllDisjoint<Head, Tail> extends true ? MutuallyExclusive<Tail> : AllDisjoint<Head, Tail> : true : true : true; | ||
/** | ||
* A `null` or `undefined` value. | ||
@@ -185,12 +279,12 @@ * | ||
/** | ||
* Returns the properties of {@link Entity | `Entity`} `E` of types that extend type `T`. Ignores `undefined` types. | ||
* Returns the properties of `object` `O` with types that extend type `T`. Ignores `undefined` types. | ||
* | ||
* @typeParam E - The {@link Entity | `Entity`} type. | ||
* @typeParam O - The `object` type. | ||
* @typeParam T - The type to filter by. | ||
* | ||
* @category Entities | ||
* @category Utilities | ||
*/ | ||
type PropertiesOfType<E extends Entity, T> = keyof { | ||
[Property in keyof Exactify<E> as [T] extends [never] ? [NonNullable<E[Property]>] extends [never] ? Property : never : [NonNullable<E[Property]>] extends [never] ? never : NonNullable<E[Property]> extends T ? Property : never]: never; | ||
} & string; | ||
type PropertiesOfType<O extends object, T> = keyof { | ||
[Property in keyof O as [T] extends [never] ? [NonNullable<O[Property]>] extends [never] ? Property : never : [NonNullable<O[Property]>] extends [never] ? never : NonNullable<O[Property]> extends T ? Property : never]: never; | ||
}; | ||
@@ -206,25 +300,25 @@ /** | ||
*/ | ||
type PartialTranscodable<Item extends Entity, T extends TranscodeMap> = Partial<Pick<Item, PropertiesOfType<Item, T[keyof T]>>>; | ||
type PartialTranscodable<Item extends Entity, T extends TranscodeMap> = Partial<Pick<Item, PropertiesOfType<Item, T[keyof Exactify<T>]>>>; | ||
/** | ||
* Returns the properties of {@link Entity | `Entity`} `E` of types that do not extend type `T`. Ignores `undefined` types. | ||
* Returns the properties of `object` `O` with types that do not extend type `T`. Ignores `undefined` types. | ||
* | ||
* @typeParam E - The {@link Entity | `Entity`} type. | ||
* @typeParam O - The 'object' type. | ||
* @typeParam T - The type to filter by. | ||
* | ||
* @category Entities | ||
* @category Utilities | ||
*/ | ||
type PropertiesNotOfType<E extends Entity, T> = keyof { | ||
[Property in keyof Exactify<E> as [T] extends [never] ? [NonNullable<E[Property]>] extends [never] ? never : Property : [NonNullable<E[Property]>] extends [never] ? NonNullable<E[Property]> extends T ? Property : never : never]: never; | ||
type PropertiesNotOfType<O extends object, T> = keyof { | ||
[Property in keyof O as [T] extends [never] ? [NonNullable<O[Property]>] extends [never] ? never : Property : [NonNullable<O[Property]>] extends [never] ? NonNullable<O[Property]> extends T ? Property : never : never]: never; | ||
} & string; | ||
/** | ||
* Specifies progressive sorting on properties of `Item`. | ||
* Specifies progressive sorting on properties of an {@link Entity | `Entity`} type. | ||
* | ||
* @typeParam Item - Item type, must extend {@link Entity | `Entity`}. | ||
* @typeParam E - {@link Entity | `Entity`} type. | ||
* | ||
* @category Sort | ||
*/ | ||
type SortOrder<Item extends Entity> = { | ||
property: keyof Item; | ||
type SortOrder<E extends Entity> = { | ||
property: keyof Exactify<E>; | ||
desc?: boolean; | ||
@@ -257,5 +351,5 @@ }[]; | ||
/** | ||
* Returns the properties of {@link Entity | `Entity`} `E` whose types are covered by {@link TranscodeMap | `TranscodeMap`} `T`. | ||
* Returns the properties of an {@link Entity | `Entity`} or {@link EntityMap | `EntityMap`} whose types are covered by {@link TranscodeMap | `TranscodeMap`} `T`. | ||
* | ||
* @typeParam E - The {@link Entity | `Entity`} type. | ||
* @typeParam O - The {@link Entity | `Entity`} or {@link EntityMap | `EntityMap`} type. | ||
* @typeParam T - The {@link TranscodeMap | `TranscodeMap`}. | ||
@@ -266,8 +360,8 @@ * | ||
*/ | ||
type TranscodableProperties<E extends Entity, T extends TranscodeMap> = PropertiesOfType<E, T[keyof Exactify<T>]>; | ||
type TranscodableProperties<O extends EntityMap | Entity, T extends TranscodeMap> = PropertiesOfType<O extends EntityMap ? FlattenEntityMap<O> : O, T[keyof Exactify<T>]>; | ||
/** | ||
* Returns the properties of {@link Entity | `Entity`} `E` whose types are not covered by {@link TranscodeMap | `TranscodeMap`} `T`. | ||
* Returns the properties of an {@link Entity | `Entity`} or {@link EntityMap | `EntityMap`} whose types are not covered by {@link TranscodeMap | `TranscodeMap`} `T`. | ||
* | ||
* @typeParam E - The {@link Entity | `Entity`} type. | ||
* @typeParam O - The {@link Entity | `Entity`} or {@link EntityMap | `EntityMap`} type. | ||
* @typeParam T - The {@link TranscodeMap | `TranscodeMap`}. | ||
@@ -278,3 +372,3 @@ * | ||
*/ | ||
type UntranscodableProperties<E extends Entity, T extends TranscodeMap> = PropertiesNotOfType<E, T[keyof Exactify<T>]>; | ||
type UntranscodableProperties<O extends EntityMap | Entity, T extends TranscodeMap> = PropertiesNotOfType<O extends EntityMap ? FlattenEntityMap<O> : O, T[keyof Exactify<T>]>; | ||
@@ -293,2 +387,2 @@ /** | ||
export { type DefaultTranscodeMap, type Entity, type Exactify, type MakeOptional, type Nil, type PartialTranscodable, type PropertiesNotOfType, type PropertiesOfType, type SortOrder, type TranscodableProperties, type TranscodeMap, type Transcodes, type UntranscodableProperties, type WithRequiredAndNonNullable, defaultTranscodes, isNil, sort }; | ||
export { type AllDisjoint, type DefaultTranscodeMap, type Entity, type EntityKeys, type EntityMap, type EntityMapValues, type EntityValue, type Exactify, type FlattenEntityMap, type MakeOptional, type MutuallyExclusive, type Nil, type PartialTranscodable, type PropertiesNotOfType, type PropertiesOfType, type SortOrder, type TranscodableProperties, type TranscodeMap, type Transcodes, type UntranscodableProperties, type WithRequiredAndNonNullable, defaultTranscodes, isNil, sort }; |
@@ -129,3 +129,3 @@ { | ||
"types": "dist/index.d.ts", | ||
"version": "0.4.4", | ||
"version": "0.4.5", | ||
"dependencies": { | ||
@@ -132,0 +132,0 @@ "radash": "^12.1.0" |
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
31280
727