Skip to content

Commit

Permalink
Merge pull request #258 from diasbruno/feature/array-repeat
Browse files Browse the repository at this point in the history
feature: added array/repeat.
  • Loading branch information
roboshoes committed Nov 8, 2019
2 parents fcc0564 + 57a2ab4 commit f158927
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ return {
'reject' : require('./array/reject'),
'remove' : require('./array/remove'),
'removeAll' : require('./array/removeAll'),
'repeat' : require('./array/repeat'),
'reverse' : require('./array/reverse'),
'shuffle' : require('./array/shuffle'),
'slice' : require('./array/slice'),
Expand Down
14 changes: 14 additions & 0 deletions src/array/repeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
define(function() {

/**
* Create an array of size N and fill with a value.
* This function will throw an exception in case
* you pass a negative number.
*/
function repeat(n, value) {
var arr = new Array(n);
return arr.fill(value);
}

return repeat;
});
37 changes: 37 additions & 0 deletions tests/spec/array/spec-repeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
define(['mout/array/repeat'], function (repeat) {

describe('array/repeat()', function(){

it('should create an array of size 0.', function(){
expect(repeat(0, 'a')).toEqual([]);
});

it('should create an array of size 1 and fill with null.', function(){
expect(repeat(1, null)).toEqual([null]);
});

it('should create an array of size 1 and fill with a value "a".', function(){
expect(repeat(1, 'a')).toEqual(['a']);
});

it('should create an array of size N and fill with a value "a".', function(){
expect(repeat(2, 'a')).toEqual(['a', 'a']);
});

it('should create an array of size N and fill with values "albatros".', function(){
expect(repeat(2, 'albatros')).toEqual(['albatros', 'albatros']);
});

it('should create an array of size N and fill with a number 1.', function(){
expect(repeat(2, 1)).toEqual([1, 1]);
});

it('should create an array of size N and fill with array.', function(){
expect(repeat(2, [1])).toEqual([[1], [1]]);
});

it('should throw an exception in case the of a negative number.', function(){
expect(() => repeat(-1, null)).toThrow();
});
});
});
1 change: 1 addition & 0 deletions tests/spec/spec-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ define([
'./array/spec-reject',
'./array/spec-remove',
'./array/spec-removeAll',
'./array/spec-repeat',
'./array/spec-reverse',
'./array/spec-shuffle',
'./array/spec-slice',
Expand Down

0 comments on commit f158927

Please sign in to comment.