Comparing version 7.0.0 to 7.1.0
{ | ||
"name": "utilsac", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"description": "JavaScript General Purpose Utility Functions", | ||
@@ -11,4 +11,3 @@ "main": "files.js", | ||
"utility", | ||
"files", | ||
"experimentation" | ||
"files" | ||
], | ||
@@ -15,0 +14,0 @@ "author": "GrosSacASacs", |
@@ -35,2 +35,3 @@ # JavaScript General Purpose Utility Functions | ||
chainPromises, | ||
chainRequestAnimationFrame, | ||
doNTimes, | ||
@@ -37,0 +38,0 @@ chainPromiseNTimes, |
@@ -7,2 +7,3 @@ export { | ||
chainPromises, | ||
chainRequestAnimationFrame, | ||
doNTimes, | ||
@@ -21,3 +22,3 @@ chainPromiseNTimes, | ||
the returned function always return undefined | ||
TODO is debounced the correct word ? | ||
@@ -43,3 +44,3 @@ */ | ||
the returned function always return undefined | ||
an alternative implementation could use Date.now() , this means less performance | ||
@@ -58,3 +59,3 @@ but would work for throttling inside a single long tick | ||
functionToThrottle(...args); | ||
setTimeout(makeReady, minimumTimeSpace); | ||
setTimeout(makeReady, minimumTimeSpace); | ||
}; | ||
@@ -71,3 +72,3 @@ }; | ||
roundStep02(5); --> 5 (already rounded) | ||
warning: can have small errors due to fixed precision floats */ | ||
@@ -88,14 +89,16 @@ const halfPrecision = precision / 2; | ||
const fillArrayWithFunctionResult = function (aFunction, times) { | ||
// [].fill is for static values only | ||
const returnArray = []; | ||
let i; | ||
for (i = 0; i < times; i += 1) { | ||
returnArray.push(aFunction()); | ||
/* [].fill is for static values only | ||
alternative , return Array.from({length: times}, aFunction); | ||
same if aFunction ignores its second argument | ||
*/ | ||
const array = []; | ||
for (let i = 0; i < times; i += 1) { | ||
array.push(aFunction()); | ||
} | ||
return returnArray; | ||
return array; | ||
}; | ||
const doNTimes = function (task, times) { | ||
let i; | ||
for (i = 0; i < times; i += 1) { | ||
for (let i = 0; i < times; i += 1) { | ||
task(); | ||
@@ -129,2 +132,25 @@ } | ||
const chainRequestAnimationFrame = function (functions) { | ||
return new Promise(function (resolve, reject) { | ||
const values = []; | ||
const length = functions.length; | ||
let i = 0; | ||
const next = function (timing) { | ||
i += 1; | ||
if (i < length) { | ||
try { | ||
values.push(functions[i]()); | ||
} catch (error) { | ||
reject(error); | ||
return; | ||
} | ||
requestAnimationFrame(next); | ||
} else { | ||
resolve(values); | ||
} | ||
}; | ||
next(); | ||
}); | ||
}; | ||
const chainPromiseNTimes = function (promiseCreator, times) { | ||
@@ -135,3 +161,3 @@ // different than Promise.all | ||
// resolves with an array of values | ||
// could be made with chainPromises, but chose not to | ||
@@ -150,5 +176,5 @@ // to avoid an adapter array | ||
promiseCreator().then(chainer); | ||
} else { | ||
resolve(values); | ||
return; | ||
} | ||
resolve(values); | ||
}; | ||
@@ -155,0 +181,0 @@ promiseCreator().then(chainer); |
22576
432
66