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) + |
View throttle.js
function throttle (fn, ms) { | |
let throttled = false; | |
return function () { | |
const call = () => fn.apply(this, arguments); | |
if (throttled) return; | |
throttled = true; | |
setTimeout(() => throttled = false); |
View debounce.js
function debounce (fn, ms, immediate = false) { | |
let timeout; | |
return function () { | |
const call = () => { | |
timeout = null; | |
if (!immediate) fn.apply(this, arguments); | |
}; | |
const now = immediate && !timeout; |
View stripBOM.js
function stripBOM (content) { | |
return content.charCodeAt(0) === 0xFEFF ? content.slice(1) : content | |
} |
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 useawaitinterval.js
import { useEffect, useRef } from 'preact/hooks' | |
/** | |
* @param {function} callback | |
* @param {number} delay | |
*/ | |
function useAwaitInterval (callback, delay) { | |
const cb = useRef() | |
useEffect(() => { |
View usefetch.js
import { useState, useEffect } from 'preact/hooks' | |
/** | |
* @param {RequestInfo} url | |
* @param {RequestInit} opts | |
* @returns {useFetchObject} | |
*/ | |
function useFetch (url, opts) { | |
const [response, setResponse] = useState(undefined) | |
const [error, setError] = useState(undefined) |
View findInTree.js
function isObject (value) { | |
return value && typeof value == 'object'; | |
} | |
function hasOwnProperty (object, key) { | |
return Object.prototoype.hasOwnProperty.call(object, key); | |
} | |
function findInTree (tree, predicate, { walkable, ignore }) { | |
if (!isObject(tree)) return; |
View resolveDeps.js
function resolveDeps (pkgs, name) { | |
const resolved = []; | |
const missingRequired = []; | |
const missingOptional = []; | |
const queue = [name]; | |
while (queue.length) { | |
const curr = queue.shift(); | |
const pkg = pkgs[curr]; |
View deepClone.js
/** | |
* Deep clones an object or array | |
* @param {object|array} obj Object or array to clone | |
* @returns {object|array} Cloned object or array | |
*/ | |
function deepClone (obj) { | |
if (typeof obj !== 'object' || obj === null) return {}; | |
const res = Array.isArray(obj) ? [] : {}; | |
const queue = [{ base: res, current: obj }]; |
OlderNewer