Skip to content

Commit

Permalink
feat: shuffle function
Browse files Browse the repository at this point in the history
  • Loading branch information
vigan-abd committed Nov 2, 2023
1 parent 4f112b1 commit 8928cca
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.5.0
- feat: shuffle

# 1.4.0
- feat: isObject

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Currently supported utils:
- `omitBy` - provides new object that omits only specific fields of source object depending on predicate function filter
- `pick` - provides new object that picks only specific fields of source object
- `pickBy` - provides new object that picks only specific fields of source object depending on predicate function filter
- `shuffle` - performs pseudo random shuffle on clone of the array
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export function omit(obj: Object, keys: Array<string>): Object
export function omitBy(obj: Object, predicate: (val: any, key: string) => boolean): Object
export function pick(obj: Object, keys: Array<string>): Object
export function pickBy(obj: Object, predicate: (val: any, key: string) => boolean): Object
export function shuffle<T>(array: Array<T>): Array<T>
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const omit = require('./src/omit')
const omitBy = require('./src/omitBy')
const pick = require('./src/pick')
const pickBy = require('./src/pickBy')
const shuffle = require('./src/shuffle')

module.exports = {
camelize,
Expand All @@ -31,5 +32,6 @@ module.exports = {
omit,
omitBy,
pick,
pickBy
pickBy,
shuffle
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitfinex/lib-js-util-base",
"version": "1.4.0",
"version": "1.5.0",
"description": "general utils",
"main": "index.js",
"scripts": {
Expand Down
22 changes: 22 additions & 0 deletions src/shuffle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

/**
* Performs pseudo random shuffles on clone of the array
*
* @param {Array<any>} array
* @returns {Array<any>}
*/
const shuffle = (array) => {
const clone = [...array]
const length = clone.length
for (let i = 0; i < length; i++) {
const mov = Math.floor(Math.random() * length)
const tmp = clone[mov]
clone[mov] = clone[i]
clone[i] = tmp
}

return clone
}

module.exports = shuffle
18 changes: 18 additions & 0 deletions test/shuffle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

/* eslint-env mocha */

const assert = require('assert')
const { shuffle } = require('../index')

describe('shuffle', () => {
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

it('should return a new array', () => {
assert.ok(shuffle(array).some((x, i) => array[i] !== x))
})

it('should contain the same elements after a collection is shuffled', () => {
assert.deepStrictEqual(shuffle(array).sort((a, b) => a - b), array)
})
})

0 comments on commit 8928cca

Please sign in to comment.