@trackunit/shared-utils 0.0.44-alpha-d54f3828a2.0 → 0.0.44
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/index.cjs.js +15 -0
- package/index.esm.js +15 -1
- package/package.json +1 -1
- package/src/typeUtils.d.ts +35 -29
package/index.cjs.js
CHANGED
|
@@ -228,6 +228,20 @@ const nonNullable = (value) => {
|
|
|
228
228
|
const truthy = (value) => {
|
|
229
229
|
return !!value;
|
|
230
230
|
};
|
|
231
|
+
/**
|
|
232
|
+
* Returns an array of the **correctly typed** keys of a given object.
|
|
233
|
+
*
|
|
234
|
+
* This function uses the `Object.keys()` method to get an array of the object's keys,
|
|
235
|
+
* and then casts each key to `keyof TObject` to ensure TypeScript understands the keys are of the correct type.
|
|
236
|
+
*
|
|
237
|
+
* @template TObject - The type of the object.
|
|
238
|
+
* @param {TObject} object - The object to get the keys from.
|
|
239
|
+
* @returns {(keyof TObject)[]} An array of the keys of the object.
|
|
240
|
+
*/
|
|
241
|
+
const objectKeys = (object) => {
|
|
242
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
243
|
+
return Object.keys(object).map(key => key);
|
|
244
|
+
};
|
|
231
245
|
|
|
232
246
|
/** toggle whether a value is in an array or not */
|
|
233
247
|
function toggle(array, value) {
|
|
@@ -1014,6 +1028,7 @@ exports.nonNullable = nonNullable;
|
|
|
1014
1028
|
exports.numberCompare = numberCompare;
|
|
1015
1029
|
exports.numberCompareUnknownAfterHighest = numberCompareUnknownAfterHighest;
|
|
1016
1030
|
exports.objNotEmpty = objNotEmpty;
|
|
1031
|
+
exports.objectKeys = objectKeys;
|
|
1017
1032
|
exports.pick = pick;
|
|
1018
1033
|
exports.removeLeftPadding = removeLeftPadding;
|
|
1019
1034
|
exports.resizeBlob = resizeBlob;
|
package/index.esm.js
CHANGED
|
@@ -224,6 +224,20 @@ const nonNullable = (value) => {
|
|
|
224
224
|
const truthy = (value) => {
|
|
225
225
|
return !!value;
|
|
226
226
|
};
|
|
227
|
+
/**
|
|
228
|
+
* Returns an array of the **correctly typed** keys of a given object.
|
|
229
|
+
*
|
|
230
|
+
* This function uses the `Object.keys()` method to get an array of the object's keys,
|
|
231
|
+
* and then casts each key to `keyof TObject` to ensure TypeScript understands the keys are of the correct type.
|
|
232
|
+
*
|
|
233
|
+
* @template TObject - The type of the object.
|
|
234
|
+
* @param {TObject} object - The object to get the keys from.
|
|
235
|
+
* @returns {(keyof TObject)[]} An array of the keys of the object.
|
|
236
|
+
*/
|
|
237
|
+
const objectKeys = (object) => {
|
|
238
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
239
|
+
return Object.keys(object).map(key => key);
|
|
240
|
+
};
|
|
227
241
|
|
|
228
242
|
/** toggle whether a value is in an array or not */
|
|
229
243
|
function toggle(array, value) {
|
|
@@ -971,4 +985,4 @@ const titleCase = (s) => {
|
|
|
971
985
|
*/
|
|
972
986
|
const removeLeftPadding = (id) => Number(id === null || id === void 0 ? void 0 : id.replace(/-/g, "").replace(/^0+/, ""));
|
|
973
987
|
|
|
974
|
-
export { DateTimeFormat, HoursAndMinutesFormat, align, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, ensureArray, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getDifferenceBetweenDates, getEndOfDay, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isStringArrayEqual, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, pick, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, titleCase, toID, toIDs, toUUID, toggle, trimIds, trimPath, truthy, unionArraysByKey };
|
|
988
|
+
export { DateTimeFormat, HoursAndMinutesFormat, align, arrayLengthCompare, arrayNotEmpty, booleanCompare, capitalize, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, ensureArray, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getDifferenceBetweenDates, getEndOfDay, getISOStringFromDate, getMultipleCoordinatesFromGeoJsonObject, getPointCoordinateFromGeoJsonObject, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isStringArrayEqual, isUUID, isValidImage, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectKeys, pick, removeLeftPadding, resizeBlob, resizeImage, size, stringCompare, stringCompareFromKey, stringNaturalCompare, titleCase, toID, toIDs, toUUID, toggle, trimIds, trimPath, truthy, unionArraysByKey };
|
package/package.json
CHANGED
package/src/typeUtils.d.ts
CHANGED
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Infer the element type from the array type
|
|
3
|
-
*
|
|
4
|
-
* Use case:
|
|
5
|
-
* You have a GraphQL payload that returns an array of assets with a specific signature
|
|
6
|
-
* and wish be specific instead of just using Partial<Asset> as input for a component
|
|
7
|
-
*
|
|
8
|
-
* Example:
|
|
9
|
-
* interface SomeInterface {
|
|
10
|
-
* asset: ArrayElement<SomeAssetsQueryQuery["assets"]["payload"]>;
|
|
11
|
-
* }
|
|
12
|
-
*
|
|
13
|
-
*/
|
|
14
|
-
export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
|
|
15
1
|
/**
|
|
16
2
|
* Use with filter() to remove null and undefined from an array
|
|
17
3
|
*
|
|
@@ -19,8 +5,8 @@ export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
|
|
|
19
5
|
* [1, 2, 0, null].filter(nonNullable) // number[]
|
|
20
6
|
*
|
|
21
7
|
*/
|
|
22
|
-
export declare const nonNullable: <
|
|
23
|
-
type Truthy<
|
|
8
|
+
export declare const nonNullable: <TValue>(value: TValue) => value is NonNullable<TValue>;
|
|
9
|
+
type Truthy<TOriginal> = TOriginal extends false | "" | 0 | null | undefined ? never : TOriginal;
|
|
24
10
|
/**
|
|
25
11
|
* Use with filter() to remove not-truthy items from an array
|
|
26
12
|
*
|
|
@@ -28,28 +14,48 @@ type Truthy<T> = T extends false | "" | 0 | null | undefined ? never : T;
|
|
|
28
14
|
* [1, 2, 0, null].filter(truthy) // number[]
|
|
29
15
|
*
|
|
30
16
|
*/
|
|
31
|
-
export declare const truthy: <
|
|
17
|
+
export declare const truthy: <TValue>(value: TValue) => value is Truthy<TValue>;
|
|
32
18
|
/**
|
|
33
19
|
* Constructs a type by excluding specified keys from another type in a homomorphic manner.
|
|
34
20
|
* Homomorphic types maintain the same shape as the original type but exclude specified keys.
|
|
35
|
-
* For example works correctly on
|
|
21
|
+
* For example, this works correctly on discriminated unions whereas Omit messes up the type.
|
|
36
22
|
* https://github.com/microsoft/TypeScript/issues/54451
|
|
37
23
|
*
|
|
38
|
-
* @template
|
|
39
|
-
* @template
|
|
40
|
-
* @property {
|
|
24
|
+
* @template TOriginal - The original type.
|
|
25
|
+
* @template KeysToExclude - The keys to be excluded from the original type.
|
|
26
|
+
* @property {TOriginal} TResult - The resulting type excluding the keys specified by KeysToExclude while preserving the original structure.
|
|
41
27
|
*/
|
|
42
|
-
export type MappedOmit<
|
|
43
|
-
[
|
|
28
|
+
export type MappedOmit<TOriginal, KeysToExclude extends keyof TOriginal> = {
|
|
29
|
+
[TResult in keyof TOriginal as TResult extends KeysToExclude ? never : TResult]: TOriginal[TResult];
|
|
44
30
|
};
|
|
45
31
|
/**
|
|
46
32
|
* Creates a new type by making a specific property optional within the original type.
|
|
47
33
|
*
|
|
48
|
-
* @template
|
|
49
|
-
* @template
|
|
50
|
-
* @param {
|
|
51
|
-
* @param {
|
|
52
|
-
* @returns {Omit<
|
|
34
|
+
* @template TOriginal - The original type from which a property will be made optional.
|
|
35
|
+
* @template KeyOfPropertyToMakeOptional - The key of the property to be made optional.
|
|
36
|
+
* @param {TOriginal} obj - The object whose property needs to be made optional.
|
|
37
|
+
* @param {KeyOfPropertyToMakeOptional} propKey - The key of the property to be made optional.
|
|
38
|
+
* @returns {Omit<TOriginal, KeyOfPropertyToMakeOptional> & Partial<Pick<TOriginal, KeyOfPropertyToMakeOptional>>} - A type with the specified property made optional.
|
|
39
|
+
*/
|
|
40
|
+
export type MakePropertyOptional<TOriginal, KeyOfPropertyToMakeOptional extends keyof TOriginal> = MappedOmit<TOriginal, KeyOfPropertyToMakeOptional> & Partial<Pick<TOriginal, KeyOfPropertyToMakeOptional>>;
|
|
41
|
+
/**
|
|
42
|
+
* MakePropertiesUnionWith utility type transforms all properties in a type into a union with a specified type.
|
|
43
|
+
*
|
|
44
|
+
* @template TOriginal - The original type.
|
|
45
|
+
* @template TUnion - The type to union with each property in the original type.
|
|
46
|
+
*/
|
|
47
|
+
export type MakePropertiesUnion<TOriginal, TUnion> = {
|
|
48
|
+
[Key in keyof TOriginal]: TOriginal[Key] | TUnion;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Returns an array of the **correctly typed** keys of a given object.
|
|
52
|
+
*
|
|
53
|
+
* This function uses the `Object.keys()` method to get an array of the object's keys,
|
|
54
|
+
* and then casts each key to `keyof TObject` to ensure TypeScript understands the keys are of the correct type.
|
|
55
|
+
*
|
|
56
|
+
* @template TObject - The type of the object.
|
|
57
|
+
* @param {TObject} object - The object to get the keys from.
|
|
58
|
+
* @returns {(keyof TObject)[]} An array of the keys of the object.
|
|
53
59
|
*/
|
|
54
|
-
export
|
|
60
|
+
export declare const objectKeys: <TObject extends object>(object: TObject) => (keyof TObject)[];
|
|
55
61
|
export {};
|