Skip to content

Instantly share code, notes, and snippets.

View mflorida's full-sized avatar
🛹
Shredding

Mark M. Florida mflorida

🛹
Shredding
View GitHub Profile
@mflorida
mflorida / resolveClassName.ts
Created April 15, 2024 23:52
Don't use clsx. Resolve className for component from a series of arguments as strings or arrays of strings.
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;
@mflorida
mflorida / insertAfterKey.js
Last active September 25, 2023 17:18
Insert new item into a Map object after specified key.
/**
* 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;
@mflorida
mflorida / arrayMinMax.js
Created September 14, 2023 14:52
Find min and max in array of values.
// 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]);
}
@mflorida
mflorida / parseTag.mjs
Last active August 29, 2023 17:03
Parse a tag string with attributes.
// 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;
// -----
/**
@mflorida
mflorida / x0.element.js
Created May 12, 2023 14:10
Create new or select existing DOM element.
/**
* Find or make the DOM things
*/
let createRE = /^<\s*|\s*\/*>$/g;
createRE = /^<\s*|\s*\/?>$/g;
function parseTag(tag) {
@mflorida
mflorida / mergeObjects.js
Last active April 30, 2023 08:20 — forked from Error601/mergeObjects.js
Merge/Extend Objects in native JS
/*!
* 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]';
@mflorida
mflorida / hipster-filler.js
Created April 1, 2022 18:48 — forked from Error601/hipster-filler.js
Hipster filler text generator.
/*!
* 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
@mflorida
mflorida / centeredPopup.js
Last active April 30, 2023 08:06
Open new (popup) window within the bounds of, and centered over, the calling parent window with max dimensions of 1400x900.
/**
* 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 });
@mflorida
mflorida / testSplit100.js
Last active August 10, 2021 20:01
Remove '100' by splitting and re-joining string until empty or equal to 100 ('yes'), or end result equals itself ('no').
function writeOutput(inputData) {
// Start writing code here to consume input, and return result.
// split lines of input string into an array
const inputParts = inputData.split('\n');
// get value of first element to serve as line count
const lineCount = Number(inputParts[0]);
// update this after we have a result to minimize time complexity (no nested loops)
@mflorida
mflorida / testWhile100.js
Created August 10, 2021 02:29
Remove '100' until string is either empty or not equal to '100'
function writeOutput(inputData) {
// Start writing code here to consume input, and return result.
// separate input for consumptino
const inputParts = inputData.split('\n');
// shift first element to serve as line count
const lineCount = Number(inputParts.shift());
// update this after we have a result to minimize time complexity (no nested loops)