@ptolemy2002/list-object-utils 1.2.1 → 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.
Files changed (3) hide show
  1. package/README.md +149 -1
  2. package/index.js +1 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,4 +1,152 @@
1
- # React Library CRA Project
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.
package/index.js CHANGED
@@ -156,5 +156,5 @@ function sortWithIndeces(toSort, sort = () => 0, descending = false) {
156
156
  return Array.from(toSort.keys()).sort((a, b) => sort(a, b) * (descending ? -1 : 1)).map((i) => toSort[i]);
157
157
  }
158
158
  function objectToString(obj) {
159
- return JSON.stringify(obj, (k, v) => v === void 0 ? null : v);
159
+ return JSON.stringify(obj, (_, v) => v === void 0 ? null : v);
160
160
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/list-object-utils",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js"