@reldens/utils 0.55.0 → 0.57.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/CLAUDE.md +0 -4
- package/lib/shortcuts.js +13 -0
- package/package.json +1 -1
- package/tests/shortcuts-test.js +18 -0
package/CLAUDE.md
CHANGED
package/lib/shortcuts.js
CHANGED
|
@@ -404,6 +404,19 @@ class Shortcuts
|
|
|
404
404
|
return array[Math.floor(Math.random() * array.length)];
|
|
405
405
|
}
|
|
406
406
|
|
|
407
|
+
shuffleArray(array)
|
|
408
|
+
{
|
|
409
|
+
if(!this.isArray(array)){
|
|
410
|
+
return [];
|
|
411
|
+
}
|
|
412
|
+
let newArray = [...array];
|
|
413
|
+
for(let i = newArray.length - 1; i > 0; i--){
|
|
414
|
+
let swapIndex = Math.floor(Math.random() * (i + 1));
|
|
415
|
+
[newArray[i], newArray[swapIndex]] = [newArray[swapIndex], newArray[i]];
|
|
416
|
+
}
|
|
417
|
+
return newArray;
|
|
418
|
+
}
|
|
419
|
+
|
|
407
420
|
randomInteger(min, max)
|
|
408
421
|
{
|
|
409
422
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
package/package.json
CHANGED
package/tests/shortcuts-test.js
CHANGED
|
@@ -673,6 +673,24 @@ class TestShortcuts
|
|
|
673
673
|
});
|
|
674
674
|
}
|
|
675
675
|
|
|
676
|
+
testShuffleArray()
|
|
677
|
+
{
|
|
678
|
+
this.test('returns an array with the same elements', () => {
|
|
679
|
+
let values = [1, 2, 3, 4, 5];
|
|
680
|
+
let result = sc.shuffleArray(values);
|
|
681
|
+
this.assert(5 === result.length);
|
|
682
|
+
this.assert(values.every(value => sc.inArray(value, result)));
|
|
683
|
+
});
|
|
684
|
+
this.test('does not mutate the input array', () => {
|
|
685
|
+
let values = [1, 2, 3];
|
|
686
|
+
sc.shuffleArray(values);
|
|
687
|
+
this.assert(3 === values.length && 1 === values[0] && 2 === values[1] && 3 === values[2]);
|
|
688
|
+
});
|
|
689
|
+
this.test('returns empty array for non-array', () => {
|
|
690
|
+
this.assert(0 === sc.shuffleArray(null).length);
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
|
|
676
694
|
testRandomInteger()
|
|
677
695
|
{
|
|
678
696
|
this.test('returns integer within range', () => {
|