-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdeline.js
34 lines (32 loc) · 996 Bytes
/
deline.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
function deline(strings, ...values) {
let raw;
if (typeof strings === 'string') {
raw = [strings];
} else {
({ raw } = strings);
}
const resultArr = [];
for (let i = 0; i < raw.length; i++) {
resultArr.push(raw[i].replace(/\\\n[ \t]*/g, '').replace(/\\`/g, '`'));
if (i < values.length) {
resultArr.push(values[i]);
}
}
const result = resultArr.join('').trim();
const lines = result.split('\n');
const ret = lines.reduce((accumulator, line, idx) => {
const lineTrimmed = line.trim();
if (accumulator.length > 0 && lineTrimmed === '' && accumulator[accumulator.length] === '\n') {
return accumulator;
}
if (lineTrimmed === '') {
accumulator.push(accumulator.pop().slice(0, -1));
accumulator.push('\n');
} else {
accumulator.push(`${lineTrimmed}${idx !== lines.length - 1 ? ' ' : ''}`);
}
return accumulator;
}, []);
return ret.join('').trim().replace(/\\n/g, '\n');
}
module.exports = deline;