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 / 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 / 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 / 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 / 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 / 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 / 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;
}
@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 / replaceMatches.js
Created August 28, 2021 00:27
Text replacer, it replaces source string with special keywords on required values
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;
}
@dSalieri
dSalieri / closestPoint.js
Created August 31, 2021 06:16
Finds closest value in sorted array
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]};