From d5e0c6a96787c367a29782818b21d241280f94b2 Mon Sep 17 00:00:00 2001 From: iiitma Date: Fri, 25 Jun 2021 01:44:06 +0100 Subject: [PATCH] feat(index.ts): added a new function: shuffle Shuffle takes in an array and returns a shuffled array --- README.md | 5 +++++ dist/index.d.ts | 1 + dist/index.js | 34 +++++++++++++++++++++++++++++++++- lib/index.ts | 38 ++++++++++++++++++++++++++++++++++++++ test/test.js | 27 +++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b801df..86ba4e3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/dist/index.d.ts b/dist/index.d.ts index dcbc42f..1cb273d 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -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[]; diff --git a/dist/index.js b/dist/index.js index ec53222..7fdabb9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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 @@ -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; diff --git a/lib/index.ts b/lib/index.ts index a0a3dda..3b827ca 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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 +} diff --git a/test/test.js b/test/test.js index e48a22a..1c71309 100644 --- a/test/test.js +++ b/test/test.js @@ -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); + } + + }); +}); \ No newline at end of file