Skip to content

Instantly share code, notes, and snippets.

😴
Help me get productive, thanks.

intrnl

😴
Help me get productive, thanks.
  • Indonesia
Block or report user

Report or block intrnl

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View classnames.js
function classnames (...classes) {
const arr = []
for (const name of classes) {
if (typeof name === 'string') {
arr.push(name)
}
else if (typeof name === 'object' && name !== null) {
for (let [key, value] of Object.entries(name)) {
if (value) arr.push(key)
View stripBOM.js
function stripBOM (content) {
return content.charCodeAt(0) === 0xFEFF ? content.slice(1) : content
}
View sleep.js
function sleep (ms) {
return new Promise((res) => setTimeout(res, ms))
}
@intrnl
intrnl / createElement.js
Created Jun 28, 2019
Mimics React.createElement
View createElement.js
function createElement (tag, props, ...childrens) {
const elem = Object.assign(document.createElement(tag), props)
for (const child of childrens) {
if (child instanceof HTMLElement) elem.append(child)
else elem.append(document.createTextNode(child))
}
return elem
}
@intrnl
intrnl / debounce.js
Last active Jun 30, 2019
Function debouncing
View debounce.js
function debounce (fn, delay, immediate = false) {
let timeout
return function () {
const now = immediate && (
!timeout || timeout._destroyed || timeout._called
)
const call = () => fn.apply(this, arguments)
if (now) {
@intrnl
intrnl / throttle.js
Last active Jun 30, 2019
Function throttling
View throttle.js
function throttle (fn, delay) {
let timeout
return function () {
const now = !timeout || timeout._destroyed || timeout._called
const call = () => fn.apply(this, arguments)
if (!now) return
call()
@intrnl
intrnl / stringReplaceAsync.js
Last active Jun 28, 2019
Asynchronous string replace
View stringReplaceAsync.js
function stringReplaceAsync (string, regex, replacer) {
if (!regex.global) {
const match = regex.exec(string)
if (!match) return string
const retVal = replacer(...match, match.index, string)
if (retVal instanceof Promise) {
return retVal.then((ret) => (
string.slice(0, match.index) +
You can’t perform that action at this time.