-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
61 lines (43 loc) · 1.73 KB
/
utilities.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
49
50
51
52
53
54
55
56
57
58
59
60
61
const slug = require('speakingurl');
exports.camelCase = string => string.replace(/_[a-z]/g, boundary => boundary.charAt(1).toUpperCase());
exports.crop = text => {
text = text.replace(/^\s*\n|\n\s*$/g, '');
lines = text.split('\n');
const depth = lines.reduce((minDepth, line) => {
const leadingWhitespace = line.match(/^\s*(?=\S)/);
if(leadingWhitespace) {
const depth = leadingWhitespace[0].length;
if(minDepth === null || depth < minDepth)
return depth;
}
return minDepth;
}, null);
return text.split('\n').map(line => line.substr(depth)).join('\n');
};
exports.escapeSingleQuotes = string => string.replace(/'/g, "\\'");
exports.filenamify = string => slug(string, { custom: { '-': '_' }, separator: '_' });
exports.interpolatify = (strings, ...variables) => {
let interpolated = '';
for(const [index, string] of strings.entries()) {
if(index === strings.length - 1) {
interpolated += string.replace(/\n\s*$/, ''); // strip trailing empty lines
} else {
if(index === 0) {
interpolated += string.replace(/^\s*\n/, ''); // strip leading empty lines
} else {
interpolated += string;
}
let variable = variables[index];
if(typeof variable === 'string') {
const leadingCharacters = string.match(/(^|\n)([^\n]*)$/)[2];
const leadingSpace = ' '.repeat(leadingCharacters.length);
variable = variable.split('\n').map((line, index) => {
return index === 0 ? line : leadingSpace + line;
}).join('\n');
}
interpolated += variable;
}
}
return exports.crop(interpolated); // TODO: Integrate hard-coded
};
exports.titleCase = string => string.replace(/^./, initial => initial.toUpperCase());