Created
June 22, 2022 10:05
-
-
Save worldoptimizer/ef38b989bbe76f219c77d2aba1cd9c68 to your computer and use it in GitHub Desktop.
This function creates another function that can be used to loop through a set of steps.
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 function creates another function that can be used to loop through a set of steps. | |
* This could be useful, for example, in creating animations or a set of instructions that need to be followed in order. | |
* | |
* @param {Array} arr - The array of steps to be looped through | |
* @param {number} i - The index to start at | |
* @param {function} callback - The function to be called on each step | |
* @param {string} key - The key to use for the object | |
* @returns {function} - A function that can be used to loop through the steps | |
*/ | |
function createSequence(arr, i, callback, key) { | |
i = i || 0; | |
const steps = []; | |
arr.forEach(step => { | |
if (Array.isArray(step)) { | |
for (let j = 0; j < step[0]; j++) { | |
steps.push(step[1]); | |
} | |
} else { | |
steps.push(step); | |
} | |
}); | |
return function(n) { | |
n = n == undefined ? 0 : n; | |
if (typeof n === "string") i = n = parseInt(n); | |
if (i >= steps.length) i = i % steps.length; | |
if (i < 0) i = steps.length + (i % steps.length); | |
let step = steps[i]; | |
i += n; | |
if (typeof step === "function") step = step(); | |
switch (typeof callback) { | |
case "function": | |
callback(step); | |
break; | |
case "object": | |
if (key) callback[key] = step; | |
break; | |
} | |
return step; | |
}; | |
} | |
// Simple usage | |
// create a sequence of months | |
const months = createSequence(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]); | |
// calling | |
console.log(months()) //gives you the current month | |
console.log(months(1)) //gives you the current month and advances one | |
// create a sequence of letters and set the value of the object's "letter" property to the current letter | |
const myObject = {}; | |
const letterSequence = createSequence(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"], 0, myObject, "letter"); | |
// calling | |
console.log(letterSequence(), myObject) //gives you the current letter, also sets it as myObject.letter | |
console.log(letterSequence(1), myObject) //gives you the current letter and advances one, also sets it as myObject.letter | |
// create a sequence of numbers and set call a callback on it | |
const numberSequence = createSequence([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0, function(step) { | |
console.log("Do something with step like multiply by 5 = ", step * 5); | |
}); | |
// calling | |
console.log(numberSequence()) //gives you the current number, calls callback with it | |
console.log(numberSequence(1)) //gives you the current number and advances one, calls callback with it | |
// demo of a sequence with a number on the times a item should be inserted | |
const repeatDemoSequence = createSequence([[2,'Hello'], [4,'World']]); | |
// calling repeatDemoSequence(1) repeatedly would output | |
// "Hello", "Hello", "World", "World", "World", "World" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment