@ptolemy2002/list-object-utils 1.2.0 → 1.2.2
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 +150 -4
- package/index.js +1 -12
- package/package.json +2 -5
package/README.md
CHANGED
|
@@ -1,12 +1,158 @@
|
|
|
1
|
-
#
|
|
1
|
+
# List Object Utils
|
|
2
|
+
This library contains utilities for working with lists and objects in JavaScript.
|
|
3
|
+
|
|
4
|
+
The functions are not exported as default, so you can import them in one of the following ways:
|
|
5
|
+
```
|
|
6
|
+
// ES6
|
|
7
|
+
import { functionName } from '@ptolemy2002/list-object-utils';
|
|
8
|
+
// CommonJS
|
|
9
|
+
const { functionName } = require('@ptolemy2002/list-object-utils');
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Functions
|
|
13
|
+
The following functions are available in the library:
|
|
14
|
+
|
|
15
|
+
### listsEqual
|
|
16
|
+
#### 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`.
|
|
18
|
+
|
|
19
|
+
#### Parameters
|
|
20
|
+
- `a` (Array): The first list to be compared.
|
|
21
|
+
- `b` (Array): The second list to be compared.
|
|
22
|
+
|
|
23
|
+
#### Returns
|
|
24
|
+
`Boolean` - `true` if the lists are equal, `false` otherwise.
|
|
25
|
+
|
|
26
|
+
### objectsEqual
|
|
27
|
+
#### 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.
|
|
29
|
+
|
|
30
|
+
#### Parameters
|
|
31
|
+
- `a` (Object): The first object to be compared.
|
|
32
|
+
- `b` (Object): The second object to be compared.
|
|
33
|
+
|
|
34
|
+
#### Returns
|
|
35
|
+
`Boolean` - `true` if the objects are equal, `false` otherwise.
|
|
36
|
+
|
|
37
|
+
### listSwap
|
|
38
|
+
#### Description
|
|
39
|
+
Swaps two elements in a list based on the provided indices.
|
|
40
|
+
|
|
41
|
+
#### 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.
|
|
45
|
+
|
|
46
|
+
#### Returns
|
|
47
|
+
`Array` - The list with the elements swapped.
|
|
48
|
+
|
|
49
|
+
### listRemove
|
|
50
|
+
#### Description
|
|
51
|
+
Removes an element from a list based on the provided index.
|
|
52
|
+
|
|
53
|
+
#### 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.
|
|
56
|
+
|
|
57
|
+
#### Returns
|
|
58
|
+
`Array` - The list with the element removed.
|
|
59
|
+
|
|
60
|
+
### listPush
|
|
61
|
+
#### Description
|
|
62
|
+
Pushes an element to a list based on the provided index.
|
|
63
|
+
|
|
64
|
+
#### Parameters
|
|
65
|
+
- `list` (Array): The list to which the element is to be pushed.
|
|
66
|
+
- `element` (Any): The element to be pushed.
|
|
67
|
+
|
|
68
|
+
#### Returns
|
|
69
|
+
`Array` - The list with the element pushed.
|
|
70
|
+
|
|
71
|
+
### listSet
|
|
72
|
+
#### Description
|
|
73
|
+
Sets an element in a list based on the provided index.
|
|
74
|
+
|
|
75
|
+
#### 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.
|
|
79
|
+
|
|
80
|
+
#### Returns
|
|
81
|
+
`Array` - The list with the element set.
|
|
82
|
+
|
|
83
|
+
### isSet
|
|
84
|
+
#### Description
|
|
85
|
+
Checks if a list is a set, that is, it has no duplicate elements.
|
|
86
|
+
|
|
87
|
+
#### Parameters
|
|
88
|
+
- `list` (Array): The list to be checked.
|
|
89
|
+
|
|
90
|
+
#### Returns
|
|
91
|
+
`Boolean` - `true` if the list is a set, `false` otherwise.
|
|
92
|
+
|
|
93
|
+
### listDifference
|
|
94
|
+
#### 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.
|
|
96
|
+
|
|
97
|
+
#### Parameters
|
|
98
|
+
- `a` (Array): The first list to be compared.
|
|
99
|
+
- `b` (Array): The second list to be compared.
|
|
100
|
+
|
|
101
|
+
#### 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.
|
|
103
|
+
|
|
104
|
+
### objectDifference
|
|
105
|
+
#### 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.
|
|
107
|
+
|
|
108
|
+
#### Parameters
|
|
109
|
+
- `a` (Object): The first object to be compared.
|
|
110
|
+
- `b` (Object): The second object to be compared.
|
|
111
|
+
|
|
112
|
+
#### 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.
|
|
114
|
+
|
|
115
|
+
### flattenKeys
|
|
116
|
+
#### Description
|
|
117
|
+
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
|
+
|
|
119
|
+
Note that empty lists and empty objects are left as-is.
|
|
120
|
+
|
|
121
|
+
#### 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.
|
|
124
|
+
|
|
125
|
+
#### Returns
|
|
126
|
+
`Object` - The object with the keys flattened.
|
|
127
|
+
|
|
128
|
+
### sortWithIndices
|
|
129
|
+
#### Description
|
|
130
|
+
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
|
+
|
|
132
|
+
#### 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`.
|
|
136
|
+
|
|
137
|
+
#### Returns
|
|
138
|
+
`Array` - The sorted list.
|
|
139
|
+
|
|
140
|
+
### objectToString
|
|
141
|
+
#### Description
|
|
142
|
+
A simple implementation of `JSON.stringify` that converts undefined values to `null`.
|
|
143
|
+
|
|
144
|
+
#### Parameters
|
|
145
|
+
- `obj` (Object): The object to be converted to a string.
|
|
146
|
+
|
|
147
|
+
#### Returns
|
|
148
|
+
`String` - The string representation of the object.
|
|
149
|
+
|
|
2
150
|
## Meta
|
|
3
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). It contains methods of building and publishing your library to npm.
|
|
4
152
|
For now, the library makes use of React 18 and does not use TypeScript.
|
|
5
153
|
|
|
6
154
|
## Peer Dependencies
|
|
7
|
-
|
|
8
|
-
- @types/is-callable: ^1.1.2
|
|
9
|
-
- is-callable: ^1.2.7
|
|
155
|
+
This project does not have any peer dependencies, so it should work out of the box.
|
|
10
156
|
|
|
11
157
|
## Commands
|
|
12
158
|
The following commands exist in the project:
|
package/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
1
|
var __defProp = Object.defineProperty;
|
|
3
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
5
|
var __export = (target, all) => {
|
|
8
6
|
for (var name in all)
|
|
@@ -16,14 +14,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
14
|
}
|
|
17
15
|
return to;
|
|
18
16
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
18
|
|
|
29
19
|
// src/index.js
|
|
@@ -43,7 +33,6 @@ __export(src_exports, {
|
|
|
43
33
|
sortWithIndeces: () => sortWithIndeces
|
|
44
34
|
});
|
|
45
35
|
module.exports = __toCommonJS(src_exports);
|
|
46
|
-
var import_is_callable = __toESM(require("is-callable"));
|
|
47
36
|
function listsEqual(a, b) {
|
|
48
37
|
if (!Array.isArray(a) || !Array.isArray(b)) return false;
|
|
49
38
|
if (a === b) return true;
|
|
@@ -167,5 +156,5 @@ function sortWithIndeces(toSort, sort = () => 0, descending = false) {
|
|
|
167
156
|
return Array.from(toSort.keys()).sort((a, b) => sort(a, b) * (descending ? -1 : 1)).map((i) => toSort[i]);
|
|
168
157
|
}
|
|
169
158
|
function objectToString(obj) {
|
|
170
|
-
return JSON.stringify(obj, (
|
|
159
|
+
return JSON.stringify(obj, (_, v) => v === void 0 ? null : v);
|
|
171
160
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ptolemy2002/list-object-utils",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"index.js"
|
|
@@ -26,10 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"description": "Utilities for working with lists and objects",
|
|
28
28
|
"license": "ISC",
|
|
29
|
-
"peerDependencies": {
|
|
30
|
-
"@types/is-callable": "~1.1.2",
|
|
31
|
-
"is-callable": "^1.2.7"
|
|
32
|
-
},
|
|
29
|
+
"peerDependencies": {},
|
|
33
30
|
"devDependencies": {
|
|
34
31
|
"esbuild": "^0.23.0"
|
|
35
32
|
}
|