Skip to content

Instantly share code, notes, and snippets.

@hperrin
Created August 12, 2021 21:57
Show Gist options
  • Save hperrin/5aa118a81c4237a7ebeda6003738c71d to your computer and use it in GitHub Desktop.
Save hperrin/5aa118a81c4237a7ebeda6003738c71d to your computer and use it in GitHub Desktop.

Revisions

  1. hperrin created this gist Aug 12, 2021.
    50 changes: 50 additions & 0 deletions percenttwenty.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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("");