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 isPromise(v) { | |
return v && typeof v.then === 'function' && Promise.resolve(v) === v && v instanceof Promise | |
} |
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 isNativeClass(v) { | |
return !Function.prototype.toString.call(v).indexOf('class') | |
} |
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
const elementIsVisibleInViewport = (el, partiallyVisible = false) => { | |
const { top, left, bottom, right } = el.getBoundingClientRect(); | |
const { innerHeight, innerWidth } = window; | |
return partiallyVisible | |
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) && | |
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) | |
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth; | |
}; |
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
const runAsync = fn => { | |
const worker = new Worker( | |
URL.createObjectURL(new Blob([`postMessage((${fn})());`]), { | |
type: 'application/javascript; charset=utf-8' | |
}) | |
); | |
return new Promise((res, rej) => { | |
worker.onmessage = ({ data }) => { | |
res(data), worker.terminate(); | |
}; |
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
const scrollToTop = () => { | |
const c = document.documentElement.scrollTop || document.body.scrollTop; | |
if (c > 0) { | |
window.requestAnimationFrame(scrollToTop); | |
window.scrollTo(0, c - c / 8); | |
} | |
}; | |
const smoothScroll = element => | |
document.querySelector(element).scrollIntoView({ |
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
const throttle = (fn, wait) => { | |
let inThrottle, lastFn, lastTime; | |
return function() { | |
const context = this, | |
args = arguments; | |
if (!inThrottle) { | |
fn.apply(context, args); | |
lastTime = Date.now(); | |
inThrottle = true; | |
} else { |
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 doSth() { | |
if (foo) { | |
doSomething() | |
return | |
} | |
if (bar) { | |
return bar | |
} | |
} | |
// ---------- |
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
/*! | |
* Determine if an element is in the viewport | |
* (c) 2017 Chris Ferdinandi, MIT License, https://gomakethings.com | |
* @param {Node} elem The element | |
* @return {Boolean} Returns true if element is in the viewport | |
*/ | |
var isInViewport = function (elem) { | |
var distance = elem.getBoundingClientRect(); | |
return ( | |
distance.top >= 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
const context = { | |
title: 'hello', | |
list: [1, 2, 3] | |
} | |
const template = ' \ | |
<header>${title}</header> \ | |
<ul>${list.map(i => `<li>${i}</li>`).join("")}</ul> \ | |
' |
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 Parent (name) { | |
this.name = name | |
} | |
Parent.prototype.getName = function getName () { return this.name } | |
function Child (name, age) { | |
Parent.call(this, name) | |
this.age = age | |
} | |
Child.prototype = Object.create(Parent.prototype, { |
OlderNewer