@@ -240,29 +240,61 @@ export class HelpersModule {
240240 /**
241241 * Takes an array and randomizes it in place then returns it.
242242 *
243- * Uses the modern version of the Fisher–Yates algorithm.
243+ * @template T The type of the entries to shuffle.
244+ * @param list The array to shuffle.
245+ * @param options The options to use when shuffling.
246+ * @param options.inplace Whether to shuffle the array in place or return a new array. Defaults to `false`.
247+ *
248+ * @example
249+ * faker.helpers.shuffle(['a', 'b', 'c'], { inplace: true }) // [ 'b', 'c', 'a' ]
250+ *
251+ * @since 8.0.0
252+ */
253+ shuffle < T > ( list : T [ ] , options : { inplace : true } ) : T [ ] ;
254+ /**
255+ * Returns a randomized version of the array.
244256 *
245257 * @template T The type of the entries to shuffle.
246- * @param o The array to shuffle. Defaults to `[]`.
258+ * @param list The array to shuffle.
259+ * @param options The options to use when shuffling.
260+ * @param options.inplace Whether to shuffle the array in place or return a new array. Defaults to `false`.
247261 *
248262 * @example
249- * faker.helpers.shuffle() // []
250263 * faker.helpers.shuffle(['a', 'b', 'c']) // [ 'b', 'c', 'a' ]
264+ * faker.helpers.shuffle(['a', 'b', 'c'], { inplace: false }) // [ 'b', 'c', 'a' ]
251265 *
252266 * @since 2.0.1
253267 */
254- shuffle < T > ( o ?: T [ ] ) : T [ ] {
255- if ( o == null || o . length === 0 ) {
256- return o || [ ] ;
268+ shuffle < T > ( list : readonly T [ ] , options ?: { inplace ?: false } ) : T [ ] ;
269+ /**
270+ * Returns a randomized version of the array.
271+ *
272+ * @template T The type of the entries to shuffle.
273+ * @param list The array to shuffle.
274+ * @param options The options to use when shuffling.
275+ * @param options.inplace Whether to shuffle the array in place or return a new array. Defaults to `false`.
276+ *
277+ * @example
278+ * faker.helpers.shuffle(['a', 'b', 'c']) // [ 'b', 'c', 'a' ]
279+ * faker.helpers.shuffle(['a', 'b', 'c'], { inplace: true }) // [ 'b', 'c', 'a' ]
280+ * faker.helpers.shuffle(['a', 'b', 'c'], { inplace: false }) // [ 'b', 'c', 'a' ]
281+ *
282+ * @since 2.0.1
283+ */
284+ shuffle < T > ( list : T [ ] , options ?: { inplace ?: boolean } ) : T [ ] ;
285+ shuffle < T > ( list : T [ ] , options : { inplace ?: boolean } = { } ) : T [ ] {
286+ const { inplace = false } = options ;
287+
288+ if ( ! inplace ) {
289+ list = [ ...list ] ;
257290 }
258291
259- for ( let i = o . length - 1 ; i > 0 ; -- i ) {
292+ for ( let i = list . length - 1 ; i > 0 ; -- i ) {
260293 const j = this . faker . datatype . number ( i ) ;
261- const x = o [ i ] ;
262- o [ i ] = o [ j ] ;
263- o [ j ] = x ;
294+ [ list [ i ] , list [ j ] ] = [ list [ j ] , list [ i ] ] ;
264295 }
265- return o ;
296+
297+ return list ;
266298 }
267299
268300 /**
0 commit comments