Skip to content

Instantly share code, notes, and snippets.

@kerrishotts
Created August 6, 2019 20:26
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 kerrishotts/e3a21352657b6be011d554e30f62597d to your computer and use it in GitHub Desktop.
Save kerrishotts/e3a21352657b6be011d554e30f62597d to your computer and use it in GitHub Desktop.
Dev.To 2019 Aug 6 Challenge
const TRANSFORMER = {
LOWER: String.prototype.toLowerCase,
UPPER: String.prototype.toUpperCase
};
const transform = (v, which) => which.apply(v);
const weirdCase = str =>
str.split(" ")
.map((part) => Array.from(part)
.map((ch, idx) => transform(ch, idx & 1 ? TRANSFORMER.LOWER
: TRANSFORMER.UPPER ))
.join(""))
.join(" ");
const assert = (exprFn, expected, msg) => {
const actual = exprFn();
if (actual !== expected) {
throw new Error(`${msg}; got ${actual}; expected ${expected}`);
}
}
assert(() => weirdCase(""), "", "Empty");
assert(() => weirdCase(" "), " ", "Spaces");
assert(() => weirdCase("HELLO"), "HeLlO", "All Caps");
assert(() => weirdCase("hello"), "HeLlO", "All lower case");
assert(() => weirdCase("HellO"), "HeLlO", "Mixed case");
assert(() => weirdCase("Weird string case"), "WeIrD StRiNg CaSe", "Test Case");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment