@softsky/utils 1.0.4 → 1.1.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/README.md +6 -0
- package/dist/arrays.d.ts +4 -0
- package/dist/arrays.js +37 -0
- package/dist/control.d.ts +1 -1
- package/dist/control.js +14 -9
- package/dist/numbers.d.ts +2 -0
- package/dist/numbers.js +9 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -19,6 +19,10 @@ If compare returns > 0 it means we have to cut out bigger side of array.
|
|
|
19
19
|
If compare returns < 0 it means we have to cut out smaller side of array.
|
|
20
20
|
### function chunk
|
|
21
21
|
Split array into sub arrays of spicified size
|
|
22
|
+
### function combinations
|
|
23
|
+
Return all combinations of items in array
|
|
24
|
+
### function permutations
|
|
25
|
+
Return all permutations of items in array
|
|
22
26
|
|
|
23
27
|
## consts
|
|
24
28
|
Some useful consts. That's it.
|
|
@@ -77,6 +81,8 @@ Random number between min and max. May enable float
|
|
|
77
81
|
Same as parseInt but throws
|
|
78
82
|
### function parseFloat
|
|
79
83
|
Same as parseFloat but throws
|
|
84
|
+
### function factorial
|
|
85
|
+
Factorial
|
|
80
86
|
|
|
81
87
|
## objects
|
|
82
88
|
[object Object]
|
package/dist/arrays.d.ts
CHANGED
|
@@ -17,3 +17,7 @@ export declare function swap<T>(array: T[], index: number, index2: number): T[];
|
|
|
17
17
|
export declare function binarySearch(size: number, compare: (index: number) => number): number;
|
|
18
18
|
/** Split array into sub arrays of spicified size */
|
|
19
19
|
export declare function chunk<T>(array: T[], chunkSize: number): T[][];
|
|
20
|
+
/** Return all combinations of items in array */
|
|
21
|
+
export declare function combinations<T>(array: T[]): T[][];
|
|
22
|
+
/** Return all permutations of items in array */
|
|
23
|
+
export declare function permutations<T>(array: T[]): T[][];
|
package/dist/arrays.js
CHANGED
|
@@ -58,3 +58,40 @@ export function chunk(array, chunkSize) {
|
|
|
58
58
|
result.push(copy.splice(0, chunkSize));
|
|
59
59
|
return result;
|
|
60
60
|
}
|
|
61
|
+
/** Return all combinations of items in array */
|
|
62
|
+
export function combinations(array) {
|
|
63
|
+
const amount = 1 << array.length;
|
|
64
|
+
// eslint-disable-next-line unicorn/no-new-array
|
|
65
|
+
const combinations = new Array(amount);
|
|
66
|
+
for (let combinationIndex = 0; combinationIndex < amount; combinationIndex++) {
|
|
67
|
+
const combination = [];
|
|
68
|
+
for (let index = 0; index < array.length; index++)
|
|
69
|
+
if (combinationIndex >> index & 1)
|
|
70
|
+
combination.push(array[index]);
|
|
71
|
+
combinations[combinationIndex] = combination;
|
|
72
|
+
}
|
|
73
|
+
return combinations;
|
|
74
|
+
}
|
|
75
|
+
/** Return all permutations of items in array */
|
|
76
|
+
export function permutations(array) {
|
|
77
|
+
const n = array.length;
|
|
78
|
+
const result = [];
|
|
79
|
+
// eslint-disable-next-line unicorn/no-new-array
|
|
80
|
+
const control = new Array(n).fill(0);
|
|
81
|
+
result.push([...array]);
|
|
82
|
+
let index = 0;
|
|
83
|
+
while (index < n) {
|
|
84
|
+
if (control[index] < index) {
|
|
85
|
+
const k = index % 2 === 0 ? 0 : control[index];
|
|
86
|
+
swap(array, index, k);
|
|
87
|
+
result.push([...array]);
|
|
88
|
+
control[index]++;
|
|
89
|
+
index = 0;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
control[index] = 0;
|
|
93
|
+
index++;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return result;
|
|
97
|
+
}
|
package/dist/control.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ export declare function createDelayedFunction<T, V extends unknown[]>(function_:
|
|
|
35
35
|
export declare class ImmediatePromise<T> extends Promise<T> {
|
|
36
36
|
resolve: (value: T | PromiseLike<T>) => void;
|
|
37
37
|
reject: (reason?: unknown) => void;
|
|
38
|
-
constructor();
|
|
38
|
+
constructor(execute?: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void);
|
|
39
39
|
}
|
|
40
40
|
/** Recursively resolves promises in objects and arrays */
|
|
41
41
|
export default function deepPromiseAll<T>(input: T): Promise<AwaitedObject<T>>;
|
package/dist/control.js
CHANGED
|
@@ -112,15 +112,20 @@ export function createDelayedFunction(function_, time) {
|
|
|
112
112
|
export class ImmediatePromise extends Promise {
|
|
113
113
|
resolve;
|
|
114
114
|
reject;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
116
|
+
constructor(execute) {
|
|
117
|
+
if (execute)
|
|
118
|
+
super(execute);
|
|
119
|
+
else {
|
|
120
|
+
let _resolve = noop;
|
|
121
|
+
let _reject = noop;
|
|
122
|
+
super((resolve, reject) => {
|
|
123
|
+
_resolve = resolve;
|
|
124
|
+
_reject = reject;
|
|
125
|
+
});
|
|
126
|
+
this.resolve = _resolve;
|
|
127
|
+
this.reject = _reject;
|
|
128
|
+
}
|
|
124
129
|
}
|
|
125
130
|
}
|
|
126
131
|
/** Recursively resolves promises in objects and arrays */
|
package/dist/numbers.d.ts
CHANGED
|
@@ -4,3 +4,5 @@ export declare function random(min: number, max: number, float?: boolean): numbe
|
|
|
4
4
|
export declare function parseInt(parameter: unknown, radix?: number): number;
|
|
5
5
|
/** Same as parseFloat but throws */
|
|
6
6
|
export declare function parseFloat(parameter: unknown): number;
|
|
7
|
+
/** Factorial */
|
|
8
|
+
export declare function factorial(n: number): number;
|
package/dist/numbers.js
CHANGED
|
@@ -23,3 +23,12 @@ export function parseFloat(parameter) {
|
|
|
23
23
|
throw new ValidationError(`Can not parse "${parameter}" to float`);
|
|
24
24
|
return n;
|
|
25
25
|
}
|
|
26
|
+
/** Factorial */
|
|
27
|
+
export function factorial(n) {
|
|
28
|
+
if (n === 0 || n === 1)
|
|
29
|
+
return 1;
|
|
30
|
+
let result = 1;
|
|
31
|
+
for (let index = 2; index <= n; index++)
|
|
32
|
+
result *= index;
|
|
33
|
+
return result;
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softsky/utils",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "JavaScript/TypeScript utilities",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
},
|
|
20
20
|
"homepage": "https://github.com/SoundOfTheSky/utils#readme",
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@softsky/configs": "^1.0.
|
|
23
|
-
"@types/bun": "^1.1.
|
|
22
|
+
"@softsky/configs": "^1.0.8",
|
|
23
|
+
"@types/bun": "^1.1.18"
|
|
24
24
|
},
|
|
25
25
|
"files": ["dist/**/*"]
|
|
26
26
|
}
|