-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtickplate.js
45 lines (38 loc) · 1.24 KB
/
tickplate.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
'use strict';
const metautil = require('metautil');
const SEPARATOR = '=';
const parseKeyValuePair = (value, sep = SEPARATOR) => {
const [lhs, ...rhs] = value.split(sep);
const key = lhs.trim();
if (rhs.length === 0) return { key };
const rhsRestored = rhs.join(sep).trim();
const parsed = metautil.jsonParse(rhsRestored);
return { key, value: parsed };
};
const parseKeys = (strKeyValuePairs, sep = SEPARATOR) => {
const defaults = {};
for (const pair of strKeyValuePairs) {
const { key, value } = parseKeyValuePair(pair, sep);
defaults[key] = value;
}
return { keys: Object.keys(defaults), defaults };
};
const serialize = (value, opts) => {
if (!Array.isArray(value)) return value;
const { delimiter } = opts;
return value.join(delimiter);
};
const tickplate = (strings, ...keys) => {
const { keys: tickplateKeys, defaults } = parseKeys(keys);
return (values, opts = {}) => {
const tickplateValues = { ...defaults, ...values };
const result = [strings[0]];
for (let i = 0; i < tickplateKeys.length; i++) {
const key = tickplateKeys[i];
const value = serialize(tickplateValues[key], opts);
result.push(value, strings[i + 1]);
}
return result.join('');
};
};
module.exports = tickplate;