@cornerstonejs/core 1.59.0 → 1.59.2
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/dist/cjs/utilities/getRandomSampleFromArray.d.ts +1 -0
- package/dist/cjs/utilities/getRandomSampleFromArray.js +20 -0
- package/dist/cjs/utilities/getRandomSampleFromArray.js.map +1 -0
- package/dist/cjs/utilities/index.d.ts +2 -1
- package/dist/cjs/utilities/index.js +3 -1
- package/dist/cjs/utilities/index.js.map +1 -1
- package/dist/esm/utilities/getRandomSampleFromArray.js +16 -0
- package/dist/esm/utilities/getRandomSampleFromArray.js.map +1 -0
- package/dist/esm/utilities/index.js +2 -1
- package/dist/esm/utilities/index.js.map +1 -1
- package/dist/types/utilities/getRandomSampleFromArray.d.ts +2 -0
- package/dist/types/utilities/getRandomSampleFromArray.d.ts.map +1 -0
- package/dist/types/utilities/index.d.ts +2 -1
- package/dist/types/utilities/index.d.ts.map +1 -1
- package/dist/umd/index.js +1 -1
- package/dist/umd/index.js.map +1 -1
- package/package.json +2 -2
- package/src/utilities/getRandomSampleFromArray.ts +30 -0
- package/src/utilities/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cornerstonejs/core",
|
|
3
|
-
"version": "1.59.
|
|
3
|
+
"version": "1.59.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"type": "individual",
|
|
48
48
|
"url": "https://ohif.org/donate"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "ad8b39627c4f37c1a6007c39e358966b716f52bb"
|
|
51
51
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets a random sample of specified size from the array.
|
|
3
|
+
* If the requested size is greater than the array length, returns a shuffled clone of the original array.
|
|
4
|
+
* @param array - The source array from which to sample.
|
|
5
|
+
* @param size - The number of elements to sample from the array.
|
|
6
|
+
* @returns A new array containing the random sample.
|
|
7
|
+
*/
|
|
8
|
+
export function getRandomSampleFromArray<T>(array: T[], size: number): T[] {
|
|
9
|
+
const clonedArray = [...array]; // Copy the original array
|
|
10
|
+
|
|
11
|
+
// If requested size is greater than array length, shuffle and return clone of the original array
|
|
12
|
+
if (size >= clonedArray.length) {
|
|
13
|
+
shuffleArray(clonedArray);
|
|
14
|
+
return clonedArray;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
shuffleArray(clonedArray);
|
|
18
|
+
return clonedArray.slice(0, size);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Shuffles an array
|
|
23
|
+
* @param array - The array to shuffle.
|
|
24
|
+
*/
|
|
25
|
+
function shuffleArray<T>(array: T[]): void {
|
|
26
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
27
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
28
|
+
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/utilities/index.ts
CHANGED
|
@@ -69,6 +69,7 @@ import VoxelManager from './VoxelManager';
|
|
|
69
69
|
import roundNumber, { roundToPrecision } from './roundNumber';
|
|
70
70
|
import convertToGrayscale from './convertToGrayscale';
|
|
71
71
|
import getViewportImageIds from './getViewportImageIds';
|
|
72
|
+
import { getRandomSampleFromArray } from './getRandomSampleFromArray';
|
|
72
73
|
|
|
73
74
|
// name spaces
|
|
74
75
|
import * as planar from './planar';
|
|
@@ -156,4 +157,5 @@ export {
|
|
|
156
157
|
roundNumber,
|
|
157
158
|
roundToPrecision,
|
|
158
159
|
getViewportImageIds,
|
|
160
|
+
getRandomSampleFromArray,
|
|
159
161
|
};
|