@softsky/utils 1.0.5 → 1.1.1
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 +8 -0
- package/dist/arrays.d.ts +4 -0
- package/dist/arrays.js +35 -0
- package/dist/numbers.d.ts +2 -0
- package/dist/numbers.js +9 -0
- package/dist/types.d.ts +5 -1
- 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]
|
|
@@ -119,6 +125,8 @@ Find next cron tick after passed date
|
|
|
119
125
|
Damn, I **love** TypeScript.
|
|
120
126
|
### type Optional
|
|
121
127
|
Make keys in object optional
|
|
128
|
+
### type Required
|
|
129
|
+
Make keys in object required
|
|
122
130
|
### type Constructor
|
|
123
131
|
Get contructor type of an instance
|
|
124
132
|
### type AwaitedObject
|
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,38 @@ 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
|
+
const combinations = new Array(amount);
|
|
65
|
+
for (let combinationIndex = 0; combinationIndex < amount; combinationIndex++) {
|
|
66
|
+
const combination = [];
|
|
67
|
+
for (let index = 0; index < array.length; index++)
|
|
68
|
+
if (combinationIndex >> index & 1)
|
|
69
|
+
combination.push(array[index]);
|
|
70
|
+
combinations[combinationIndex] = combination;
|
|
71
|
+
}
|
|
72
|
+
return combinations;
|
|
73
|
+
}
|
|
74
|
+
/** Return all permutations of items in array */
|
|
75
|
+
export function permutations(array) {
|
|
76
|
+
const n = array.length;
|
|
77
|
+
const result = [];
|
|
78
|
+
const control = new Array(n).fill(0);
|
|
79
|
+
result.push([...array]);
|
|
80
|
+
let index = 0;
|
|
81
|
+
while (index < n) {
|
|
82
|
+
if (control[index] < index) {
|
|
83
|
+
const k = index % 2 === 0 ? 0 : control[index];
|
|
84
|
+
swap(array, index, k);
|
|
85
|
+
result.push([...array]);
|
|
86
|
+
control[index]++;
|
|
87
|
+
index = 0;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
control[index] = 0;
|
|
91
|
+
index++;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
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/dist/types.d.ts
CHANGED
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
* Damn, I **love** TypeScript.
|
|
3
3
|
*/
|
|
4
4
|
/** Make keys in object optional */
|
|
5
|
-
export type Optional<T, K extends keyof
|
|
5
|
+
export type Optional<T, K extends keyof T> = Omit<T, K & keyof T> & Partial<Pick<T, K & keyof T>>;
|
|
6
|
+
/** Make keys in object required */
|
|
7
|
+
export type Required<T, K extends keyof T> = Omit<T, K & keyof T> & {
|
|
8
|
+
[P in K]-?: T[P];
|
|
9
|
+
};
|
|
6
10
|
/** Get contructor type of an instance */
|
|
7
11
|
export type Constructor<T> = abstract new (..._arguments: never[]) => T;
|
|
8
12
|
/** Recursively resolves promises in objects and arrays */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softsky/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
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.
|
|
22
|
+
"@softsky/configs": "^1.1.0",
|
|
23
|
+
"@types/bun": "^1.2.0"
|
|
24
24
|
},
|
|
25
25
|
"files": ["dist/**/*"]
|
|
26
26
|
}
|