forked from nsjcy/frontEnd/nsjcy

xuxj
2020-03-24 ebfd434d26f8269e225efe71ed9c80e2d4cf0ae9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import fill from 'lodash/fill';
export default (data, time = 1000) =>
  new Promise(resolve => {
    setTimeout(() => {
      resolve(data);
    }, time);
  });
 
 
export const arrayFromSeed = (seed, length = 0) => {
  const results = fill(Array(length || 1), 0).map(() =>
    Object.keys(seed).reduce((obj, key) => {
      const arr = seed[key];
      obj[key] = arr[Math.floor(
        Math.random() * arr.length
      )];
      return obj;
    }, {}));
  return length === 0 ? results[0] : results;
}
 
export const arrayFromLength = (length, map = (e, i) => i) => fill(Array(length), 0).map(map);
 
export const randomFromRange = (min, max) => Math.floor(Math.random() * (max - min)) + min;
 
export const randomFromArray = array => array[randomFromRange(0, array.length)];