Skip to content

Instantly share code, notes, and snippets.

View edgarberm's full-sized avatar
:octocat:
NaN

Edgar Bermejo edgarberm

:octocat:
NaN
View GitHub Profile
function throttle ( fn, threshhold, scope ) {
threshhold || (threshhold = 250);
var last,
deferTimer;
return function () {
var context = scope || this;
var now = +new Date,
args = arguments;
@edgarberm
edgarberm / ObjectAssign.js
Created November 4, 2015 12:24
For those browsers that don't support Object.assign yet:
if( !Object.assign ) {
Object.assign = function( obj, items ) {
var src = Object( items), target = Object( obj )
Object.getOwnPropertyNames( src ).forEach( function( k ) {
target[ k ] = src[ k ]
})
return target;
}
}
@edgarberm
edgarberm / memoize.js
Created March 28, 2016 11:50
Memoization for JavaScript
/*
* memoize.js
* by @philogb and @addyosmani
* with further optimizations by @mathias
* and @DmitryBaranovsk
* perf tests: http://bit.ly/q3zpG3
* Released under an MIT license.
*/
function memoize( fn ) {
return function () {
@edgarberm
edgarberm / isScrolledIntoView.js
Last active April 5, 2016 16:37
Check if element is into view
function isScrolledIntoView (el) {
var elemTop = el.getBoundingClientRect().top;
var elemBottom = el.getBoundingClientRect().bottom;
var isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
return isVisible;
}
@edgarberm
edgarberm / Object.diff.js
Last active July 14, 2016 15:46
Find differences between two objects
const contains = (key, list) => {
return list.indexOf(key) > -1;
}
const diff = (first, second) => {
let firstKeys = Object.keys(first);
let secondKeys = Object.keys(second);
let addition = secondKeys.filter(key => !contains(key, firstKeys))
let deletion = firstKeys.filter(key => !contains(key, secondKeys))
return {addition, deletion}
@edgarberm
edgarberm / assignPrototypeMethods.js
Created July 14, 2016 15:44
Copy the prototype methods of the given constructor method into the given context, if they do not already exist.
const assignPrototypeMethods = ( constructorMethod, context ) => {
// Loop over the BaseController's prototype methods and assign them to the current context
for ( var methodName in constructorMethod.prototype ) {
if (constructorMethod.prototype.hasOwnProperty( methodName ) && !context[ methodName ]) {
// Copy the method into the current controller context.
context[ methodName ] = constructorMethod.prototype[ methodName ];
}
}
}
@edgarberm
edgarberm / range.js
Created August 4, 2016 10:28
Returns a numeric range by passing the upper limit
range (num) {
return [...Array(num).keys()]
}
@edgarberm
edgarberm / clamp.js
Created August 4, 2016 10:29
Returns a number whose value is limited to the given range
clamp (n, min, max) {
return Math.max(Math.min(n, max), min)
}
@edgarberm
edgarberm / now.js
Created September 5, 2016 06:28
Get formatted date
export const repeat = (str, times) => (new Array(times + 1)).join(str);
export const padStart = (num, maxLength, char = ' ') => repeat(char, maxLength - num.toString().length) + num;
export const formatTime = (time) => {
const h = padStart(time.getHours(), 2, '0');
const m = padStart(time.getMinutes(), 2, '0');
const s = padStart(time.getSeconds(), 2, '0');
const ms = padStart(time.getMilliseconds(), 3, '0');
return `${h}:${m}:${s}.${ms}`;
@edgarberm
edgarberm / roman.js
Last active September 29, 2016 08:40
Converts a number to a Roman numeral string
const roman = (n) => {
n = String(Math.floor(Math.abs(n)))
let element, i, result = ''
const table = [
['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'],
['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
]
for (i = 0; i < table.length; i += 1) {