Skip to content

Commit

Permalink
feat(index.ts): added a new function: shuffle
Browse files Browse the repository at this point in the history
Shuffle takes in an array and returns a shuffled array
  • Loading branch information
loveakinlesi committed Jun 25, 2021
1 parent 8749b06 commit d5e0c6a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![🚀 Publish](https://github.com/iiitma/js-randomize/actions/workflows/publish.yml/badge.svg?branch=main)](https://github.com/iiitma/js-randomize/actions/workflows/publish.yml)

# js-randomize

## Table of Contents
Expand Down Expand Up @@ -62,6 +64,9 @@ var randomArray2 = random.array(array, 3);
// randomArray2 is an array of 3 elements from array


var shuffled = randomz.shuffle(array);
// shuffled is a shuffled version of array

```

### Typescript
Expand Down
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export declare function intArray(count?: Number, min?: Number, max?: Number): Nu
export declare function floatArray(count?: Number, min?: Number, max?: Number, dp?: Number): Number[];
export declare function boolean(): boolean;
export declare function array(array: any[], count?: Number): any[];
export declare function shuffle(array: any[]): any[];
34 changes: 33 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.array = exports.boolean = exports.floatArray = exports.intArray = exports.float = exports.int = void 0;
exports.shuffle = exports.array = exports.boolean = exports.floatArray = exports.intArray = exports.float = exports.int = void 0;
// generate integer from range
function int(min = 0, max = 100) {
// add runtime check for use in JavaScript
Expand Down Expand Up @@ -78,3 +78,35 @@ function array(array, count = 1) {
return result;
}
exports.array = array;
// generate shuffled array from array
function shuffle(array) {
// add runtime check for use in JavaScript
if (!Array.isArray(array)) {
throw new Error('Invalid Inputs');
}
// get array length
const length = array.length;
let result = [];
// List of value indices
let list = [];
// loop through "array length" times
for (let i = 0; i < length; i++) {
// generate random integer in range[0, array length]
let x = int(0, length - 1);
// check if value is already in list
while (list.includes(x)) {
// if already in list, generate new value
x = int(0, length - 1);
}
// if not, add to list
list.push(x);
}
// loop through "list length" times
for (let i = 0; i < length; i++) {
// ad to array value to result
result.push(array[list[i]]);
}
// return shuffled array
return result;
}
exports.shuffle = shuffle;
38 changes: 38 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,41 @@ export function array(array: any[], count: Number=1): any[] {
}


// generate shuffled array from array
export function shuffle(array: any[]): any[] {
// add runtime check for use in JavaScript
if (!Array.isArray(array)) {
throw new Error('Invalid Inputs')
}

// get array length
const length = array.length;

let result: any[] = [];

// List of value indices
let list: any[] =[];

// loop through "array length" times
for(let i = 0; i < length; i++){

// generate random integer in range[0, array length]
let x = int(0, length-1)

// check if value is already in list
while(list.includes(x)){
// if already in list, generate new value
x = int(0, length-1)
}
// if not, add to list
list.push(x)
}

// loop through "list length" times
for(let i = 0; i < length; i++){
// ad to array value to result
result.push(array[list[i]])
}
// return shuffled array
return result
}
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,30 @@ describe('Random Array - array()', function () {
});
});


describe('Suffle Array - shuffle()', function () {
it('should return a shuffled array', function () {

// 1. ARRANGE
const tests = [[ 31, 71, 34, 3, 25 ], ['Married', 'Single', 'Divorced', 'Widowed'], ['Self Employed', 'Employed', 'Unemployed', 'Retired', 'Student'], ['0809', '0805', '0803', '0806', '0807', '0802', '0808', '0709', '0705', '0703', '7806', '0707', '0702', '0708', '0909', '0905', '0903', '0906', '0907', '0902', '0908']]

// 2. ACT
let result = []
for (const test of tests) {
y = random.shuffle(test)
result.push(y)
}

// 3. ASSERT
for (const i in tests) {
expect(Array.isArray(result[i])).to.be.equal(true);
expect(result[i].length).to.be.equal(tests[i].length);
let index = []
for(const element of result[i]){
index.push(tests[i].indexOf(element))
}
expect( _.range(0, 10).join(',') !== index.join(',')).to.be.equal(true);
}

});
});

0 comments on commit d5e0c6a

Please sign in to comment.