@iowas/toolpad 1.0.0

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.
@@ -0,0 +1,44 @@
1
+ // src/toolpad-utils/collections.ts
2
+ function asArray(maybeArray) {
3
+ return Array.isArray(maybeArray) ? maybeArray : [maybeArray];
4
+ }
5
+ function hasOwnProperty(obj, prop) {
6
+ return obj.hasOwnProperty(prop);
7
+ }
8
+ function mapProperties(obj, mapper) {
9
+ return Object.fromEntries(
10
+ Object.entries(obj).flatMap((entry) => {
11
+ const mapped = mapper(entry);
12
+ return mapped ? [mapped] : [];
13
+ })
14
+ );
15
+ }
16
+ function mapKeys(obj, mapper) {
17
+ return mapProperties(obj, ([key, value]) => [mapper(key), value]);
18
+ }
19
+ function mapValues(obj, mapper) {
20
+ return mapProperties(obj, ([key, value]) => [key, mapper(value, key)]);
21
+ }
22
+ function filterValues(obj, filter) {
23
+ return mapProperties(obj, ([key, value]) => filter(value) ? [key, value] : null);
24
+ }
25
+ function filterKeys(obj, filter) {
26
+ return mapProperties(obj, ([key, value]) => filter(key) ? [key, value] : null);
27
+ }
28
+ function equalProperties(obj1, obj2, subset) {
29
+ const keysToCheck = new Set(
30
+ subset != null ? subset : [...Object.keys(obj1), ...Object.keys(obj2)]
31
+ );
32
+ return Array.from(keysToCheck).every((key) => Object.is(obj1[key], obj2[key]));
33
+ }
34
+
35
+ export {
36
+ asArray,
37
+ hasOwnProperty,
38
+ mapProperties,
39
+ mapKeys,
40
+ mapValues,
41
+ filterValues,
42
+ filterKeys,
43
+ equalProperties
44
+ };