Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KED-1647] Refactor random data generator, and add layers #161

Merged
merged 11 commits into from
May 11, 2020
7 changes: 3 additions & 4 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ const getDataSource = () => {
let source;
const qs = hasWindow && window.location.search.match(/data=(\w+)/);
const { REACT_APP_DATA_SOURCE } = process.env;
const isDemo =
hasWindow && window.location.host === 'quantumblacklabs.github.io';

if (qs) {
source = encodeURIComponent(qs[1]);
} else if (REACT_APP_DATA_SOURCE) {
source = REACT_APP_DATA_SOURCE;
} else if (
hasWindow &&
window.location.host === 'quantumblacklabs.github.io'
) {
} else if (isDemo) {
source = 'demo';
}
return validateDataSource(source);
Expand Down
28 changes: 28 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export const randomIndex = n => Math.floor(Math.random() * n);
*/
export const randomNumber = n => Math.ceil(Math.random() * n);

/**
* Get a random number between min and max, inclusive
* @param {number} (min) Min number
* @param {number} (max) Max number
*/
export const randomNumberBetween = (min, max) => randomNumber(max - min) + min;

/**
* Get a random datum from an array
* @param {Array} range The array to select a random item from
Expand All @@ -61,6 +68,27 @@ export const getRandomName = (n, join = '_') =>
.map(() => getRandom(LOREM_IPSUM))
.join(join);

/**
* Randomly select a certain number (n) of items from an array (arr).
* via https://stackoverflow.com/a/19270021/1651713
* @param {array} arr List from which to choose
* @param {number} n Number of items to select
*/
export const getRandomSelection = (arr, n) => {
const result = new Array(n);
let len = arr.length;
const taken = new Array(len);
if (n > len) {
return arr;
}
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
};

/**
* Filter duplicate values from an array
* @param {any} d Datum
Expand Down
Loading