Created
August 6, 2019 20:26
-
-
Save kerrishotts/e3a21352657b6be011d554e30f62597d to your computer and use it in GitHub Desktop.
Dev.To 2019 Aug 6 Challenge
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
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