-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (24 loc) · 614 Bytes
/
index.js
File metadata and controls
32 lines (24 loc) · 614 Bytes
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
'use strict';
const format = str => {
let size = -1;
return str.replace(/\n(\s+)/g, (m, m1) => {
if (size < 0) {
size = m1.replace(/\t/g, ' ').length;
}
return '\n' + m1.slice(Math.min(m1.length, size));
});
};
const dedentify = (callSite, ...args) => {
if (typeof callSite === 'string') {
return format(callSite);
}
if (typeof callSite === 'function') {
return (...args) => format(callSite(...args));
}
const output = callSite
.slice(0, args.length + 1)
.map((text, i) => (i === 0 ? '' : args[i - 1]) + text)
.join('');
return format(output);
};
module.exports = dedentify;