@softsky/utils 2.3.1 → 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 +21 -4
- package/dist/arrays.d.ts +25 -6
- package/dist/arrays.js +41 -10
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -61,11 +61,28 @@ ${\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
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
${\textsf{\color{CornflowerBlue}function}}$ removeFromArray - Delete value from array.
|
|
77
|
+
Only deletes first encountered.
|
|
78
|
+
|
|
79
|
+
Returns index of deleted value.
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
${\textsf{\color{CornflowerBlue}function}}$ removeLastFromArray - Delete value from array.
|
|
83
|
+
Only deletes last encountered.
|
|
84
|
+
|
|
85
|
+
Returns index of deleted value.
|
|
69
86
|
|
|
70
87
|
---
|
|
71
88
|
|
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,10 +22,29 @@ 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
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function pushToSorted<T>(array: T[], element: T, compare: (x: T) => number): void;
|
|
37
|
+
/**
|
|
38
|
+
* Delete value from array.
|
|
39
|
+
* Only deletes first encountered.
|
|
40
|
+
*
|
|
41
|
+
* Returns index of deleted value.
|
|
42
|
+
*/
|
|
43
|
+
export declare function removeFromArray<T>(array: T[], value: T): number;
|
|
44
|
+
/**
|
|
45
|
+
* Delete value from array.
|
|
46
|
+
* Only deletes last encountered.
|
|
47
|
+
*
|
|
48
|
+
* Returns index of deleted value.
|
|
30
49
|
*/
|
|
31
|
-
export declare function
|
|
50
|
+
export declare function removeLastFromArray<T>(array: T[], value: T): number;
|
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,13 +96,42 @@ 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);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Delete value from array.
|
|
116
|
+
* Only deletes first encountered.
|
|
117
|
+
*
|
|
118
|
+
* Returns index of deleted value.
|
|
119
|
+
*/
|
|
120
|
+
export function removeFromArray(array, value) {
|
|
121
|
+
const index = array.indexOf(value);
|
|
122
|
+
if (index !== -1)
|
|
123
|
+
array.splice(index, 1);
|
|
124
|
+
return index;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Delete value from array.
|
|
128
|
+
* Only deletes last encountered.
|
|
129
|
+
*
|
|
130
|
+
* Returns index of deleted value.
|
|
131
|
+
*/
|
|
132
|
+
export function removeLastFromArray(array, value) {
|
|
133
|
+
const index = array.lastIndexOf(value);
|
|
134
|
+
if (index !== -1)
|
|
135
|
+
array.splice(index, 1);
|
|
136
|
+
return index;
|
|
106
137
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softsky/utils",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.3",
|
|
4
4
|
"description": "JavaScript/TypeScript utilities",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"lint": "eslint \"./src/**/*.ts\" --fix && tsc",
|
|
8
8
|
"gen-readme": "bun ./generate-readme.ts",
|
|
9
|
-
"prepublishOnly": "bun run test && bun run lint && bun run gen-readme",
|
|
9
|
+
"prepublishOnly": "npm i && bun run test && bun run lint && bun run gen-readme",
|
|
10
10
|
"test": "bun test"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|