Home Reference Source

ts_temp/array/repeat.js

  1. import curryN from '../function/curryN';
  2. /**
  3. * Returns a fixed list of size `n` containing a specified identical value.
  4. *
  5. * @param {Number} n The desired size of the output list.
  6. * @param {*} value The value to repeat.
  7. * @return {Array} A new array containing `n` `value`s.
  8. * @example
  9. *
  10. * repeat(5, 'hi'); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
  11. *
  12. * var obj = {};
  13. * var repeatedObjs = repeat(5, obj); //=> [{}, {}, {}, {}, {}]
  14. * repeatedObjs[0] === repeatedObjs[1]; //=> true
  15. */
  16. export default curryN(2, (n = 0, value) => {
  17. const result = new Array(n);
  18. for (let i = 0; i < n; i++) {
  19. result[i] = value;
  20. }
  21. return result;
  22. });