-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreformat.js
48 lines (39 loc) · 855 Bytes
/
reformat.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
41
42
43
44
45
46
47
48
/**
* Separate s into digits and letters array.
* The longer array will be returned as the first element.
* @param {*} s
*/
const separate = (s) => {
const letters = []
const digits = []
for (let i = 0; i < s.length; i++) {
const charCode = s.charCodeAt(i)
if (charCode >= 48 && charCode <= 57) {
digits.push(s[i])
} else {
letters.push(s[i])
}
}
return (digits.length >= letters.length)
? [digits, letters]
: [letters, digits]
}
/**
* @param {string} s
* @return {string}
*/
const reformat = function (s) {
const [long, short] = separate(s)
if (long.length - short.length >= 2) {
return ''
}
const arr = []
while (long.length > 0) {
arr.push(long.shift())
if (short.length > 0) {
arr.push(short.shift())
}
}
return arr.join('')
}
module.exports = reformat