@trackunit/shared-utils 0.0.48 → 0.0.50
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 +52 -3
- package/index.esm.js +50 -4
- package/package.json +1 -1
- package/src/typeUtils.d.ts +59 -3
package/index.cjs.js
CHANGED
|
@@ -208,6 +208,9 @@ const formatCoordinates = (coordinates, toFixed) => {
|
|
|
208
208
|
: `${coordinates.latitude}, ${coordinates.longitude}`;
|
|
209
209
|
};
|
|
210
210
|
|
|
211
|
+
/* -------------------------------------------------------------------------- */
|
|
212
|
+
/* Filtering utilities */
|
|
213
|
+
/* -------------------------------------------------------------------------- */
|
|
211
214
|
/**
|
|
212
215
|
* Use with filter() to remove null and undefined from an array
|
|
213
216
|
*
|
|
@@ -228,20 +231,63 @@ const nonNullable = (value) => {
|
|
|
228
231
|
const truthy = (value) => {
|
|
229
232
|
return !!value;
|
|
230
233
|
};
|
|
234
|
+
/**
|
|
235
|
+
* Converts an object into an array of properly typed key-value pairs.
|
|
236
|
+
* This is a type-faithful alternative to Object.entries().
|
|
237
|
+
*
|
|
238
|
+
* Uses the `Object.entries()` to get an array of the object's key-value pairs,
|
|
239
|
+
* and then casts each key-value pair to `[keyof TObject, TObject[keyof TObject]]`
|
|
240
|
+
*
|
|
241
|
+
* See https://stackoverflow.com/a/76176570
|
|
242
|
+
*
|
|
243
|
+
* @param {TObject} object - The object to convert.
|
|
244
|
+
* @template TObject - The type of the object.
|
|
245
|
+
*/
|
|
246
|
+
const objectEntries = (object) =>
|
|
247
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, local-rules/prefer-custom-object-entries
|
|
248
|
+
Object.entries(object);
|
|
249
|
+
/**
|
|
250
|
+
* Converts an array of key-value pairs into an object.
|
|
251
|
+
* This is a type-faithful alternative to Object.fromEntries().
|
|
252
|
+
*
|
|
253
|
+
* @param {TEntries} entries - The array of key-value pairs to convert.
|
|
254
|
+
* @template TEntries - The type of the entries array.
|
|
255
|
+
*/
|
|
256
|
+
const objectFromEntries = (entries) => {
|
|
257
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, local-rules/prefer-custom-object-from-entries
|
|
258
|
+
return Object.fromEntries(entries);
|
|
259
|
+
};
|
|
231
260
|
/**
|
|
232
261
|
* Returns an array of the **correctly typed** keys of a given object.
|
|
262
|
+
* This is a type-faithful alternative to Object.keys().
|
|
263
|
+
*
|
|
264
|
+
* Uses the `Object.keys()` to get an array of the object's keys,
|
|
265
|
+
* and then casts each key to `keyof TObject`
|
|
233
266
|
*
|
|
234
|
-
*
|
|
235
|
-
* and then casts each key to `keyof TObject` to ensure TypeScript understands the keys are of the correct type.
|
|
267
|
+
* See https://stackoverflow.com/a/76176570
|
|
236
268
|
*
|
|
237
269
|
* @template TObject - The type of the object.
|
|
238
270
|
* @param {TObject} object - The object to get the keys from.
|
|
239
271
|
* @returns {(keyof TObject)[]} An array of the keys of the object.
|
|
240
272
|
*/
|
|
241
273
|
const objectKeys = (object) => {
|
|
242
|
-
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
274
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, local-rules/prefer-custom-object-keys
|
|
243
275
|
return Object.keys(object).map(key => key);
|
|
244
276
|
};
|
|
277
|
+
/**
|
|
278
|
+
* Returns an array of the **correctly typed** values of a given object.
|
|
279
|
+
* This is a type-faithful alternative to Object.values().
|
|
280
|
+
*
|
|
281
|
+
* Uses the `Object.values()` to get an array of the object's values,
|
|
282
|
+
* and then casts each value to `TObject[keyof TObject]`
|
|
283
|
+
*
|
|
284
|
+
* @template TObject - The type of the object.
|
|
285
|
+
* @param {TObject} object - The object to get the values from.
|
|
286
|
+
* @returns {TObject[keyof TObject][]} An array of the values of the object.
|
|
287
|
+
*/
|
|
288
|
+
const objectValues = (object) =>
|
|
289
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, local-rules/prefer-custom-object-values
|
|
290
|
+
Object.values(object);
|
|
245
291
|
|
|
246
292
|
/** toggle whether a value is in an array or not */
|
|
247
293
|
function toggle(array, value) {
|
|
@@ -1030,7 +1076,10 @@ exports.nonNullable = nonNullable;
|
|
|
1030
1076
|
exports.numberCompare = numberCompare;
|
|
1031
1077
|
exports.numberCompareUnknownAfterHighest = numberCompareUnknownAfterHighest;
|
|
1032
1078
|
exports.objNotEmpty = objNotEmpty;
|
|
1079
|
+
exports.objectEntries = objectEntries;
|
|
1080
|
+
exports.objectFromEntries = objectFromEntries;
|
|
1033
1081
|
exports.objectKeys = objectKeys;
|
|
1082
|
+
exports.objectValues = objectValues;
|
|
1034
1083
|
exports.pick = pick;
|
|
1035
1084
|
exports.removeLeftPadding = removeLeftPadding;
|
|
1036
1085
|
exports.resizeBlob = resizeBlob;
|
package/index.esm.js
CHANGED
|
@@ -204,6 +204,9 @@ const formatCoordinates = (coordinates, toFixed) => {
|
|
|
204
204
|
: `${coordinates.latitude}, ${coordinates.longitude}`;
|
|
205
205
|
};
|
|
206
206
|
|
|
207
|
+
/* -------------------------------------------------------------------------- */
|
|
208
|
+
/* Filtering utilities */
|
|
209
|
+
/* -------------------------------------------------------------------------- */
|
|
207
210
|
/**
|
|
208
211
|
* Use with filter() to remove null and undefined from an array
|
|
209
212
|
*
|
|
@@ -224,20 +227,63 @@ const nonNullable = (value) => {
|
|
|
224
227
|
const truthy = (value) => {
|
|
225
228
|
return !!value;
|
|
226
229
|
};
|
|
230
|
+
/**
|
|
231
|
+
* Converts an object into an array of properly typed key-value pairs.
|
|
232
|
+
* This is a type-faithful alternative to Object.entries().
|
|
233
|
+
*
|
|
234
|
+
* Uses the `Object.entries()` to get an array of the object's key-value pairs,
|
|
235
|
+
* and then casts each key-value pair to `[keyof TObject, TObject[keyof TObject]]`
|
|
236
|
+
*
|
|
237
|
+
* See https://stackoverflow.com/a/76176570
|
|
238
|
+
*
|
|
239
|
+
* @param {TObject} object - The object to convert.
|
|
240
|
+
* @template TObject - The type of the object.
|
|
241
|
+
*/
|
|
242
|
+
const objectEntries = (object) =>
|
|
243
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, local-rules/prefer-custom-object-entries
|
|
244
|
+
Object.entries(object);
|
|
245
|
+
/**
|
|
246
|
+
* Converts an array of key-value pairs into an object.
|
|
247
|
+
* This is a type-faithful alternative to Object.fromEntries().
|
|
248
|
+
*
|
|
249
|
+
* @param {TEntries} entries - The array of key-value pairs to convert.
|
|
250
|
+
* @template TEntries - The type of the entries array.
|
|
251
|
+
*/
|
|
252
|
+
const objectFromEntries = (entries) => {
|
|
253
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, local-rules/prefer-custom-object-from-entries
|
|
254
|
+
return Object.fromEntries(entries);
|
|
255
|
+
};
|
|
227
256
|
/**
|
|
228
257
|
* Returns an array of the **correctly typed** keys of a given object.
|
|
258
|
+
* This is a type-faithful alternative to Object.keys().
|
|
259
|
+
*
|
|
260
|
+
* Uses the `Object.keys()` to get an array of the object's keys,
|
|
261
|
+
* and then casts each key to `keyof TObject`
|
|
229
262
|
*
|
|
230
|
-
*
|
|
231
|
-
* and then casts each key to `keyof TObject` to ensure TypeScript understands the keys are of the correct type.
|
|
263
|
+
* See https://stackoverflow.com/a/76176570
|
|
232
264
|
*
|
|
233
265
|
* @template TObject - The type of the object.
|
|
234
266
|
* @param {TObject} object - The object to get the keys from.
|
|
235
267
|
* @returns {(keyof TObject)[]} An array of the keys of the object.
|
|
236
268
|
*/
|
|
237
269
|
const objectKeys = (object) => {
|
|
238
|
-
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
270
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, local-rules/prefer-custom-object-keys
|
|
239
271
|
return Object.keys(object).map(key => key);
|
|
240
272
|
};
|
|
273
|
+
/**
|
|
274
|
+
* Returns an array of the **correctly typed** values of a given object.
|
|
275
|
+
* This is a type-faithful alternative to Object.values().
|
|
276
|
+
*
|
|
277
|
+
* Uses the `Object.values()` to get an array of the object's values,
|
|
278
|
+
* and then casts each value to `TObject[keyof TObject]`
|
|
279
|
+
*
|
|
280
|
+
* @template TObject - The type of the object.
|
|
281
|
+
* @param {TObject} object - The object to get the values from.
|
|
282
|
+
* @returns {TObject[keyof TObject][]} An array of the values of the object.
|
|
283
|
+
*/
|
|
284
|
+
const objectValues = (object) =>
|
|
285
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, local-rules/prefer-custom-object-values
|
|
286
|
+
Object.values(object);
|
|
241
287
|
|
|
242
288
|
/** toggle whether a value is in an array or not */
|
|
243
289
|
function toggle(array, value) {
|
|
@@ -987,4 +1033,4 @@ const titleCase = (s) => {
|
|
|
987
1033
|
*/
|
|
988
1034
|
const removeLeftPadding = (id) => Number(id === null || id === void 0 ? void 0 : id.replace(/-/g, "").replace(/^0+/, ""));
|
|
989
1035
|
|
|
990
|
-
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 };
|
|
1036
|
+
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, objectEntries, objectFromEntries, objectKeys, objectValues, 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
|
@@ -47,15 +47,71 @@ export type MakePropertyOptional<TOriginal, KeyOfPropertyToMakeOptional extends
|
|
|
47
47
|
export type MakePropertiesUnion<TOriginal, TUnion> = {
|
|
48
48
|
[Key in keyof TOriginal]: TOriginal[Key] | TUnion;
|
|
49
49
|
};
|
|
50
|
+
/**
|
|
51
|
+
* Represents a type that makes all properties of a given type optional recursively.
|
|
52
|
+
* This is useful for creating types that can accept partial data structures,
|
|
53
|
+
* where some fields may be omitted.
|
|
54
|
+
*
|
|
55
|
+
* @template TObject - The original type whose properties should be made optional.
|
|
56
|
+
* @example
|
|
57
|
+
* // Usage:
|
|
58
|
+
* type PartialUser = DeepPartial<User>;
|
|
59
|
+
* const user: PartialUser = { name: 'John' }; // Only 'name' is required.
|
|
60
|
+
*/
|
|
61
|
+
export type DeepPartial<TObject> = TObject extends Record<PropertyKey, unknown> ? {
|
|
62
|
+
[Key in keyof TObject]?: DeepPartial<TObject[Key]>;
|
|
63
|
+
} : TObject;
|
|
64
|
+
type Entries<TObject> = {
|
|
65
|
+
[Key in keyof TObject]: [Key, TObject[Key]];
|
|
66
|
+
}[keyof TObject][];
|
|
67
|
+
type KeyValueTupleToObject<TEntries extends readonly (readonly [PropertyKey, unknown])[]> = {
|
|
68
|
+
[Key in TEntries[number] as Key[0]]: Key[1];
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Converts an object into an array of properly typed key-value pairs.
|
|
72
|
+
* This is a type-faithful alternative to Object.entries().
|
|
73
|
+
*
|
|
74
|
+
* Uses the `Object.entries()` to get an array of the object's key-value pairs,
|
|
75
|
+
* and then casts each key-value pair to `[keyof TObject, TObject[keyof TObject]]`
|
|
76
|
+
*
|
|
77
|
+
* See https://stackoverflow.com/a/76176570
|
|
78
|
+
*
|
|
79
|
+
* @param {TObject} object - The object to convert.
|
|
80
|
+
* @template TObject - The type of the object.
|
|
81
|
+
*/
|
|
82
|
+
export declare const objectEntries: <TObject extends Record<PropertyKey, unknown>>(object: TObject) => Entries<TObject>;
|
|
83
|
+
/**
|
|
84
|
+
* Converts an array of key-value pairs into an object.
|
|
85
|
+
* This is a type-faithful alternative to Object.fromEntries().
|
|
86
|
+
*
|
|
87
|
+
* @param {TEntries} entries - The array of key-value pairs to convert.
|
|
88
|
+
* @template TEntries - The type of the entries array.
|
|
89
|
+
*/
|
|
90
|
+
export declare const objectFromEntries: <const TEntries extends readonly (readonly [PropertyKey, unknown])[]>(entries: TEntries) => KeyValueTupleToObject<TEntries>;
|
|
50
91
|
/**
|
|
51
92
|
* Returns an array of the **correctly typed** keys of a given object.
|
|
93
|
+
* This is a type-faithful alternative to Object.keys().
|
|
52
94
|
*
|
|
53
|
-
*
|
|
54
|
-
* and then casts each key to `keyof TObject`
|
|
95
|
+
* Uses the `Object.keys()` to get an array of the object's keys,
|
|
96
|
+
* and then casts each key to `keyof TObject`
|
|
97
|
+
*
|
|
98
|
+
* See https://stackoverflow.com/a/76176570
|
|
55
99
|
*
|
|
56
100
|
* @template TObject - The type of the object.
|
|
57
101
|
* @param {TObject} object - The object to get the keys from.
|
|
58
102
|
* @returns {(keyof TObject)[]} An array of the keys of the object.
|
|
59
103
|
*/
|
|
60
|
-
export declare const objectKeys: <TObject extends
|
|
104
|
+
export declare const objectKeys: <TObject extends Record<PropertyKey, unknown>>(object: TObject) => (keyof TObject)[];
|
|
105
|
+
/**
|
|
106
|
+
* Returns an array of the **correctly typed** values of a given object.
|
|
107
|
+
* This is a type-faithful alternative to Object.values().
|
|
108
|
+
*
|
|
109
|
+
* Uses the `Object.values()` to get an array of the object's values,
|
|
110
|
+
* and then casts each value to `TObject[keyof TObject]`
|
|
111
|
+
*
|
|
112
|
+
* @template TObject - The type of the object.
|
|
113
|
+
* @param {TObject} object - The object to get the values from.
|
|
114
|
+
* @returns {TObject[keyof TObject][]} An array of the values of the object.
|
|
115
|
+
*/
|
|
116
|
+
export declare const objectValues: <TObject extends Record<PropertyKey, unknown>>(object: TObject) => TObject[keyof TObject][];
|
|
61
117
|
export {};
|