tickplate 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -0
- package/package.json +15 -10
- package/tickplate.d.ts +8 -1
- package/tickplate.js +37 -6
package/README.md
CHANGED
|
@@ -31,6 +31,30 @@ const data = {
|
|
|
31
31
|
|
|
32
32
|
const templ = t`${'hello'} ${'myFriend'}, great ${'positions'} of Rome`;
|
|
33
33
|
|
|
34
|
+
console.log(templ(data));
|
|
35
|
+
console.log(templ(data, { delimiter: ', ' }));
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
With default values provided (optionally):
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
const t = require('tickplate');
|
|
42
|
+
|
|
43
|
+
const data = {
|
|
44
|
+
greeting: 'Valē!',
|
|
45
|
+
person: {
|
|
46
|
+
name: 'Lucius Aurelius Verus',
|
|
47
|
+
toString() {
|
|
48
|
+
return this.name;
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
positions: ['brother', 'emperor', 'co-emperor'],
|
|
52
|
+
ruleFrom: 161,
|
|
53
|
+
ruleTo: 169,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const templ = t`${'greeting='} ${'person="Marcus Aurelius"'}, great ${'positions=["emperor", "philosopher"]'} of Rome from ${'ruleFrom=161'} to ${'ruleTo=180'} AD`;
|
|
57
|
+
|
|
34
58
|
console.log(templ(data));
|
|
35
59
|
```
|
|
36
60
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tickplate",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>",
|
|
5
5
|
"description": "Back-tick templates for JavaScript",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
],
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
|
-
"url": "https://github.com/metarhia/tickplate"
|
|
17
|
+
"url": "git+https://github.com/metarhia/tickplate.git"
|
|
18
18
|
},
|
|
19
19
|
"bugs": {
|
|
20
20
|
"url": "https://github.com/metarhia/tickplate/issues",
|
|
@@ -27,9 +27,11 @@
|
|
|
27
27
|
},
|
|
28
28
|
"main": "tickplate.js",
|
|
29
29
|
"types": "tickplate.d.ts",
|
|
30
|
-
"files": [
|
|
30
|
+
"files": [
|
|
31
|
+
"tickplate.d.ts"
|
|
32
|
+
],
|
|
31
33
|
"engines": {
|
|
32
|
-
"node": "
|
|
34
|
+
"node": "18 || 20 || 21"
|
|
33
35
|
},
|
|
34
36
|
"readmeFilename": "README.md",
|
|
35
37
|
"scripts": {
|
|
@@ -39,12 +41,15 @@
|
|
|
39
41
|
"fmt": "prettier --write \"**/*.js\" \"**/*.json\" \"**/*.md\" \".*rc\" \"**/*.yml\""
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
|
-
"eslint": "^8.
|
|
44
|
+
"eslint": "^8.54.0",
|
|
43
45
|
"eslint-config-metarhia": "^8.1.0",
|
|
44
|
-
"eslint-config-prettier": "^
|
|
45
|
-
"eslint-plugin-import": "^2.
|
|
46
|
-
"eslint-plugin-prettier": "^
|
|
47
|
-
"prettier": "^
|
|
48
|
-
"typescript": "^5.
|
|
46
|
+
"eslint-config-prettier": "^9.0.0",
|
|
47
|
+
"eslint-plugin-import": "^2.29.0",
|
|
48
|
+
"eslint-plugin-prettier": "^5.0.1",
|
|
49
|
+
"prettier": "^3.1.0",
|
|
50
|
+
"typescript": "^5.3.2"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"metautil": "^4.0.1"
|
|
49
54
|
}
|
|
50
55
|
}
|
package/tickplate.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
interface TickplateOptions {
|
|
2
|
+
delimiter?: any;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
declare function tickplate(
|
|
6
|
+
strings: Array<string>,
|
|
7
|
+
...keys: Array<string>
|
|
8
|
+
): (values: object, opts?: TickplateOptions) => string;
|
|
2
9
|
|
|
3
10
|
export = tickplate;
|
package/tickplate.js
CHANGED
|
@@ -1,14 +1,45 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const metautil = require('metautil');
|
|
4
|
+
|
|
5
|
+
const SEPARATOR = '=';
|
|
6
|
+
|
|
7
|
+
const parseKeyValuePair = (value, sep = SEPARATOR) => {
|
|
8
|
+
const [lhs, ...rhs] = value.split(sep);
|
|
9
|
+
const key = lhs.trim();
|
|
10
|
+
if (rhs.length === 0) return { key };
|
|
11
|
+
const rhsRestored = rhs.join(sep).trim();
|
|
12
|
+
const parsed = metautil.jsonParse(rhsRestored);
|
|
13
|
+
return { key, value: parsed };
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const parseKeys = (strKeyValuePairs, sep = SEPARATOR) => {
|
|
17
|
+
const defaults = {};
|
|
18
|
+
for (const pair of strKeyValuePairs) {
|
|
19
|
+
const { key, value } = parseKeyValuePair(pair, sep);
|
|
20
|
+
defaults[key] = value;
|
|
21
|
+
}
|
|
22
|
+
return { keys: Object.keys(defaults), defaults };
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const serialize = (value, opts) => {
|
|
26
|
+
if (!Array.isArray(value)) return value;
|
|
27
|
+
const { delimiter } = opts;
|
|
28
|
+
return value.join(delimiter);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const tickplate = (strings, ...keys) => {
|
|
32
|
+
const { keys: tickplateKeys, defaults } = parseKeys(keys);
|
|
33
|
+
return (values, opts = {}) => {
|
|
34
|
+
const tickplateValues = { ...defaults, ...values };
|
|
6
35
|
const result = [strings[0]];
|
|
7
|
-
for (let i = 0; i <
|
|
8
|
-
const key =
|
|
9
|
-
|
|
36
|
+
for (let i = 0; i < tickplateKeys.length; i++) {
|
|
37
|
+
const key = tickplateKeys[i];
|
|
38
|
+
const value = serialize(tickplateValues[key], opts);
|
|
39
|
+
result.push(value, strings[i + 1]);
|
|
10
40
|
}
|
|
11
41
|
return result.join('');
|
|
12
42
|
};
|
|
43
|
+
};
|
|
13
44
|
|
|
14
45
|
module.exports = tickplate;
|