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
function sliceAdvanced(arr, start, end) { | |
arr = createArrayFromArrayLike(arr); | |
let boundary1 = start === undefined || arr.length + start < 0 ? 0 : start < 0 ? arr.length + start : start > arr.length ? arr.length : start; | |
let boundary2 = end === undefined || end > arr.length ? arr.length : end > 0 ? end : arr.length + end < 0 ? 0 : arr.length + end; | |
let actualLength = boundary2 - boundary1 < 0 ? 0 : boundary2 - boundary1; | |
let slicedArr = Array(actualLength); | |
let restArr = Array(arr.length - actualLength); | |
let key = 0; | |
let shiftForRest = 0; | |
while (key < arr.length) { |
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
function slice(arr) { | |
arr = createArrayFromArrayLike(arr); | |
return function (start, end, flag) { | |
let boundary1 = typeof start === "boolean" || start === undefined || arr.length + start < 0 ? 0 : start < 0 ? arr.length + start : start > arr.length ? arr.length : start; | |
let boundary2 = typeof end === "boolean" || end === undefined || end > arr.length ? arr.length : end > 0 ? end : arr.length + end < 0 ? 0 : arr.length + end; | |
let inverseFlag = typeof start === "boolean" ? start : typeof end === "boolean" ? end : flag; | |
let shift = boundary2 - boundary1; | |
if (inverseFlag) { | |
let slicedArr = Array(arr.length - shift); | |
let key = 0; |
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
/// Complexity can't be less than 3 because sometimes happens lags and can get incorrect value, so minimal is 3, not less | |
async function getHz(complexity, approach) { | |
complexity = (complexity < 3 ? 3 : complexity) || 5; | |
approach = approach || "average"; | |
let arr = []; | |
while (arr.length < complexity) { | |
let value = Math.floor( | |
await new Promise((resolve, reject) => { | |
requestAnimationFrame((time1) => requestAnimationFrame((time2) => resolve(1000 / (time2 - time1)))); | |
}) |
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
function throttle(func, delay) { | |
let timerId; | |
return function (args) { | |
if (timerId == null) { | |
func(args); | |
timerId = setTimeout(function () { | |
timerId = null; | |
}, delay); | |
} | |
}; |
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
function debounce(func, delay) { | |
let timerId; | |
return function (args) { | |
clearTimeout(timerId); | |
timerId = setTimeout(function () { | |
func(args); | |
}, delay); | |
}; | |
} |
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
/// If you will use extremely low values, actual interval will be equal to 1000/HZ of your monitor in the other case interval will be equal to settled by you | |
function rafTimer(callback, interval, duration) { | |
interval = interval || 1; | |
duration = duration || Infinity; | |
let startTime = performance.now(); | |
let endInterval = startTime; | |
let removeId = null; | |
(function step() { | |
let endIntervalStep = endInterval - startTime; | |
let currentInterval = performance.now() - startTime; |
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
function hasPrototypeIterator(o){ | |
return o != null ? Object.hasOwnProperty.call(Object.getPrototypeOf(o), Symbol.iterator) : null; | |
} |
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
function toggleAttribute(el, name, value) { | |
if(!(el instanceof HTMLElement)) return null; | |
if (el.hasAttribute(name)) { | |
el.removeAttribute(name); | |
return false; | |
} else { | |
el.setAttribute(name, value != null ? value : ""); | |
return true; | |
} | |
} |
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
function replaceMatches(template, objReplacer, objData){ | |
if(typeof template !== "string") return null; | |
if(typeof objReplacer !== "object") return template; | |
objData = objData || {}; | |
for(let key of Object.keys(objReplacer)){ | |
if(!Object.hasOwnProperty.call(objData, key)) continue; | |
template = template.replace(new RegExp(objReplacer[key],"g"), objData[key]) | |
} | |
return template; | |
} |
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
function closestPoint(arr, point){ | |
if(!Array.isArray(arr)) return []; | |
point = Number.isFinite(point) ? point : 0; | |
let prevDiff = Infinity; | |
let result = arr.findIndex((value,key) => { | |
let diff = Math.abs(value - point); | |
return diff >= prevDiff ? true : (prevDiff = diff, false); | |
}); | |
result = result === -1 ? arr.length - 1 : result - 1; | |
return {key: result, value: arr[result]}; |
OlderNewer