cafe-utility 4.2.0 → 4.3.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/index.d.ts +6 -2
- package/index.js +28 -3
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ declare function ensureDeep(object: CafeObject, path: string, value: unknown): u
|
|
|
31
31
|
declare function deleteDeep(object: CafeObject, path: string): void;
|
|
32
32
|
declare function replaceDeep(object: CafeObject, path: string, value: unknown): unknown;
|
|
33
33
|
declare function getFirstDeep(object: CafeObject, paths: string[], fallbackToAnyKey: string): unknown;
|
|
34
|
-
declare function forever(callable: () => Promise<
|
|
34
|
+
declare function forever(callable: () => Promise<void>, millis: number): Promise<never>;
|
|
35
35
|
declare function readUtf8FileAsync(path: string): Promise<string>;
|
|
36
36
|
declare function readJsonAsync(path: string): Promise<CafeObject>;
|
|
37
37
|
declare function writeJsonAsync(path: string, object: CafeObject, prettify?: boolean): Promise<void>;
|
|
@@ -206,7 +206,7 @@ interface NumberFormatOptions {
|
|
|
206
206
|
unit?: null | string;
|
|
207
207
|
}
|
|
208
208
|
declare function formatNumber(number: number, options?: NumberFormatOptions): string;
|
|
209
|
-
declare function parseIntOrThrow(numberOrString:
|
|
209
|
+
declare function parseIntOrThrow(numberOrString: any): number;
|
|
210
210
|
declare function clamp(value: number, lower: number, upper: number): number;
|
|
211
211
|
declare function increment(value: number, change: number, maximum: number): number;
|
|
212
212
|
declare function decrement(value: number, change: number, minimum: number): number;
|
|
@@ -222,6 +222,8 @@ declare function mergeArrays(target: CafeObject<unknown[]>, source: CafeObject<u
|
|
|
222
222
|
declare function empty<T>(array: T[]): T[];
|
|
223
223
|
declare function removeEmptyArrays(object: CafeObject): CafeObject;
|
|
224
224
|
declare function removeEmptyValues(object: CafeObject): CafeObject;
|
|
225
|
+
declare function filterObjectKeys<T>(object: CafeObject<T>, predicate: (key: string) => boolean): CafeObject<T>;
|
|
226
|
+
declare function filterObjectValues<T>(object: CafeObject<T>, predicate: (value: T) => boolean): CafeObject<T>;
|
|
225
227
|
declare function mapObject<T, K>(object: CafeObject<T>, mapper: (value: T) => K): CafeObject<K>;
|
|
226
228
|
declare function rethrow<T>(asyncFn: () => Promise<T>, throwable: Error): Promise<T>;
|
|
227
229
|
declare function setSomeOnObject(object: CafeObject, key: string, value: unknown): void;
|
|
@@ -379,6 +381,8 @@ export declare const Objects: {
|
|
|
379
381
|
match: typeof match;
|
|
380
382
|
sort: typeof sortObjectValues;
|
|
381
383
|
map: typeof mapObject;
|
|
384
|
+
filterKeys: typeof filterObjectKeys;
|
|
385
|
+
filterValues: typeof filterObjectValues;
|
|
382
386
|
rethrow: typeof rethrow;
|
|
383
387
|
setSomeOnObject: typeof setSomeOnObject;
|
|
384
388
|
flip: typeof flip;
|
package/index.js
CHANGED
|
@@ -107,18 +107,21 @@ function last(array) {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
function pickWeighted(array, weights, randomNumber) {
|
|
110
|
+
if (isUndefined(randomNumber)) {
|
|
111
|
+
randomNumber = Math.random()
|
|
112
|
+
}
|
|
110
113
|
if (array.length !== weights.length) {
|
|
111
114
|
throw new Error('Array length mismatch')
|
|
112
115
|
}
|
|
113
116
|
let sum = weights.reduce((accumulator, element) => accumulator + element, 0)
|
|
114
|
-
const random =
|
|
115
|
-
for (let i = 0; i < array.length; i++) {
|
|
117
|
+
const random = randomNumber * sum
|
|
118
|
+
for (let i = 0; i < array.length - 1; i++) {
|
|
116
119
|
sum -= weights[i]
|
|
117
120
|
if (random >= sum) {
|
|
118
121
|
return array[i]
|
|
119
122
|
}
|
|
120
123
|
}
|
|
121
|
-
|
|
124
|
+
return last(array)
|
|
122
125
|
}
|
|
123
126
|
|
|
124
127
|
function sortWeighted(array, weights) {
|
|
@@ -1337,6 +1340,26 @@ function removeEmptyValues(object) {
|
|
|
1337
1340
|
return object
|
|
1338
1341
|
}
|
|
1339
1342
|
|
|
1343
|
+
function filterObjectKeys(object, predicate) {
|
|
1344
|
+
const output = {}
|
|
1345
|
+
for (const [key, value] of Object.entries(object)) {
|
|
1346
|
+
if (predicate(key)) {
|
|
1347
|
+
output[key] = value
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
return output
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
function filterObjectValues(object, predicate) {
|
|
1354
|
+
const output = {}
|
|
1355
|
+
for (const [key, value] of Object.entries(object)) {
|
|
1356
|
+
if (predicate(value)) {
|
|
1357
|
+
output[key] = value
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
return output
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1340
1363
|
function mapObject(object, mapper) {
|
|
1341
1364
|
const output = {}
|
|
1342
1365
|
for (const entry of Object.entries(object)) {
|
|
@@ -1752,6 +1775,8 @@ exports.Objects = {
|
|
|
1752
1775
|
match,
|
|
1753
1776
|
sort: sortObjectValues,
|
|
1754
1777
|
map: mapObject,
|
|
1778
|
+
filterKeys: filterObjectKeys,
|
|
1779
|
+
filterValues: filterObjectValues,
|
|
1755
1780
|
rethrow,
|
|
1756
1781
|
setSomeOnObject,
|
|
1757
1782
|
flip,
|