@ptolemy2002/list-object-utils 1.2.5 → 2.0.1

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/README.md CHANGED
@@ -2,156 +2,160 @@
2
2
  This library contains utilities for working with lists and objects in JavaScript.
3
3
 
4
4
  The functions are not exported as default, so you can import them in one of the following ways:
5
- ```
5
+ ```javascript
6
6
  // ES6
7
7
  import { functionName } from '@ptolemy2002/list-object-utils';
8
8
  // CommonJS
9
9
  const { functionName } = require('@ptolemy2002/list-object-utils');
10
10
  ```
11
11
 
12
+ ## Type Reference
13
+ ```typescript
14
+ type ArrayOptional = any[] | null | undefined;
15
+ type ObjectOptional = Object | null | undefined;
16
+ type SortCallback = (a: number, b: number) => number;
17
+ ```
18
+
12
19
  ## Functions
13
20
  The following functions are available in the library:
14
21
 
15
22
  ### listsEqual
16
23
  #### Description
17
- Checks if two lists are equal to each other. Uses recursion to check nested lists and the `objectsEqual` function to check nested objects. If either of the operands is not an array, the function will return `false`.
24
+ Checks if two lists are equal to each other. Uses recursion to check nested lists and the `objectsEqual` function to check nested objects. If either of the operands is not a list, the function will be equivalent to a simple equality check.
18
25
 
19
26
  #### Parameters
20
- - `a` (Array): The first list to be compared.
21
- - `b` (Array): The second list to be compared.
27
+ - `a` (`ArrayOptional`): The first list to be compared.
28
+ - `b` (`ArrayOptional`): The second list to be compared.
22
29
 
23
30
  #### Returns
24
- `Boolean` - `true` if the lists are equal, `false` otherwise.
31
+ `boolean` - `true` if the lists are equal, `false` otherwise.
25
32
 
26
33
  ### objectsEqual
27
34
  #### Description
28
- Checks if two objects are equal to each other. Uses recursion to check nested objects and the `listsEqual` function to check nested lists. If either of the operands is not an object, the function will return `false`. This check also applies to arrays.
35
+ Checks if two objects are equal to each other. Uses recursion to check nested objects and the `listsEqual` function to check nested lists. If either of the operands is not an object, the function will be equivalent to a simple equality check.
29
36
 
30
37
  #### Parameters
31
- - `a` (Object): The first object to be compared.
32
- - `b` (Object): The second object to be compared.
38
+ - `a` (`ObjectOptional`): The first object to be compared.
39
+ - `b` (`ObjectOptional`): The second object to be compared.
33
40
 
34
41
  #### Returns
35
- `Boolean` - `true` if the objects are equal, `false` otherwise.
42
+ `boolean` - `true` if the objects are equal, `false` otherwise.
36
43
 
37
- ### listSwap
44
+ ### listSwap<T>
38
45
  #### Description
39
- Swaps two elements in a list based on the provided indices.
46
+ Swaps two elements in a list based on the provided indices. Throws a `TypeError` if the indices are not integers and a `RangeError` if the indices are out of bounds.
40
47
 
41
48
  #### Parameters
42
- - `list` (Array): The list in which the elements are to be swapped.
43
- - `i` (Number): The index of the first element to be swapped.
44
- - `j` (Number): The index of the second element to be swapped.
49
+ - `list` (`T[]`): The list in which the elements are to be swapped.
50
+ - `i` (`number`): The index of the first element to be swapped.
51
+ - `j` (`number`): The index of the second element to be swapped.
45
52
 
46
53
  #### Returns
47
- `Array` - The list with the elements swapped.
54
+ `T[]` - The list with the elements swapped.
48
55
 
49
- ### listRemove
56
+ ### listRemove<T>
50
57
  #### Description
51
- Removes an element from a list based on the provided index.
58
+ Removes an element from a list based on the provided index. Throws a `TypeError` if the index is not an integer and a `RangeError` if the index is out of bounds.
52
59
 
53
60
  #### Parameters
54
- - `list` (Array): The list from which the element is to be removed.
55
- - `i` (Number): The index of the element to be removed.
61
+ - `list` (`T[]`): The list from which the element is to be removed.
62
+ - `i` (`number`): The index of the element to be removed.
56
63
 
57
64
  #### Returns
58
- `Array` - The list with the element removed.
65
+ `T[]` - The list with the element removed.
59
66
 
60
- ### listPush
67
+ ### listPush<T>
61
68
  #### Description
62
69
  Pushes an element to a list based on the provided index.
63
70
 
64
71
  #### Parameters
65
- - `list` (Array): The list to which the element is to be pushed.
66
- - `element` (Any): The element to be pushed.
72
+ - `list` (`T[]`): The list to which the element is to be pushed.
73
+ - `element` (`T`): The element to be pushed.
67
74
 
68
75
  #### Returns
69
- `Array` - The list with the element pushed.
76
+ `T[]` - The list with the element pushed.
70
77
 
71
- ### listSet
78
+ ### listSet<T>
72
79
  #### Description
73
- Sets an element in a list based on the provided index.
80
+ Sets an element in a list based on the provided index. Throws a `TypeError` if the index is not an integer and a `RangeError` if the index is out of bounds.
74
81
 
75
82
  #### Parameters
76
- - `list` (Array): The list in which the element is to be set.
77
- - `i` (Number): The index of the element to be set.
78
- - `element` (Any): The element to be set.
83
+ - `list` (`T[]`): The list in which the element is to be set.
84
+ - `i` (`number`): The index of the element to be set.
85
+ - `element` (`T`): The element to be set.
79
86
 
80
87
  #### Returns
81
- `Array` - The list with the element set.
88
+ `T[]` - The list with the element set.
82
89
 
83
90
  ### isSet
84
91
  #### Description
85
- Checks if a list is a set, that is, it has no duplicate elements.
92
+ Checks if a list is a set, that is, it has no duplicate elements. Element identity is determined using `Array.includes`.
86
93
 
87
94
  #### Parameters
88
- - `list` (Array): The list to be checked.
95
+ - `list` (`any[]`): The list to be checked.
89
96
 
90
97
  #### Returns
91
- `Boolean` - `true` if the list is a set, `false` otherwise.
98
+ `boolean` - `true` if the list is a set, `false` otherwise.
92
99
 
93
100
  ### listDifference
94
101
  #### Description
95
- Finds the difference between two lists, that is, the elements that do not match between the two lists. If one list has a different value at an index than the other list at the same index, the element from the second list will be included in the result. Uses recursion to check nested lists and the `objectDifference` function to check nested objects.
102
+ Finds the difference between two lists, that is, the elements that do not match between the two lists. If one list has a different value at an index than the other list at the same index, the element from the second list will be included in the result. Uses recursion to check nested lists and the `objectDifference` function to check nested objects. If either of the operands is not a list, the function will return an empty object if the operands are equal and the second operand if they are not.
96
103
 
97
104
  #### Parameters
98
- - `a` (Array): The first list to be compared.
99
- - `b` (Array): The second list to be compared.
105
+ - `a` (`ArrayOptional`): The first list to be compared.
106
+ - `b` (`ArrayOptional`): The second list to be compared.
100
107
 
101
108
  #### Returns
102
- `Object` - An object with a property for every index at which the two lists differ. The value for each property is the element from the second list.
109
+ `ObjectOptional` - If the operands are both lists, an object with a property for every index at which the two lists differ. The value for each property is the element from the second list. If the operands are not both lists and equal, an empty object. If the operands are not both lists and not equal, the second operand.
103
110
 
104
111
  ### objectDifference
105
112
  #### Description
106
- Finds the difference between two objects, that is, the properties that do not match between the two objects. If one object has a different value for a property than the other object for the same property, the value from the second object will be included in the result. Uses recursion to check nested objects and the `listDifference` function to check nested lists.
113
+ Finds the difference between two objects, that is, the properties that do not match between the two objects. If one object has a different value for a property than the other object for the same property, the value from the second object will be included in the result. Uses recursion to check nested objects and the `listDifference` function to check nested lists. If either of the operands is not an object, the function will return an empty object if the operands are equal and the second operand if they are not.
107
114
 
108
115
  #### Parameters
109
- - `a` (Object): The first object to be compared.
110
- - `b` (Object): The second object to be compared.
116
+ - `a` (`ObjectOptional`): The first object to be compared.
117
+ - `b` (`ObjectOptional`): The second object to be compared.
111
118
 
112
119
  #### Returns
113
- `Object` - An object with a property for every property that differs between the two objects. The value for each property is the value from the second object.
120
+ `ObjectOptional` - If the operands are both objects, an object with a property for every key at which the two objects differ. The value for each property is the value from the second object. If the operands are not both objects and equal, an empty object. If the operands are not both objects and not equal, the second operand.
114
121
 
115
- ### flattenKeys
122
+ ### flattenKeys<T>
116
123
  #### Description
117
124
  For an object that contains properties that are also objects, this function will flatten the keys of the object to a single level. The keys will be concatenated with a period (`.`) between them. Also does this with lists. Uses recursion to flatten nested objects.
118
125
 
119
126
  Note that empty lists and empty objects are left as-is.
120
127
 
121
128
  #### Parameters
122
- - `obj` (Object): The object to be flattened.
123
- - `prefix` (String): The prefix to be added to the keys. Default is an empty string.
129
+ - `obj` (`T`): The object to be flattened.
130
+ - `prefix` (`string`): The prefix to be added to the keys. Default is an empty string.
124
131
 
125
132
  #### Returns
126
- `Object` - The object with the keys flattened.
133
+ `Record<string, ValueOf<T>>` - The object with the keys flattened.
127
134
 
128
- ### sortWithIndices
135
+ ### sortWithIndices<T>
129
136
  #### Description
130
137
  Sorts a list based on the callback provided. The difference between this function and the native `sort` function is that this function specifies the indices of the elements to the callback instead of the elements themselves.
131
138
 
132
139
  #### Parameters
133
- - `list` (Array): The list to be sorted.
134
- - `sort` (Function): The callback function that will be used to sort the list. It should take two arguments, which are the indices of the elements to be compared.
135
- - `descending` (Boolean): If `true`, the list will be sorted in descending order. Default is `false`.
140
+ - `list` (`T[]`): The list to be sorted.
141
+ - `sort` (`SortCallback`): The callback function that will be used to sort the list. It should take two arguments, which are the indices of the elements to be compared.
142
+ - `descending` (`boolean`): If `true`, the list will be sorted in descending order. Default is `false`.
136
143
 
137
144
  #### Returns
138
- `Array` - The sorted list.
145
+ `T[]` - The list sorted based on the callback and the `descending` parameter.
139
146
 
140
147
  ### objectToString
141
148
  #### Description
142
149
  A simple implementation of `JSON.stringify` that converts undefined values to `null`.
143
150
 
144
151
  #### Parameters
145
- - `obj` (Object): The object to be converted to a string.
152
+ - `obj` (`Object`): The object to be converted to a string.
146
153
 
147
154
  #### Returns
148
- `String` - The string representation of the object.
149
-
150
- ## Meta
151
- This is a React Library Created by Ptolemy2002's [cra-template-react-library](https://www.npmjs.com/package/@ptolemy2002/cra-template-react-library) template in combination with [create-react-app](https://www.npmjs.com/package/create-react-app). However, it does not actually depend on React - it has been modified to work out of the box. It contains methods of building and publishing your library to npm.
155
+ `string` - The string representation of the object.
152
156
 
153
157
  ## Peer Dependencies
154
- This project does not have any peer dependencies, so it should work out of the box.
158
+ - @ptolemy2002/ts-utils^1.2.0
155
159
 
156
160
  ## Commands
157
161
  The following commands exist in the project:
@@ -0,0 +1,17 @@
1
+ import { ValueOf } from "@ptolemy2002/ts-utils";
2
+ type ArrayOptional = any[] | null | undefined;
3
+ type ObjectOptional = Object | null | undefined;
4
+ export declare function listsEqual(a: ArrayOptional, b: ArrayOptional): boolean;
5
+ export declare function objectsEqual(a: ObjectOptional, b: ObjectOptional): boolean;
6
+ export declare function listSwap<T>(list: T[], i: number, j: number): T[];
7
+ export declare function listRemove<T>(list: T[], i: number): T[];
8
+ export declare function listPush<T>(list: T[], item: T): T[];
9
+ export declare function listSet<T>(list: T[], i: number, item: T): T[];
10
+ export declare function isSet(list: any[]): boolean;
11
+ export declare function listDifference(a: ArrayOptional, b: ArrayOptional): ObjectOptional;
12
+ export declare function objectDifference(a: ObjectOptional, b: ObjectOptional): ObjectOptional;
13
+ export declare function flattenKeys<T>(obj: T, prefix?: string): Record<string, ValueOf<T>>;
14
+ type SortCallback = (a: number, b: number) => number;
15
+ export declare function sortWithIndeces<T>(toSort: T[], sort?: SortCallback, descending?: boolean): T[];
16
+ export declare function objectToString(obj: Object): string;
17
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listsEqual = listsEqual;
4
+ exports.objectsEqual = objectsEqual;
5
+ exports.listSwap = listSwap;
6
+ exports.listRemove = listRemove;
7
+ exports.listPush = listPush;
8
+ exports.listSet = listSet;
9
+ exports.isSet = isSet;
10
+ exports.listDifference = listDifference;
11
+ exports.objectDifference = objectDifference;
12
+ exports.flattenKeys = flattenKeys;
13
+ exports.sortWithIndeces = sortWithIndeces;
14
+ exports.objectToString = objectToString;
15
+ function listsEqual(a, b) {
16
+ if (a === b)
17
+ return true;
18
+ if (!a || !b)
19
+ return false;
20
+ if (a.length !== b.length)
21
+ return false;
22
+ for (let i = 0; i < a.length; ++i) {
23
+ if (Array.isArray(a[i]) && Array.isArray(b[i])) {
24
+ if (!listsEqual(a[i], b[i]))
25
+ return false;
26
+ }
27
+ else if (typeof a[i] === "object" && typeof b[i] === "object") {
28
+ if (!objectsEqual(a[i], b[i]))
29
+ return false;
30
+ }
31
+ else if (a[i] !== b[i]) {
32
+ return false;
33
+ }
34
+ }
35
+ return true;
36
+ }
37
+ function objectsEqual(a, b) {
38
+ if (a === b)
39
+ return true;
40
+ if (!a || !b)
41
+ return false;
42
+ if (Object.keys(a).length !== Object.keys(b).length)
43
+ return false;
44
+ for (const key in a) {
45
+ if (Array.isArray(a[key]) && Array.isArray(b[key])) {
46
+ if (!listsEqual(a[key], b[key]))
47
+ return false;
48
+ }
49
+ else if (typeof a[key] === "object" && typeof b[key] === "object") {
50
+ if (!objectsEqual(a[key], b[key]))
51
+ return false;
52
+ }
53
+ else if (a[key] !== b[key]) {
54
+ return false;
55
+ }
56
+ }
57
+ return true;
58
+ }
59
+ function listSwap(list, i, j) {
60
+ if (i !== Math.floor(i))
61
+ throw new TypeError("i must be an integer");
62
+ if (j !== Math.floor(j))
63
+ throw new TypeError("j must be an integer");
64
+ if (i < 0 || i >= list.length)
65
+ throw new RangeError("i out of bounds");
66
+ if (j < 0 || j >= list.length)
67
+ throw new RangeError("j out of bounds");
68
+ const newList = [...list];
69
+ const temp = newList[i];
70
+ newList[i] = newList[j];
71
+ newList[j] = temp;
72
+ return newList;
73
+ }
74
+ function listRemove(list, i) {
75
+ if (i !== Math.floor(i))
76
+ throw new TypeError("i must be an integer");
77
+ if (i < 0 || i >= list.length)
78
+ throw new RangeError("i out of bounds");
79
+ const newList = [...list];
80
+ newList.splice(i, 1);
81
+ return newList;
82
+ }
83
+ function listPush(list, item) {
84
+ return [...list, item];
85
+ }
86
+ function listSet(list, i, item) {
87
+ if (i !== Math.floor(i))
88
+ throw new TypeError("i must be an integer");
89
+ if (i < 0 || i >= list.length)
90
+ throw new RangeError("i out of bounds");
91
+ const newList = [...list];
92
+ newList[i] = item;
93
+ return newList;
94
+ }
95
+ function isSet(list) {
96
+ const seen = [];
97
+ for (let i = 0; i < list.length; i++) {
98
+ if (seen.includes(list[i]))
99
+ return false;
100
+ seen.push(list[i]);
101
+ }
102
+ return true;
103
+ }
104
+ function listDifference(a, b) {
105
+ if (a === b)
106
+ return {};
107
+ if (!a || !b)
108
+ return b;
109
+ const result = {};
110
+ a.forEach((element, i) => {
111
+ if (b[i] !== element) {
112
+ if (Array.isArray(element) && Array.isArray(b[i])) {
113
+ result[i] = listDifference(element, b[i]);
114
+ if (result[i].length === 0)
115
+ delete result[i];
116
+ }
117
+ else if (typeof element === "object" && typeof b[i] === "object") {
118
+ result[i] = objectDifference(element, b[i]);
119
+ if (Object.keys(result[i]).length === 0)
120
+ delete result[i];
121
+ }
122
+ else {
123
+ result[i] = b[i];
124
+ }
125
+ }
126
+ });
127
+ if (b.length > a.length) {
128
+ for (let i = a.length; i < b.length; i++) {
129
+ result[i] = b[i];
130
+ }
131
+ }
132
+ return result;
133
+ }
134
+ function objectDifference(a, b) {
135
+ if (a === b)
136
+ return {};
137
+ if (!a || !b)
138
+ return b;
139
+ const result = {};
140
+ for (const key in a) {
141
+ if (b[key] !== a[key]) {
142
+ if (Array.isArray(a[key]) && Array.isArray(b[key])) {
143
+ result[key] = listDifference(a[key], b[key]);
144
+ }
145
+ else if (typeof a[key] === "object" && typeof b[key] === "object") {
146
+ result[key] = objectDifference(a[key], b[key]);
147
+ }
148
+ else {
149
+ result[key] = b[key];
150
+ }
151
+ }
152
+ }
153
+ for (const key in b) {
154
+ if (!a.hasOwnProperty(key)) {
155
+ result[key] = b[key];
156
+ }
157
+ }
158
+ return result;
159
+ }
160
+ function flattenKeys(obj, prefix = "") {
161
+ const result = {};
162
+ for (const key in obj) {
163
+ if (Array.isArray(obj[key])) {
164
+ const flat = flattenKeys(obj[key], prefix + key + ".");
165
+ Object.assign(result, flat);
166
+ if (Object.keys(flat).length === 0)
167
+ result[prefix + key] = obj[key];
168
+ }
169
+ else if (typeof obj[key] === "object") {
170
+ const flat = flattenKeys(obj[key], prefix + key + ".");
171
+ Object.assign(result, flat);
172
+ if (Object.keys(flat).length === 0)
173
+ result[prefix + key] = obj[key];
174
+ }
175
+ else {
176
+ result[prefix + key] = obj[key];
177
+ }
178
+ }
179
+ return result;
180
+ }
181
+ function sortWithIndeces(toSort, sort = () => 0, descending = false) {
182
+ return Array.from(toSort.keys()).sort((a, b) => sort(a, b) * (descending ? -1 : 1)).map((i) => toSort[i]);
183
+ }
184
+ function objectToString(obj) {
185
+ return JSON.stringify(obj, (_, v) => v === undefined ? null : v);
186
+ }
package/package.json CHANGED
@@ -1,12 +1,19 @@
1
1
  {
2
2
  "name": "@ptolemy2002/list-object-utils",
3
- "version": "1.2.5",
4
- "main": "index.js",
3
+ "version": "2.0.1",
4
+ "private": false,
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
5
7
  "files": [
6
- "index.js"
8
+ "/dist"
7
9
  ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/Ptolemy2002/list-object-utils",
13
+ "directory": "lib"
14
+ },
8
15
  "scripts": {
9
- "build": "esbuild src/index.js --bundle --format=cjs --outfile=index.js --loader:.js=jsx --external:is-callable",
16
+ "build": "tsc --project ./tsconfig.json",
10
17
  "postinstall": "npx typesync",
11
18
  "uninstall": "bash ./scripts/uninstall.sh",
12
19
  "reinstall": "bash ./scripts/reinstall.sh",
@@ -19,15 +26,10 @@
19
26
  "release-minor": "bash ./scripts/release.sh minor",
20
27
  "release-major": "bash ./scripts/release.sh major"
21
28
  },
22
- "repository": {
23
- "type": "git",
24
- "url": "https://github.com/Ptolemy2002/list-object-utils",
25
- "directory": "lib"
29
+ "peerDependencies": {
30
+ "@ptolemy2002/ts-utils": "^1.2.0"
26
31
  },
27
- "description": "Utilities for working with lists and objects",
28
- "license": "ISC",
29
- "peerDependencies": {},
30
32
  "devDependencies": {
31
- "esbuild": "^0.23.0"
33
+ "@ptolemy2002/ts-utils": "^1.2.0"
32
34
  }
33
35
  }
package/index.js DELETED
@@ -1,164 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/index.js
20
- var src_exports = {};
21
- __export(src_exports, {
22
- flattenKeys: () => flattenKeys,
23
- isSet: () => isSet,
24
- listDifference: () => listDifference,
25
- listPush: () => listPush,
26
- listRemove: () => listRemove,
27
- listSet: () => listSet,
28
- listSwap: () => listSwap,
29
- listsEqual: () => listsEqual,
30
- objectDifference: () => objectDifference,
31
- objectToString: () => objectToString,
32
- objectsEqual: () => objectsEqual,
33
- sortWithIndeces: () => sortWithIndeces
34
- });
35
- module.exports = __toCommonJS(src_exports);
36
- function listsEqual(a, b) {
37
- if (!Array.isArray(a) || !Array.isArray(b)) return false;
38
- if (a === b) return true;
39
- if (!a || !b) return false;
40
- if (a.length !== b.length) return false;
41
- for (let i = 0; i < a.length; ++i) {
42
- if (Array.isArray(a[i]) && Array.isArray(b[i])) {
43
- if (!listsEqual(a[i], b[i])) return false;
44
- } else if (typeof a[i] === "object" && typeof b[i] === "object") {
45
- if (!objectsEqual(a[i], b[i])) return false;
46
- } else if (a[i] !== b[i]) {
47
- return false;
48
- }
49
- }
50
- return true;
51
- }
52
- function objectsEqual(a, b) {
53
- if (!(typeof a === "object" && !Array.isArray(a)) || !(typeof b === "object" && !Array.isArray(b))) return false;
54
- if (a === b) return true;
55
- if (!a || !b) return false;
56
- if (Object.keys(a).length !== Object.keys(b).length) return false;
57
- for (const key in a) {
58
- if (Array.isArray(a[key]) && Array.isArray(b[key])) {
59
- if (!listsEqual(a[key], b[key])) return false;
60
- } else if (typeof a[key] === "object" && typeof b[key] === "object") {
61
- if (!objectsEqual(a[key], b[key])) return false;
62
- } else if (a[key] !== b[key]) {
63
- return false;
64
- }
65
- }
66
- return true;
67
- }
68
- function listSwap(list, i, j) {
69
- const newList = [...list];
70
- const temp = newList[i];
71
- newList[i] = newList[j];
72
- newList[j] = temp;
73
- return newList;
74
- }
75
- function listRemove(list, i) {
76
- const newList = [...list];
77
- newList.splice(i, 1);
78
- return newList;
79
- }
80
- function listPush(list, element) {
81
- return [...list, element];
82
- }
83
- function listSet(list, i, element) {
84
- const newList = [...list];
85
- newList[i] = element;
86
- return newList;
87
- }
88
- function isSet(list) {
89
- const seen = [];
90
- for (let i = 0; i < list.length; i++) {
91
- if (seen.includes(list[i])) return false;
92
- seen.push(list[i]);
93
- }
94
- return true;
95
- }
96
- function listDifference(a, b) {
97
- if (a === b) return {};
98
- if (!a || !b) return b;
99
- const result = {};
100
- a.forEach((element, i) => {
101
- if (b[i] !== element) {
102
- if (Array.isArray(element) && Array.isArray(b[i])) {
103
- result[i] = listDifference(element, b[i]);
104
- if (result[i].length === 0) delete result[i];
105
- } else if (typeof element === "object" && typeof b[i] === "object") {
106
- result[i] = objectDifference(element, b[i]);
107
- if (Object.keys(result[i]).length === 0) delete result[i];
108
- } else {
109
- result[i] = b[i];
110
- }
111
- }
112
- });
113
- if (b.length > a.length) {
114
- for (let i = a.length; i < b.length; i++) {
115
- result[i] = b[i];
116
- }
117
- }
118
- return result;
119
- }
120
- function objectDifference(a, b) {
121
- if (a === b) return {};
122
- if (!a || !b) return b;
123
- const result = {};
124
- for (const key in a) {
125
- if (b[key] !== a[key]) {
126
- if (Array.isArray(a[key]) && Array.isArray(b[key])) {
127
- result[key] = listDifference(a[key], b[key]);
128
- } else if (typeof a[key] === "object" && typeof b[key] === "object") {
129
- result[key] = objectDifference(a[key], b[key]);
130
- } else {
131
- result[key] = b[key];
132
- }
133
- }
134
- }
135
- for (const key in b) {
136
- if (!a.hasOwnProperty(key)) {
137
- result[key] = b[key];
138
- }
139
- }
140
- return result;
141
- }
142
- function flattenKeys(obj, prefix = "") {
143
- const result = {};
144
- for (const key in obj) {
145
- if (Array.isArray(obj[key])) {
146
- const flat = flattenKeys(obj[key], prefix + key + ".");
147
- Object.assign(result, flat);
148
- if (Object.keys(flat).length === 0) result[prefix + key] = obj[key];
149
- } else if (typeof obj[key] === "object") {
150
- const flat = flattenKeys(obj[key], prefix + key + ".");
151
- Object.assign(result, flat);
152
- if (Object.keys(flat).length === 0) result[prefix + key] = obj[key];
153
- } else {
154
- result[prefix + key] = obj[key];
155
- }
156
- }
157
- return result;
158
- }
159
- function sortWithIndeces(toSort, sort = () => 0, descending = false) {
160
- return Array.from(toSort.keys()).sort((a, b) => sort(a, b) * (descending ? -1 : 1)).map((i) => toSort[i]);
161
- }
162
- function objectToString(obj) {
163
- return JSON.stringify(obj, (_, v) => v === void 0 ? null : v);
164
- }