-
Notifications
You must be signed in to change notification settings - Fork 1
/
DEV#180.js
40 lines (33 loc) · 1.41 KB
/
DEV#180.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// https://dev.to/thepracticaldev/daily-challenge-180-sms-shortener-43lk
function shortener (str) {
const regex = /\s+(\S*)$/g
if (str.length <= 160) {
return str
};
const match = str.match(regex)
if (!match) {
return 'No place to trim'
}
const trimmed = match[0].trim()
const capital = trimmed.replace(/^./, trimmed[0].toUpperCase())
return shortener(str.replace(regex, capital))
};
// Tests
const cases = [
{
label: 'example#1',
input: shortener('No one expects the Spanish Inquisition! Our chief weapon is surprise, fear and surprise; two chief weapons, fear, surprise, and ruthless efficiency! And that will be it.'),
shouldBe: 'No one expects the Spanish Inquisition! Our chief weapon is surprise, fear and surprise; two chief weapons, fear,Surprise,AndRuthlessEfficiency!AndThatWillBeIt.'
},
{
label: 'example#2',
input: shortener('This message is already short enough'),
shouldBe: 'This message is already short enough'
},
{
label: 'example#3',
input: shortener('ThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoonThereIsNoSpoon'),
shouldBe: 'No place to trim'
}
]
cases.map(test => console.log((test.input === test.shouldBe ? '✅' : '❗') + ' ' + test.label + ' => ' + 'Should be: ' + test.shouldBe + ' received: ' + test.input))