@ptolemy2002/list-object-utils 1.2.4 → 2.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.
- package/README.md +57 -53
- package/dist/index.d.ts +16 -0
- package/dist/index.js +186 -0
- package/package.json +13 -16
- package/index.js +0 -166
package/README.md
CHANGED
|
@@ -2,115 +2,122 @@
|
|
|
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
|
|
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` (
|
|
21
|
-
- `b` (
|
|
27
|
+
- `a` (`ArrayOptional`): The first list to be compared.
|
|
28
|
+
- `b` (`ArrayOptional`): The second list to be compared.
|
|
22
29
|
|
|
23
30
|
#### Returns
|
|
24
|
-
`
|
|
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
|
|
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` (
|
|
32
|
-
- `b` (
|
|
38
|
+
- `a` (`ObjectOptional`): The first object to be compared.
|
|
39
|
+
- `b` (`ObjectOptional`): The second object to be compared.
|
|
33
40
|
|
|
34
41
|
#### Returns
|
|
35
|
-
`
|
|
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` (
|
|
43
|
-
- `i` (
|
|
44
|
-
- `j` (
|
|
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
|
-
`
|
|
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` (
|
|
55
|
-
- `i` (
|
|
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
|
-
`
|
|
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` (
|
|
66
|
-
- `element` (
|
|
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
|
-
`
|
|
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` (
|
|
77
|
-
- `i` (
|
|
78
|
-
- `element` (
|
|
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
|
-
`
|
|
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` (
|
|
95
|
+
- `list` (`any[]`): The list to be checked.
|
|
89
96
|
|
|
90
97
|
#### Returns
|
|
91
|
-
`
|
|
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` (
|
|
99
|
-
- `b` (
|
|
105
|
+
- `a` (`ArrayOptional`): The first list to be compared.
|
|
106
|
+
- `b` (`ArrayOptional`): The second list to be compared.
|
|
100
107
|
|
|
101
108
|
#### Returns
|
|
102
|
-
`
|
|
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` (
|
|
110
|
-
- `b` (
|
|
116
|
+
- `a` (`ObjectOptional`): The first object to be compared.
|
|
117
|
+
- `b` (`ObjectOptional`): The second object to be compared.
|
|
111
118
|
|
|
112
119
|
#### Returns
|
|
113
|
-
`
|
|
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
122
|
### flattenKeys
|
|
116
123
|
#### Description
|
|
@@ -119,36 +126,33 @@ For an object that contains properties that are also objects, this function will
|
|
|
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` (
|
|
129
|
+
- `obj` (`Object`): 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
133
|
`Object` - 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` (
|
|
134
|
-
- `sort` (
|
|
135
|
-
- `descending` (
|
|
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
|
-
`
|
|
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
|
-
`
|
|
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
158
|
This project does not have any peer dependencies, so it should work out of the box.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type ArrayOptional = any[] | null | undefined;
|
|
2
|
+
type ObjectOptional = Object | null | undefined;
|
|
3
|
+
export declare function listsEqual(a: ArrayOptional, b: ArrayOptional): boolean;
|
|
4
|
+
export declare function objectsEqual(a: ObjectOptional, b: ObjectOptional): boolean;
|
|
5
|
+
export declare function listSwap<T>(list: T[], i: number, j: number): T[];
|
|
6
|
+
export declare function listRemove<T>(list: T[], i: number): T[];
|
|
7
|
+
export declare function listPush<T>(list: T[], item: T): T[];
|
|
8
|
+
export declare function listSet<T>(list: T[], i: number, item: T): T[];
|
|
9
|
+
export declare function isSet(list: any[]): boolean;
|
|
10
|
+
export declare function listDifference(a: ArrayOptional, b: ArrayOptional): ObjectOptional;
|
|
11
|
+
export declare function objectDifference(a: ObjectOptional, b: ObjectOptional): ObjectOptional;
|
|
12
|
+
export declare function flattenKeys(obj: Object, prefix?: string): Object;
|
|
13
|
+
type SortCallback = (a: number, b: number) => number;
|
|
14
|
+
export declare function sortWithIndeces<T>(toSort: T[], sort?: SortCallback, descending?: boolean): T[];
|
|
15
|
+
export declare function objectToString(obj: Object): string;
|
|
16
|
+
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": "
|
|
4
|
-
"
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
5
7
|
"files": [
|
|
6
|
-
"
|
|
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": "
|
|
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,5 @@
|
|
|
19
26
|
"release-minor": "bash ./scripts/release.sh minor",
|
|
20
27
|
"release-major": "bash ./scripts/release.sh major"
|
|
21
28
|
},
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
"url": "https://github.com/Ptolemy2002/list-object-utils",
|
|
25
|
-
"directory": "lib"
|
|
26
|
-
},
|
|
27
|
-
"description": "Utilities for working with lists and objects",
|
|
28
|
-
"license": "ISC",
|
|
29
|
-
"peerDependencies": {},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"esbuild": "^0.23.0"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
29
|
+
"devDependencies": {}
|
|
30
|
+
}
|
package/index.js
DELETED
|
@@ -1,166 +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 true;
|
|
98
|
-
if (!a || !b) return false;
|
|
99
|
-
if (a.length !== b.length) return false;
|
|
100
|
-
const result = {};
|
|
101
|
-
a.forEach((element, i) => {
|
|
102
|
-
if (b[i] !== element) {
|
|
103
|
-
if (Array.isArray(element) && Array.isArray(b[i])) {
|
|
104
|
-
result[i] = listDifference(element, b[i]);
|
|
105
|
-
if (result[i].length === 0) delete result[i];
|
|
106
|
-
} else if (typeof element === "object" && typeof b[i] === "object") {
|
|
107
|
-
result[i] = objectDifference(element, b[i]);
|
|
108
|
-
if (Object.keys(result[i]).length === 0) delete result[i];
|
|
109
|
-
} else {
|
|
110
|
-
result[i] = b[i];
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
if (b.length > a.length) {
|
|
115
|
-
for (let i = a.length; i < b.length; i++) {
|
|
116
|
-
result[i] = b[i];
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return result;
|
|
120
|
-
}
|
|
121
|
-
function objectDifference(a, b) {
|
|
122
|
-
if (a === b) return true;
|
|
123
|
-
if (!a || !b) return false;
|
|
124
|
-
if (Object.keys(a).length !== Object.keys(b).length) return false;
|
|
125
|
-
const result = {};
|
|
126
|
-
for (const key in a) {
|
|
127
|
-
if (b[key] !== a[key]) {
|
|
128
|
-
if (Array.isArray(a[key]) && Array.isArray(b[key])) {
|
|
129
|
-
result[key] = listDifference(a[key], b[key]);
|
|
130
|
-
} else if (typeof a[key] === "object" && typeof b[key] === "object") {
|
|
131
|
-
result[key] = objectDifference(a[key], b[key]);
|
|
132
|
-
} else {
|
|
133
|
-
result[key] = b[key];
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
for (const key in b) {
|
|
138
|
-
if (!a.hasOwnProperty(key)) {
|
|
139
|
-
result[key] = b[key];
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return result;
|
|
143
|
-
}
|
|
144
|
-
function flattenKeys(obj, prefix = "") {
|
|
145
|
-
const result = {};
|
|
146
|
-
for (const key in obj) {
|
|
147
|
-
if (Array.isArray(obj[key])) {
|
|
148
|
-
const flat = flattenKeys(obj[key], prefix + key + ".");
|
|
149
|
-
Object.assign(result, flat);
|
|
150
|
-
if (Object.keys(flat).length === 0) result[prefix + key] = obj[key];
|
|
151
|
-
} else if (typeof obj[key] === "object") {
|
|
152
|
-
const flat = flattenKeys(obj[key], prefix + key + ".");
|
|
153
|
-
Object.assign(result, flat);
|
|
154
|
-
if (Object.keys(flat).length === 0) result[prefix + key] = obj[key];
|
|
155
|
-
} else {
|
|
156
|
-
result[prefix + key] = obj[key];
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return result;
|
|
160
|
-
}
|
|
161
|
-
function sortWithIndeces(toSort, sort = () => 0, descending = false) {
|
|
162
|
-
return Array.from(toSort.keys()).sort((a, b) => sort(a, b) * (descending ? -1 : 1)).map((i) => toSort[i]);
|
|
163
|
-
}
|
|
164
|
-
function objectToString(obj) {
|
|
165
|
-
return JSON.stringify(obj, (_, v) => v === void 0 ? null : v);
|
|
166
|
-
}
|