This file contains hidden or 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
/** | |
* Fancy ID generator that creates 20-character string identifiers with the following properties: | |
* | |
* 1. They're based on timestamp so that they sort *after* any existing ids. | |
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs. | |
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly). | |
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the | |
* latter ones will sort after the former ones. We do this by using the previous random bits | |
* but "incrementing" them by 1 (only in the case of a timestamp collision). | |
*/ |
This file contains hidden or 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
/** | |
* Pick random item from an iterable object | |
* @param {string|Array|Map|Iterable} items | |
* @returns {*} | |
*/ | |
export function pickRandom(items) { | |
return [...items][Math.floor(Math.random() * (items.length))]; | |
} |
This file contains hidden or 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 resolveClassName(className: string | string[], ...more: (string | string[])[]): string { | |
return Array.from( | |
new Set( | |
[''].concat(className, more.flat()).filter(Boolean).join(' ').split(/\s+/) | |
) | |
).reduce((classes, cur) => { | |
if ((cur = cur.trim())) { | |
classes.push(cur); | |
} | |
return classes; |
This file contains hidden or 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
/** | |
* Insert 'insert' 2-D array after 'after' key in 'map' | |
* WARNING: THIS MUTATES THE ORIGINAL Map! | |
* @param {Map} map - Map to insert into | |
* @param {any} after - key to insert after | |
* @param {[any, any]} insert - 2-D array for entry to insert | |
* @returns {Map} - returns mutated 'map' passed as first argument | |
*/ | |
function insertAfterKey(map, after, insert){ | |
const [insertKey, insertValue] = insert; |
This file contains hidden or 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
// Return a tuple of the minimum and maximum values in `arr` array | |
function arrayMinMax(arr){ | |
return [...arr].reduce(([min, max], value) => { | |
return [ | |
value < min ? value : min, | |
value > max ? value : max | |
]; | |
}, [Infinity, -Infinity]); | |
} |
This file contains hidden or 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
// Export regular expressions used in this module that other modules may need. | |
// ----- | |
// Characters used to indicate a `#document-fragment` should be created | |
export const fragRE = /^([!#]+|<>|<\/>)/; | |
// Characters that indicate a new element should be created... | |
// ...possibly from another library, like: parseTag('<div>'); | |
export const createRE = /^[\s<+=]*|[/>\s]*$/g; | |
// ----- | |
/** |
This file contains hidden or 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
/** | |
* Find or make the DOM things | |
*/ | |
let createRE = /^<\s*|\s*\/*>$/g; | |
createRE = /^<\s*|\s*\/?>$/g; | |
function parseTag(tag) { | |
This file contains hidden or 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
/*! | |
* Replace some functionality of jQuery's $.extend() method | |
* with only native JavaScript. Unlike $.extend(), this | |
* function will ONLY merge plain objects (not arrays). | |
* https://gist.github.com/Error601/9a181a0b9f414b752c38 | |
*/ | |
function isObject( obj ){ | |
return Object.prototype.toString.call(obj) === '[object Object]'; |
This file contains hidden or 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
/*! | |
* Hipster filler text generator. | |
* Inspired by (and words taken from): | |
* http://hipsum.co/ | |
* | |
* Usage: | |
* | |
* // sentences only | |
* APP.utils.hipster.sentence(); // generates 1 sentence with 9, 12, or 18 words | |
* APP.utils.hipster.sentences(3); // generates 3 sentences |
This file contains hidden or 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
/** | |
* Open a new browser window centered to the parent window. | |
* @param {string} url - Required URL for popup window | |
* @param {string} [name] - optional window name | |
* @param {object} [cfg] - optional config object for window.open parameters | |
* @returns {*} - returns reference to popup | |
* @example | |
* | |
* // open a 600x400 popup named 'foo' centered over parent window: | |
* centeredPopup('/page/foo', 'foo', { width: 600, height: 400 }); |
NewerOlder