some-common-functions-js 1.0.2 → 1.0.5
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 +11 -3
- package/README.md +6 -49
- 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,11 @@ 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
|
|
21
|
+
|
|
22
|
+
## Version 1.05 - 2025/11/21 - ITA
|
|
23
|
+
- Corrected README documentation.
|
|
24
|
+
|
|
25
|
+
## Version 1.06 - 2025/11/21 - ITA
|
|
26
|
+
- Updated package.json version in lieu of changes. Otherwise the updated package won't be published on npm.
|
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,
|
|
@@ -288,7 +278,8 @@ let teams = [
|
|
|
288
278
|
numGames: 10
|
|
289
279
|
}
|
|
290
280
|
];
|
|
291
|
-
// Using objCompare to sort fields where there
|
|
281
|
+
// Using objCompare to sort fields where there are mixed sort directions.
|
|
282
|
+
// Sort by score descending, then by numGames ascending.
|
|
292
283
|
teams.sort((team1, team2) => {
|
|
293
284
|
return commonFunctions.objCompare(
|
|
294
285
|
team1, team2,
|
|
@@ -297,35 +288,6 @@ teams.sort((team1, team2) => {
|
|
|
297
288
|
);
|
|
298
289
|
});
|
|
299
290
|
console.log(teams);
|
|
300
|
-
/*
|
|
301
|
-
|
|
302
|
-
let teams = [
|
|
303
|
-
{
|
|
304
|
-
score: 85,
|
|
305
|
-
numGames: 10
|
|
306
|
-
},
|
|
307
|
-
{
|
|
308
|
-
score: 90,
|
|
309
|
-
numGames: 12
|
|
310
|
-
},
|
|
311
|
-
{
|
|
312
|
-
score: 85,
|
|
313
|
-
numGames: 8
|
|
314
|
-
},
|
|
315
|
-
{
|
|
316
|
-
score: 90,
|
|
317
|
-
numGames: 10
|
|
318
|
-
}
|
|
319
|
-
];
|
|
320
|
-
// Sort by score descending, then by numGames ascending.
|
|
321
|
-
objArray.sort((team1, team2) => {
|
|
322
|
-
return commonFunctions.objCompare(
|
|
323
|
-
team1, team2,
|
|
324
|
-
"score desc",
|
|
325
|
-
"numGames asc"
|
|
326
|
-
);
|
|
327
|
-
});
|
|
328
|
-
|
|
329
291
|
/*
|
|
330
292
|
[
|
|
331
293
|
{ score: 90, numGames: 10 },
|
|
@@ -354,14 +316,11 @@ let teamsArray = [
|
|
|
354
316
|
{ score: 85, numGames: 8 },
|
|
355
317
|
{ score: 85, numGames: 10 }
|
|
356
318
|
]; // Sorted by "score desc", "numGames asc".
|
|
357
|
-
|
|
358
|
-
|
|
359
319
|
let searchObj = { score: 85, numGames: 8 };
|
|
360
320
|
let anIndex = (commonFunctions.binarySearchObj(teamsArray, searchObj, "score desc", "numGames asc"));
|
|
361
321
|
|
|
362
322
|
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
323
|
```
|
|
364
|
-
|
|
365
324
|
## 4. `getObjArrayWithNoDuplicates(objArray, firstOfDuplicates, ...comparisonFields)`
|
|
366
325
|
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
326
|
* If firstOfDuplicates === true, then the first element in each set of duplicates is taken.
|
|
@@ -399,9 +358,7 @@ console.log(noDuplicatesArray); // Should contain only unique objects according
|
|
|
399
358
|
]
|
|
400
359
|
*/
|
|
401
360
|
```
|
|
402
|
-
|
|
403
361
|
---
|
|
404
|
-
|
|
405
362
|
## License
|
|
406
363
|
MIT
|
|
407
364
|
|