Created
August 12, 2021 21:57
-
-
Save hperrin/5aa118a81c4237a7ebeda6003738c71d to your computer and use it in GitHub Desktop.
Useful functions written in the most ridiculous way.
This file contains 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 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