Skip to content

Commit

Permalink
added repeat() method to transform
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Oct 29, 2020
1 parent 4f9dba7 commit 745a471
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,27 @@ function palindrome(arr=[0], noDouble=false){
exports.palindrome = palindrome;
exports.mirror = palindrome;

// repeat the values of an array n-times
// Using a second array for repeat times iterates over that array
//
// @param {Array} -> array with values to repeat
// @param {Int/Array} -> array or number of repetitions per value
// @return {Array}
//
function repeat(arr=[0], rep=1){
arr = (Array.isArray(arr))? arr : [arr];
rep = (Array.isArray(rep))? rep : [rep];
let a = [];
for (let i in arr){
let r = rep[i % rep.length];
for (k=0; k<r; k++){
a.push(arr[i]);
}
}
return a;
}
exports.repeat = repeat;

// reverse the order of items in an Array
//
// @param {Array} -> array to reverse
Expand Down
5 changes: 4 additions & 1 deletion test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ markov.clear();
console.log(markov.table);
*/

fullTest();
console.log(Mod.repeat([10, 20, 30], 0));
console.log(Mod.repeat([10, 20, 30], [1, 4, 2, 0]));

// fullTest();

function fullTest(){
console.time('Total Time');
Expand Down

0 comments on commit 745a471

Please sign in to comment.