-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from diasbruno/feature/array-repeat
feature: added array/repeat.
- Loading branch information
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters