Skip to content

Commit f158927

Browse files
authored
Merge pull request #258 from diasbruno/feature/array-repeat
feature: added array/repeat.
2 parents fcc0564 + 57a2ab4 commit f158927

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

src/array.js

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ return {
3838
'reject' : require('./array/reject'),
3939
'remove' : require('./array/remove'),
4040
'removeAll' : require('./array/removeAll'),
41+
'repeat' : require('./array/repeat'),
4142
'reverse' : require('./array/reverse'),
4243
'shuffle' : require('./array/shuffle'),
4344
'slice' : require('./array/slice'),

src/array/repeat.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
define(function() {
2+
3+
/**
4+
* Create an array of size N and fill with a value.
5+
* This function will throw an exception in case
6+
* you pass a negative number.
7+
*/
8+
function repeat(n, value) {
9+
var arr = new Array(n);
10+
return arr.fill(value);
11+
}
12+
13+
return repeat;
14+
});

tests/spec/array/spec-repeat.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
define(['mout/array/repeat'], function (repeat) {
2+
3+
describe('array/repeat()', function(){
4+
5+
it('should create an array of size 0.', function(){
6+
expect(repeat(0, 'a')).toEqual([]);
7+
});
8+
9+
it('should create an array of size 1 and fill with null.', function(){
10+
expect(repeat(1, null)).toEqual([null]);
11+
});
12+
13+
it('should create an array of size 1 and fill with a value "a".', function(){
14+
expect(repeat(1, 'a')).toEqual(['a']);
15+
});
16+
17+
it('should create an array of size N and fill with a value "a".', function(){
18+
expect(repeat(2, 'a')).toEqual(['a', 'a']);
19+
});
20+
21+
it('should create an array of size N and fill with values "albatros".', function(){
22+
expect(repeat(2, 'albatros')).toEqual(['albatros', 'albatros']);
23+
});
24+
25+
it('should create an array of size N and fill with a number 1.', function(){
26+
expect(repeat(2, 1)).toEqual([1, 1]);
27+
});
28+
29+
it('should create an array of size N and fill with array.', function(){
30+
expect(repeat(2, [1])).toEqual([[1], [1]]);
31+
});
32+
33+
it('should throw an exception in case the of a negative number.', function(){
34+
expect(() => repeat(-1, null)).toThrow();
35+
});
36+
});
37+
});

tests/spec/spec-array.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ define([
3636
'./array/spec-reject',
3737
'./array/spec-remove',
3838
'./array/spec-removeAll',
39+
'./array/spec-repeat',
3940
'./array/spec-reverse',
4041
'./array/spec-shuffle',
4142
'./array/spec-slice',

0 commit comments

Comments
 (0)