@trackunit/shared-utils 1.2.41 → 1.2.43
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 +45 -0
- package/index.esm.js +43 -1
- package/package.json +1 -1
- package/src/objectUtils.d.ts +28 -1
package/index.cjs.js
CHANGED
|
@@ -709,6 +709,48 @@ const getFirstLevelObjectPropertyDifferences = (obj1, obj2) => {
|
|
|
709
709
|
return diff;
|
|
710
710
|
}, {});
|
|
711
711
|
};
|
|
712
|
+
/**
|
|
713
|
+
* Replaces all null or undefined number values in a record with 0.
|
|
714
|
+
*
|
|
715
|
+
* @param record - The record to transform
|
|
716
|
+
* @returns {Record<string, number>} A new record with all null/undefined number values replaced with 0
|
|
717
|
+
* @example replaceNullableNumbersWithZero({ a: 1, b: null, c: undefined }) // { a: 1, b: 0, c: 0 }
|
|
718
|
+
*/
|
|
719
|
+
const replaceNullableNumbersWithZero = (record) => {
|
|
720
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, no-autofix/local-rules/prefer-custom-object-from-entries
|
|
721
|
+
return Object.fromEntries(objectEntries(record).map(([key, value]) => [key, value ?? 0]));
|
|
722
|
+
};
|
|
723
|
+
/**
|
|
724
|
+
* Removes a property from an object in a type-safe way.
|
|
725
|
+
*
|
|
726
|
+
* @param obj - The object to remove a property from
|
|
727
|
+
* @param key - The key to remove
|
|
728
|
+
* @returns {object} A new object without the specified property
|
|
729
|
+
* @example removeProperty({ a: 1, b: 2 }, 'a') // { b: 2 }
|
|
730
|
+
*/
|
|
731
|
+
const removeProperty = (obj, key) => {
|
|
732
|
+
const { [key]: _, ...rest } = obj;
|
|
733
|
+
// should be safe because we remove the key from the object, just no way to type it properly
|
|
734
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
735
|
+
return rest;
|
|
736
|
+
};
|
|
737
|
+
/**
|
|
738
|
+
* Removes multiple properties from an object in a type-safe way.
|
|
739
|
+
*
|
|
740
|
+
* @param obj - The object to remove properties from
|
|
741
|
+
* @param keys - The keys to remove
|
|
742
|
+
* @returns {object} A new object without the specified properties
|
|
743
|
+
* @example removeProperties({ a: 1, b: 2, c: 3 }, ['a', 'b']) // { c: 3 }
|
|
744
|
+
*/
|
|
745
|
+
const removeProperties = (obj, keys) => {
|
|
746
|
+
const result = { ...obj };
|
|
747
|
+
for (const key of keys) {
|
|
748
|
+
delete result[key];
|
|
749
|
+
}
|
|
750
|
+
// should be safe because we remove the keys from the object, just no way to type it properly
|
|
751
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
752
|
+
return result;
|
|
753
|
+
};
|
|
712
754
|
|
|
713
755
|
/**
|
|
714
756
|
* @param path the path to manipulate
|
|
@@ -1359,6 +1401,9 @@ exports.objectValues = objectValues;
|
|
|
1359
1401
|
exports.pick = pick;
|
|
1360
1402
|
exports.preload = preload;
|
|
1361
1403
|
exports.removeLeftPadding = removeLeftPadding;
|
|
1404
|
+
exports.removeProperties = removeProperties;
|
|
1405
|
+
exports.removeProperty = removeProperty;
|
|
1406
|
+
exports.replaceNullableNumbersWithZero = replaceNullableNumbersWithZero;
|
|
1362
1407
|
exports.resizeBlob = resizeBlob;
|
|
1363
1408
|
exports.resizeImage = resizeImage;
|
|
1364
1409
|
exports.rgb2hex = rgb2hex;
|
package/index.esm.js
CHANGED
|
@@ -707,6 +707,48 @@ const getFirstLevelObjectPropertyDifferences = (obj1, obj2) => {
|
|
|
707
707
|
return diff;
|
|
708
708
|
}, {});
|
|
709
709
|
};
|
|
710
|
+
/**
|
|
711
|
+
* Replaces all null or undefined number values in a record with 0.
|
|
712
|
+
*
|
|
713
|
+
* @param record - The record to transform
|
|
714
|
+
* @returns {Record<string, number>} A new record with all null/undefined number values replaced with 0
|
|
715
|
+
* @example replaceNullableNumbersWithZero({ a: 1, b: null, c: undefined }) // { a: 1, b: 0, c: 0 }
|
|
716
|
+
*/
|
|
717
|
+
const replaceNullableNumbersWithZero = (record) => {
|
|
718
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion, no-autofix/local-rules/prefer-custom-object-from-entries
|
|
719
|
+
return Object.fromEntries(objectEntries(record).map(([key, value]) => [key, value ?? 0]));
|
|
720
|
+
};
|
|
721
|
+
/**
|
|
722
|
+
* Removes a property from an object in a type-safe way.
|
|
723
|
+
*
|
|
724
|
+
* @param obj - The object to remove a property from
|
|
725
|
+
* @param key - The key to remove
|
|
726
|
+
* @returns {object} A new object without the specified property
|
|
727
|
+
* @example removeProperty({ a: 1, b: 2 }, 'a') // { b: 2 }
|
|
728
|
+
*/
|
|
729
|
+
const removeProperty = (obj, key) => {
|
|
730
|
+
const { [key]: _, ...rest } = obj;
|
|
731
|
+
// should be safe because we remove the key from the object, just no way to type it properly
|
|
732
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
733
|
+
return rest;
|
|
734
|
+
};
|
|
735
|
+
/**
|
|
736
|
+
* Removes multiple properties from an object in a type-safe way.
|
|
737
|
+
*
|
|
738
|
+
* @param obj - The object to remove properties from
|
|
739
|
+
* @param keys - The keys to remove
|
|
740
|
+
* @returns {object} A new object without the specified properties
|
|
741
|
+
* @example removeProperties({ a: 1, b: 2, c: 3 }, ['a', 'b']) // { c: 3 }
|
|
742
|
+
*/
|
|
743
|
+
const removeProperties = (obj, keys) => {
|
|
744
|
+
const result = { ...obj };
|
|
745
|
+
for (const key of keys) {
|
|
746
|
+
delete result[key];
|
|
747
|
+
}
|
|
748
|
+
// should be safe because we remove the keys from the object, just no way to type it properly
|
|
749
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
750
|
+
return result;
|
|
751
|
+
};
|
|
710
752
|
|
|
711
753
|
/**
|
|
712
754
|
* @param path the path to manipulate
|
|
@@ -1304,4 +1346,4 @@ const convertYardsToMeters = (value) => {
|
|
|
1304
1346
|
return Math.round(value * 0.9144);
|
|
1305
1347
|
};
|
|
1306
1348
|
|
|
1307
|
-
export { DateTimeFormat, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, calculateImageScaleRatio, capitalize, colorsFromStyleDeclaration, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, fetchImageAsBase64, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getAllColors, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getISOStringFromDate, getMimeTypeFromDataURL, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, loadSVGDimensions, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, preload, removeLeftPadding, resizeBlob, resizeImage, rgb2hex, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, svgToPNG, titleCase, toID, toIDs, toPNG, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
|
|
1349
|
+
export { DateTimeFormat, HoursAndMinutesFormat, align, alphabeticallySort, arrayLengthCompare, arrayNotEmpty, booleanCompare, calculateImageScaleRatio, capitalize, colorsFromStyleDeclaration, convertBlobToBase64, convertMetersToYards, convertYardsToMeters, dateCompare, deleteUndefinedKeys, difference, doNothing, enumFromValue, enumFromValueTypesafe, enumOrUndefinedFromValue, exhaustiveCheck, fetchImageAsBase64, filterByMultiple, formatAddress, formatCoordinates, fuzzySearch, getAllColors, getDifferenceBetweenDates, getEndOfDay, getFirstLevelObjectPropertyDifferences, getISOStringFromDate, getMimeTypeFromDataURL, getResizedDimensions, getStartOfDay, groupBy, groupTinyDataToOthers, hourIntervals, intersection, isArrayEqual, isSorted, isUUID, isValidImage, loadSVGDimensions, nonNullable, numberCompare, numberCompareUnknownAfterHighest, objNotEmpty, objectEntries, objectFromEntries, objectKeys, objectValues, pick, preload, removeLeftPadding, removeProperties, removeProperty, replaceNullableNumbersWithZero, resizeBlob, resizeImage, rgb2hex, size, stringCompare, stringCompareFromKey, stringNaturalCompare, stripHiddenCharacters, svgToPNG, titleCase, toID, toIDs, toPNG, toUUID, trimIds, trimPath, truthy, unionArraysByKey, uuidv3, uuidv4, uuidv5 };
|
package/package.json
CHANGED
package/src/objectUtils.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MappedOmit } from "./typeUtils";
|
|
1
2
|
/**
|
|
2
3
|
* Deletes all undefined keys from an object.
|
|
3
4
|
*
|
|
@@ -21,4 +22,30 @@ export declare const pick: <T, K extends keyof T>(obj: T, ...keys: K[]) => Pick<
|
|
|
21
22
|
* Returns an object with only the differences between two input objects.
|
|
22
23
|
* Not recursive, only compares first level properties.
|
|
23
24
|
*/
|
|
24
|
-
export declare const getFirstLevelObjectPropertyDifferences: <
|
|
25
|
+
export declare const getFirstLevelObjectPropertyDifferences: <TRecord extends Record<PropertyKey, unknown>>(obj1: TRecord, obj2: TRecord) => Partial<TRecord>;
|
|
26
|
+
/**
|
|
27
|
+
* Replaces all null or undefined number values in a record with 0.
|
|
28
|
+
*
|
|
29
|
+
* @param record - The record to transform
|
|
30
|
+
* @returns {Record<string, number>} A new record with all null/undefined number values replaced with 0
|
|
31
|
+
* @example replaceNullableNumbersWithZero({ a: 1, b: null, c: undefined }) // { a: 1, b: 0, c: 0 }
|
|
32
|
+
*/
|
|
33
|
+
export declare const replaceNullableNumbersWithZero: <TRecord extends Record<PropertyKey, number | null | undefined>>(record: TRecord) => { [KeyItem in keyof TRecord]: number; };
|
|
34
|
+
/**
|
|
35
|
+
* Removes a property from an object in a type-safe way.
|
|
36
|
+
*
|
|
37
|
+
* @param obj - The object to remove a property from
|
|
38
|
+
* @param key - The key to remove
|
|
39
|
+
* @returns {object} A new object without the specified property
|
|
40
|
+
* @example removeProperty({ a: 1, b: 2 }, 'a') // { b: 2 }
|
|
41
|
+
*/
|
|
42
|
+
export declare const removeProperty: <TRecord extends Record<PropertyKey, unknown>, KeyItem extends keyof TRecord>(obj: TRecord, key: KeyItem) => MappedOmit<TRecord, KeyItem>;
|
|
43
|
+
/**
|
|
44
|
+
* Removes multiple properties from an object in a type-safe way.
|
|
45
|
+
*
|
|
46
|
+
* @param obj - The object to remove properties from
|
|
47
|
+
* @param keys - The keys to remove
|
|
48
|
+
* @returns {object} A new object without the specified properties
|
|
49
|
+
* @example removeProperties({ a: 1, b: 2, c: 3 }, ['a', 'b']) // { c: 3 }
|
|
50
|
+
*/
|
|
51
|
+
export declare const removeProperties: <TRecord extends Record<PropertyKey, unknown>, KeyItem extends keyof TRecord>(obj: TRecord, keys: readonly KeyItem[]) => MappedOmit<TRecord, KeyItem>;
|