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
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
/// Don't use interval less 5ms because web agents can't provide delay less than 4ms in due with optimization in nested setTimeout calls | |
/// But how the practice shows 5ms is lower border for this timer, because it accounts errors and evaluates corrected interval. That's is why 5ms the lowest interval of this timer. | |
function timer(callback, interval, duration, ...args) { | |
if (typeof callback !== "function") return; | |
const toNumberPositive = (value) => { | |
const n = Number(value); | |
return (n !== n || n < 0) ? 0 : n | |
}; | |
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
/// 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
/// Compares two objects and returns true or false | |
/// No protection for recursive structure of objects, don't compare recursive objects | |
/// Comparation doesn't provide exotic objects like Set, Map or Date, you should realize them by yourself if you want compare it too | |
/// Below there is demonstration how to realize and include exotic objects into main algorithm | |
function objectsEquality(specialTypes) { | |
specialTypes = sortOf(specialTypes) === "object" ? specialTypes : {}; | |
return function equality(o1, o2, options) { | |
/// if arguments that must be objects aren't them, value is returned null | |
if ([o1, o2].some((i) => typeof i !== "object")) return null; | |
/// Objects are equal, if objects have same reference to object |
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
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 hasPrototypeIterator(o){ | |
return o != null ? Object.hasOwnProperty.call(Object.getPrototypeOf(o), Symbol.iterator) : null; | |
} |
OlderNewer