forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeat.ts
More file actions
26 lines (23 loc) · 738 Bytes
/
repeat.ts
File metadata and controls
26 lines (23 loc) · 738 Bytes
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 curryN from '../function/curryN';
import { repeat } from '../typings/types';
/**
* Returns a fixed list of size `n` containing a specified identical value.
*
* @param {Number} n The desired size of the output list.
* @param {*} value The value to repeat.
* @return {Array} A new array containing `n` `value`s.
* @example
*
* repeat(5, 'hi'); //=> ['hi', 'hi', 'hi', 'hi', 'hi']
*
* var obj = {};
* var repeatedObjs = repeat(5, obj); //=> [{}, {}, {}, {}, {}]
* repeatedObjs[0] === repeatedObjs[1]; //=> true
*/
export default curryN(2, (n = 0, value) => {
const result = new Array(n);
for (let i = 0; i < n; i++) {
result[i] = value;
}
return result;
}) as typeof repeat