Skip to content

Instantly share code, notes, and snippets.

View dSalieri's full-sized avatar
😒
Dark guardian always there!

dSalieri

😒
Dark guardian always there!
View GitHub Profile
@dSalieri
dSalieri / slice.js
Last active July 16, 2021 17:58
Slice method that one may be able return reverse result
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;
@dSalieri
dSalieri / sliceAdvanced.js
Last active July 16, 2021 17:55
less fast than slice, but you have two arrays in one object that may be useful for some operation
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) {
@dSalieri
dSalieri / timer.js
Last active March 15, 2024 12:24
Timer based on setTimeout
/// 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
};
@dSalieri
dSalieri / getHz.js
Last active July 19, 2021 16:15
If you want to know hz of your monitor
/// 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))));
})
@dSalieri
dSalieri / rafTimer.js
Last active July 25, 2021 19:00
Timer based on requestAnimationFrame
/// 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;
@dSalieri
dSalieri / objectsEquality.js
Last active September 22, 2021 06:22
Compares two objects on equality
/// 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
@dSalieri
dSalieri / throttle.js
Created July 23, 2021 16:42
Light template of throttle
function throttle(func, delay) {
let timerId;
return function (args) {
if (timerId == null) {
func(args);
timerId = setTimeout(function () {
timerId = null;
}, delay);
}
};
@dSalieri
dSalieri / debounce.js
Last active July 24, 2021 23:36
Light template of debounce
function debounce(func, delay) {
let timerId;
return function (args) {
clearTimeout(timerId);
timerId = setTimeout(function () {
func(args);
}, delay);
};
}
@dSalieri
dSalieri / toggleAttribute.js
Last active August 23, 2021 17:06
It's a little bit different toggle function, it toggles attribute in HTMLElement and sets value for it
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;
}
}
@dSalieri
dSalieri / hasPrototypeIterator.js
Last active August 23, 2021 04:03
Returns true/false if object has iterator in prototype
function hasPrototypeIterator(o){
return o != null ? Object.hasOwnProperty.call(Object.getPrototypeOf(o), Symbol.iterator) : null;
}