some-common-functions-js 1.0.2 → 1.0.3
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/CHANGELOG.md +5 -3
- package/README.md +4 -21
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# Change log
|
|
2
2
|
|
|
3
|
-
## Version 1.
|
|
3
|
+
## Version 1.0.0 - 2025/11/19 - ITA
|
|
4
4
|
Genesis.
|
|
5
5
|
|
|
6
|
-
## Version 1.
|
|
6
|
+
## Version 1.0.1 - 2025/11/19 - ITA
|
|
7
7
|
Improve README.md documentation.
|
|
8
8
|
|
|
9
|
-
## Version 1.
|
|
9
|
+
## Version 1.0.2 - 2025/11/20 - ITA
|
|
10
10
|
- Improve README.md documentation and move documentation of changes to CHANGELOG.md
|
|
11
11
|
- Move more previously tested and used utility functions to this package. These are:
|
|
12
12
|
- compare(value1, value2, sortDir = 'asc')
|
|
@@ -16,3 +16,5 @@ Improve README.md documentation.
|
|
|
16
16
|
- getObjArrayWithNoDuplicates(objArray, firstOfDuplicates, ...comparisonFields)
|
|
17
17
|
- Add the "lodash" dependency used by some of these newly added functions.
|
|
18
18
|
|
|
19
|
+
## Version 1.0.4 - 2025/11/20 - ITA
|
|
20
|
+
- Improved the readability of README.me
|
package/README.md
CHANGED
|
@@ -43,7 +43,6 @@ let car = {
|
|
|
43
43
|
model: "Ranger",
|
|
44
44
|
year: "2015",
|
|
45
45
|
};
|
|
46
|
-
|
|
47
46
|
let result = hasOnly(car, "make", "model", "year");
|
|
48
47
|
// true, because car has *only all of the specified fields and no other fields.
|
|
49
48
|
|
|
@@ -56,14 +55,12 @@ result = hasOnly(car, "maxSpeed", "gvm", "power");
|
|
|
56
55
|
result = hasOnly(car, "make", "model");
|
|
57
56
|
// false, because car has fields other than the specified fields.
|
|
58
57
|
```
|
|
59
|
-
|
|
60
58
|
### `hasAll(anObject, ...fields)`
|
|
61
59
|
Returns `true` if the object contains **all** the specified fields.
|
|
62
60
|
The object may contain additional fields.
|
|
63
61
|
***Examples***
|
|
64
62
|
```
|
|
65
63
|
const { hasAll } = require("some-common-functions-js");
|
|
66
|
-
|
|
67
64
|
let car = {
|
|
68
65
|
make: "Ford",
|
|
69
66
|
model: "Ranger",
|
|
@@ -71,7 +68,6 @@ let car = {
|
|
|
71
68
|
power: "1000kW",
|
|
72
69
|
type: "pickup truck"
|
|
73
70
|
};
|
|
74
|
-
|
|
75
71
|
let result = hasAll(car, "make", "model");
|
|
76
72
|
// true, because car has all the specified fields.
|
|
77
73
|
|
|
@@ -110,6 +106,7 @@ Compares two values of the same primitive type, according to the sort direction.
|
|
|
110
106
|
* Sort directions: 'asc', 'desc'. Default is 'asc'.
|
|
111
107
|
|
|
112
108
|
***Examples***
|
|
109
|
+
```
|
|
113
110
|
const { compare } = require("some-common-functions-js");
|
|
114
111
|
|
|
115
112
|
let x = "Fong Kong";
|
|
@@ -117,10 +114,9 @@ let y = "Isaiah Tshabalala";
|
|
|
117
114
|
let result = compare(x, y); // -1 because "Fong Kong" is before "Isaiah Tshabalala" in ascending order.
|
|
118
115
|
result = compare(y, x);
|
|
119
116
|
console.log(result); // 1 because "Isaiah Tshabalala" is after "Fong Kong" in ascending order.
|
|
120
|
-
|
|
121
117
|
result = compare(x, y, 'desc');
|
|
122
118
|
console.log(result); // 1 because "Fong Kong" is after "Isaiah Tshabalala" in descending order.
|
|
123
|
-
|
|
119
|
+
```
|
|
124
120
|
### `binarySearch(anArray, searchVal, startFrom = 0, arraySortDir = 'asc')`
|
|
125
121
|
Binary Searches a sorted primitive type array for a value and returns the index.
|
|
126
122
|
* ArraySortDir specifies the direction in which the array is sorted (desc or asc).
|
|
@@ -132,14 +128,14 @@ Binary Searches a sorted primitive type array for a value and returns the index.
|
|
|
132
128
|
* Sort directions: 'asc', 'desc'. Default is 'asc'.
|
|
133
129
|
|
|
134
130
|
***Example***
|
|
131
|
+
```
|
|
135
132
|
const { binarySearch, compare } = require("some-common-functions-js");
|
|
136
133
|
let myArray = [100, 101, 102, 103, 104, 105, 106, 107];
|
|
137
134
|
let index = binarySearch(myArray, 103); // 3
|
|
138
135
|
let result = compare(103, myArray[index]); // 0, 103 === myArray[index].
|
|
139
|
-
|
|
140
136
|
index = binarySearch(myArray, 103, 4); // 4
|
|
141
137
|
result = compare(103, myArray[4]); // -1 meaning 103 is less than (before) myArray[4]
|
|
142
|
-
|
|
138
|
+
```
|
|
143
139
|
### `objCompare(obj1, obj2, ...comparisonFields)`
|
|
144
140
|
Compare 2 objects according to the comparison fields specified in the comparison fields, and return the result.
|
|
145
141
|
* Each each of the comparisonFields must be of the form 'fieldName sortDirection' or 'fieldName'.
|
|
@@ -150,7 +146,6 @@ Compare 2 objects according to the comparison fields specified in the comparison
|
|
|
150
146
|
***Example***
|
|
151
147
|
```
|
|
152
148
|
const { objCompare } = require('some-common-functions-js');
|
|
153
|
-
|
|
154
149
|
let anObject = {
|
|
155
150
|
firstName: "Isaiah",
|
|
156
151
|
lastName: "Tshabalala",
|
|
@@ -166,7 +161,6 @@ let anObject = {
|
|
|
166
161
|
}
|
|
167
162
|
}
|
|
168
163
|
};
|
|
169
|
-
|
|
170
164
|
let anObject2 = {
|
|
171
165
|
firstName: "Lindiwe",
|
|
172
166
|
lastName: "Tshabalala",
|
|
@@ -182,7 +176,6 @@ let anObject2 = {
|
|
|
182
176
|
}
|
|
183
177
|
}
|
|
184
178
|
};
|
|
185
|
-
|
|
186
179
|
let result = objCompare(anObject, anObject2, "lastName", "firstName");
|
|
187
180
|
// -1 because "Tshabalala Isaiah" is before "Tshabalala Lindiwe" according to ascending order.
|
|
188
181
|
|
|
@@ -204,7 +197,6 @@ let anObject3 = {
|
|
|
204
197
|
}
|
|
205
198
|
}
|
|
206
199
|
};
|
|
207
|
-
|
|
208
200
|
let objArray = [anObject2, anObject, anObject3];
|
|
209
201
|
// Using objCompare to sort objects in objArray using multiple fields including nested fields.
|
|
210
202
|
objArray.sort((obj1, obj2)=> {
|
|
@@ -219,7 +211,6 @@ objArray.sort((obj1, obj2)=> {
|
|
|
219
211
|
);
|
|
220
212
|
});
|
|
221
213
|
console.log(objArray);
|
|
222
|
-
|
|
223
214
|
/*
|
|
224
215
|
[
|
|
225
216
|
{
|
|
@@ -269,7 +260,6 @@ console.log(objArray);
|
|
|
269
260
|
}
|
|
270
261
|
]
|
|
271
262
|
*/
|
|
272
|
-
|
|
273
263
|
let teams = [
|
|
274
264
|
{
|
|
275
265
|
score: 85,
|
|
@@ -298,7 +288,6 @@ teams.sort((team1, team2) => {
|
|
|
298
288
|
});
|
|
299
289
|
console.log(teams);
|
|
300
290
|
/*
|
|
301
|
-
|
|
302
291
|
let teams = [
|
|
303
292
|
{
|
|
304
293
|
score: 85,
|
|
@@ -325,7 +314,6 @@ objArray.sort((team1, team2) => {
|
|
|
325
314
|
"numGames asc"
|
|
326
315
|
);
|
|
327
316
|
});
|
|
328
|
-
|
|
329
317
|
/*
|
|
330
318
|
[
|
|
331
319
|
{ score: 90, numGames: 10 },
|
|
@@ -354,14 +342,11 @@ let teamsArray = [
|
|
|
354
342
|
{ score: 85, numGames: 8 },
|
|
355
343
|
{ score: 85, numGames: 10 }
|
|
356
344
|
]; // Sorted by "score desc", "numGames asc".
|
|
357
|
-
|
|
358
|
-
|
|
359
345
|
let searchObj = { score: 85, numGames: 8 };
|
|
360
346
|
let anIndex = (commonFunctions.binarySearchObj(teamsArray, searchObj, "score desc", "numGames asc"));
|
|
361
347
|
|
|
362
348
|
let result = commonFunctions.objCompare(searchObj, teamsArray[anIndex], "score desc", "numGames asc"); // 0 -- an object with value { score: 85, numGames: 8} exists at teamsArray[anIndex];
|
|
363
349
|
```
|
|
364
|
-
|
|
365
350
|
## 4. `getObjArrayWithNoDuplicates(objArray, firstOfDuplicates, ...comparisonFields)`
|
|
366
351
|
Create an array of objects with duplicates eliminated. Taking only the first or last object from each duplicate set. The input array must be sorted according to the values comparisonFields.
|
|
367
352
|
* If firstOfDuplicates === true, then the first element in each set of duplicates is taken.
|
|
@@ -399,9 +384,7 @@ console.log(noDuplicatesArray); // Should contain only unique objects according
|
|
|
399
384
|
]
|
|
400
385
|
*/
|
|
401
386
|
```
|
|
402
|
-
|
|
403
387
|
---
|
|
404
|
-
|
|
405
388
|
## License
|
|
406
389
|
MIT
|
|
407
390
|
|