Skip to content

Instantly share code, notes, and snippets.

@minifyre
minifyre / grammarly2markdown.js
Last active March 7, 2024 15:04
Converts text in the Grammarly editor to markdown. To use it, paste the code into the console & copy the resulting string's contents.
$('.ql-editor[contenteditable="true"]').innerHTML
/** @note escape characters */
.replace(/\#/g, '\\#')
.replace(/\*/g, '\\*')
/** @note convert headings */
.replace(/<\/h[1-6]>/g, '\n')
.replace(/<h1>/g, '# ')
.replace(/<h2>/g, '## ')
.replace(/<h3>/g, '### ')
.replace(/<h4>/g, '#### ')
@minifyre
minifyre / hooks.ts
Last active January 15, 2021 19:55
export const useStateChangeEffect = <S>(
initialState: S,
effect: (arg: S) => void,
runIf: (arg: S) => boolean = isTrue => !!isTrue,
) => {
const [state, setState] = useState(initialState);
const prevState = useRef(state);
const triggerEffect = runIf(state) && state !== prevState.current;
useEffect(() => void (triggerEffect && effect(state)), [effect, state, triggerEffect]);
@minifyre
minifyre / object.ts
Created December 19, 2020 20:54
Object Utils
const renameObjKey = <T extends Record<string, any>>(
oldKey: string,
newKey: string,
{[oldKey]: value, ...obj}: T,
) => Object.assign({[newKey]: value}, obj);
@minifyre
minifyre / array.ts
Created December 18, 2020 01:22
Array Utils
// note: the existing item needs to appear after so that this can put stuff at the start of the array
// note: if an i > length is used, it will just be added to the last place
const insertAt = <T>(arr: T[], i:number, item: T) =>
arr.slice(0, i).concat([item]).concat(arr.slice(i)); // prettier-ignore
const removeAt = <T>(arr: T[], i: number) =>
arr.slice(0, i).concat(arr.slice(i + 1)) // prettier-ignore
const setAt = <T>(arr: T[], i: number, item: T) =>
arr.slice(0, i).concat([item]).concat(arr.slice(i + 1)); // prettier-ignore
items.map((result, item, i, {[i+1]: nextItem}) => [item, nextItem]);
import {useState} from 'react';
export const useVector = <T>(initialValue: T, arity = 3) => {
const [vector, setVector] = useState<T[]>([...Array(arity)].fill(initialValue));
const updateVector = (newValue: T) => setVector([newValue].concat(vector.slice(1)));
return [vector, updateVector] as const;
};
/\
_\/_/
/ /
/\
/ /
@minifyre
minifyre / space-case.js
Created January 24, 2020 01:27
Convert PascalCase, camelCase, snake_case, & kebab-case into "space case".
export const spaceCase = txtWithNoSpaces =>
txtWithNoSpaces
.split(/_|-/g) // snake_case & kebab-case
.join(' ')
.replace(/([A-Z])/g, ' $1') // PascalCase & camelCase
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
.replace(/\s+/, ' ') // dashed entries create two spaces, bump that down to one
.replace(/([A-Z])\s(?=[A-Z]\b)/g, '$1') // e.g. "U U I D" => "UUID"
.hue-gradient
{
background:linear-gradient(to right,
#f00 0%,
#ff0 16.66%,
#0f0 33.33%,
#0ff 50%,
#00f 66.66%,
#f0f 83.33%,
#f00 100%
@minifyre
minifyre / colorblind-gradients.html
Last active May 26, 2021 02:58
A visual representation of different types of colorblindness & their affects on color gradients (filters adapted from this awesome repo at https://github.com/oftheheadland/Colorblindly).
<!Doctype html>
<style>
div
{
background:linear-gradient(to right,
#f00 0%,
#ff0 16.66%,
#0f0 33.33%,
#0ff 50%,
#00f 66.66%,