Skip to content

Instantly share code, notes, and snippets.

View hackash's full-sized avatar
🦇
Good stuff

Ashot hackash

🦇
Good stuff
View GitHub Profile
@hackash
hackash / function cache
Created October 16, 2016 16:46
Caching function return value in javascript
function multiply(a, b, c) {
if (!multiply.cache) {
multiply.cache = {};
}
var cachedArgs = JSON.stringify(arguments);
if (multiply.cache[cachedArgs]) {
return multiply.cache[cachedArgs];
}
return multiply.cache[cachedArgs] = a * b * c;
@hackash
hackash / animation 60FPS
Last active October 8, 2016 21:53
60 frames per second (60FPS) based animation in javascript.
(function () {
var config = {
elem: document.querySelector('.moveable'), // absolute or relative positioned DOM node
states: [],
speed: 2, // pixel per frame
loop: true,
target: {
from: 0,
to: 500
}
@hackash
hackash / Url relative to
Last active October 7, 2016 21:18
Extending string prototype with a function , that returns url relative to gives as an argument.
String.prototype.startsWith = function (input) {
return this.substring(0, input.length) === input;
};
String.prototype.relativeTo = function (input) {
var toTop = /..\//gi;
var abs = /^https?:\/\//i;
var inCurrent = './';
var matches;
@hackash
hackash / Flatten an array
Last active June 14, 2020 07:58
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers.
function isArray(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
}
function arrToFlat(arr, arrToReturn) {
if (!isArray(arr)) {
return false;
}
if (!arrToReturn) {
arrToReturn = [];