Skip to content

Instantly share code, notes, and snippets.

@hperrin
Created August 12, 2021 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hperrin/5aa118a81c4237a7ebeda6003738c71d to your computer and use it in GitHub Desktop.
Save hperrin/5aa118a81c4237a7ebeda6003738c71d to your computer and use it in GitHub Desktop.
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()
.slice(1)
.map(String)
.join("");
export const titleCase = (string, delimiter = " ") =>
string
.split(delimiter)
.filter(Boolean)
.reduce(
(obj, word) => {
obj.f.push(word.slice(0, 1));
obj.r.push(word.slice(1));
return obj;
},
{
f: [],
r: [],
go: function () {
this.f = this.f.join("").toUpperCase().split("");
return this.f
.reduce((arr, cur, i) => {
arr.push(cur);
arr.push(this.r[i].toLowerCase());
return arr;
}, [])
.join("");
},
}
)
.go();
export const camelCase = (string, delimiter = " ") =>
string
.split(delimiter)
.map((word) => [word.slice(0, 1), word.slice(1)])
.flat()
.map((part, index) =>
part[["toUpperCase", "toLowerCase"][Math.max(index, 1) % 2]]()
)
.join("");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment