Skip to content

Instantly share code, notes, and snippets.

View hperrin's full-sized avatar

Hunter Perrin hperrin

View GitHub Profile
function verses(bottles) {
const count = (b, cap) => `${b || `${cap ? 'N' : 'n'}o more`} bottle${b == 1 ? '' : 's'}`;
return [...new Array(bottles + 1)].map((v, i) => bottles - i).map(b => [
`${count(b, true)} of beer on the wall, ${count(b)} of beer.`,
b > 0
? `Take one down and pass it around, ${count(b - 1)} of beer on the wall.`
: `Go to the store and buy some more, ${count(bottles)} of beer on the wall.`
]);
}
@hperrin
hperrin / percenttwenty.js
Created August 12, 2021 21:57
Useful functions written in the most ridiculous way.
export const repeat = (str, num) => `${Array(num + 1)}`.replaceAll(/,/g, str);
export const kebabCase = (string, delimiter = " ") =>
string
.split(delimiter)
.map((word) => [
`${Number.NEGATIVE_INFINITY}`.slice(0, 1)[0],
word.toLowerCase(),
])
.flat()
@hperrin
hperrin / splitn.js
Last active January 12, 2022 07:35
splitn.js, a split function that returns everything after the limit in the last element.
export default const splitn = (s, d, n = Infinity) => {
const a = s.split(d);
return [
...a.slice(0, n - 1),
...(a.length >= n ? [a.slice(n - 1).join(d)] : [])
];
};