@softsky/utils 2.3.2 → 2.3.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/README.md +9 -4
- package/dist/arrays.d.ts +11 -6
- package/dist/arrays.js +17 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -61,11 +61,16 @@ ${\textsf{\color{CornflowerBlue}function}}$ combinations - Return all combinatio
|
|
|
61
61
|
${\textsf{\color{CornflowerBlue}function}}$ permutations - Return all permutations of items in array
|
|
62
62
|
|
|
63
63
|
---
|
|
64
|
-
${\textsf{\color{CornflowerBlue}function}}$ pushToSorted - Push data
|
|
64
|
+
${\textsf{\color{CornflowerBlue}function}}$ pushToSorted - Push data to sorted array. Array will always be kept sorted.
|
|
65
65
|
|
|
66
|
-
Compare
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
Compare function should compare your needed value with value on index passed to it.
|
|
67
|
+
If compare returns 0 it means we found target.
|
|
68
|
+
If compare returns > 0 it means target is smaller.
|
|
69
|
+
If compare returns < 0 it means target is bigger.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
pushToSorted(numArray, 10, x => x - 10);
|
|
73
|
+
```
|
|
69
74
|
|
|
70
75
|
---
|
|
71
76
|
${\textsf{\color{CornflowerBlue}function}}$ removeFromArray - Delete value from array.
|
package/dist/arrays.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export declare function swap<T>(array: T[], index: number, index2: number): T[];
|
|
|
14
14
|
* If compare returns > 0 it means we have to cut out bigger side of array.
|
|
15
15
|
* If compare returns < 0 it means we have to cut out smaller side of array.
|
|
16
16
|
*/
|
|
17
|
-
export declare function binarySearch(size: number, compare: (index: number) => number): number;
|
|
17
|
+
export declare function binarySearch(size: number, compare: (index: number) => number, returnClosest?: boolean): number;
|
|
18
18
|
/** Split array into sub arrays of spicified size */
|
|
19
19
|
export declare function chunk<T>(array: T[], chunkSize: number): T[][];
|
|
20
20
|
/** Return all combinations of items in array */
|
|
@@ -22,13 +22,18 @@ export declare function combinations<T>(array: T[]): T[][];
|
|
|
22
22
|
/** Return all permutations of items in array */
|
|
23
23
|
export declare function permutations<T>(array: T[]): T[][];
|
|
24
24
|
/**
|
|
25
|
-
* Push data
|
|
25
|
+
* Push data to sorted array. Array will always be kept sorted.
|
|
26
26
|
*
|
|
27
|
-
* Compare
|
|
28
|
-
*
|
|
29
|
-
*
|
|
27
|
+
* Compare function should compare your needed value with value on index passed to it.
|
|
28
|
+
* If compare returns 0 it means we found target.
|
|
29
|
+
* If compare returns > 0 it means target is smaller.
|
|
30
|
+
* If compare returns < 0 it means target is bigger.
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* pushToSorted(numArray, 10, x => x - 10);
|
|
34
|
+
* ```
|
|
30
35
|
*/
|
|
31
|
-
export declare function pushToSorted<T>(array: T[], element: T, compare: (
|
|
36
|
+
export declare function pushToSorted<T>(array: T[], element: T, compare: (x: T) => number): void;
|
|
32
37
|
/**
|
|
33
38
|
* Delete value from array.
|
|
34
39
|
* Only deletes first encountered.
|
package/dist/arrays.js
CHANGED
|
@@ -32,12 +32,14 @@ export function swap(array, index, index2) {
|
|
|
32
32
|
* If compare returns > 0 it means we have to cut out bigger side of array.
|
|
33
33
|
* If compare returns < 0 it means we have to cut out smaller side of array.
|
|
34
34
|
*/
|
|
35
|
-
export function binarySearch(size, compare) {
|
|
35
|
+
export function binarySearch(size, compare, returnClosest) {
|
|
36
36
|
let low = 0;
|
|
37
|
-
let high = size
|
|
37
|
+
let high = size;
|
|
38
38
|
let position = -1;
|
|
39
|
-
while (
|
|
40
|
-
const mid =
|
|
39
|
+
while (high >= low) {
|
|
40
|
+
const mid = ((low + high) / 2) | 0;
|
|
41
|
+
if (returnClosest)
|
|
42
|
+
position = mid;
|
|
41
43
|
const compared = compare(mid);
|
|
42
44
|
if (compared === 0) {
|
|
43
45
|
position = mid;
|
|
@@ -94,15 +96,20 @@ export function permutations(array) {
|
|
|
94
96
|
return result;
|
|
95
97
|
}
|
|
96
98
|
/**
|
|
97
|
-
* Push data
|
|
99
|
+
* Push data to sorted array. Array will always be kept sorted.
|
|
100
|
+
*
|
|
101
|
+
* Compare function should compare your needed value with value on index passed to it.
|
|
102
|
+
* If compare returns 0 it means we found target.
|
|
103
|
+
* If compare returns > 0 it means target is smaller.
|
|
104
|
+
* If compare returns < 0 it means target is bigger.
|
|
98
105
|
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
106
|
+
* ```ts
|
|
107
|
+
* pushToSorted(numArray, 10, x => x - 10);
|
|
108
|
+
* ```
|
|
102
109
|
*/
|
|
103
110
|
export function pushToSorted(array, element, compare) {
|
|
104
|
-
const index = array.
|
|
105
|
-
array.splice(index
|
|
111
|
+
const index = binarySearch(array.length, (x) => compare(array[x]), true);
|
|
112
|
+
array.splice(index, 0, element);
|
|
106
113
|
}
|
|
107
114
|
/**
|
|
108
115
|
* Delete value from array.
|