utilitish 0.0.2 → 0.0.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.
|
@@ -84,5 +84,11 @@ declare global {
|
|
|
84
84
|
* @returns The array itself after swapping
|
|
85
85
|
*/
|
|
86
86
|
swap(i: number, j: number): this;
|
|
87
|
+
/**
|
|
88
|
+
* Returns a new array with the elements shuffled in random order.
|
|
89
|
+
* Uses the Fisher-Yates shuffle algorithm.
|
|
90
|
+
* @returns A new shuffled array.
|
|
91
|
+
*/
|
|
92
|
+
shuffle(): T[];
|
|
87
93
|
}
|
|
88
94
|
}
|
|
@@ -125,3 +125,11 @@ const utils_1 = require("../utils");
|
|
|
125
125
|
}
|
|
126
126
|
return this;
|
|
127
127
|
});
|
|
128
|
+
(0, utils_1.defineIfNotExists)(Array.prototype, 'shuffle', function () {
|
|
129
|
+
const arr = this.slice();
|
|
130
|
+
for (let i = arr.length - 1; i > 0; i--) {
|
|
131
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
132
|
+
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
133
|
+
}
|
|
134
|
+
return arr;
|
|
135
|
+
});
|