tickplate 1.0.9 → 1.1.0
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/LICENSE +1 -1
- package/README.md +57 -12
- package/package.json +10 -5
- package/tickplate.d.ts +9 -4
- package/tickplate.js +12 -4
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
3
|
+
Copyright (c) 2017-2026 Metarhia contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -6,13 +6,19 @@
|
|
|
6
6
|
[](https://www.npmjs.com/package/tickplate)
|
|
7
7
|
[](https://www.npmjs.com/package/tickplate)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Zero-dependency back-tick templates using property names instead of expressions.
|
|
10
|
+
Part of the [Metarhia](https://github.com/metarhia) stack.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
```bash
|
|
15
|
+
npm install tickplate
|
|
16
|
+
```
|
|
14
17
|
|
|
15
|
-
##
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Place tag `t` before a template literal. Placeholders use property names (strings)
|
|
21
|
+
as keys; pass a data object to the returned function to render.
|
|
16
22
|
|
|
17
23
|
```js
|
|
18
24
|
const t = require('tickplate');
|
|
@@ -31,18 +37,58 @@ const data = {
|
|
|
31
37
|
const templ = t`${'hello'} ${'myFriend'}, great ${'positions'} of Rome`;
|
|
32
38
|
|
|
33
39
|
console.log(templ(data));
|
|
40
|
+
// Ave! Marcus Aurelius, great emperor, philosopher, writer of Rome
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### ESM
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
import t from 'tickplate';
|
|
47
|
+
|
|
48
|
+
const templ = t`Hello ${'name'}!`;
|
|
49
|
+
console.log(templ({ name: 'World' }));
|
|
50
|
+
// Hello World!
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## API
|
|
54
|
+
|
|
55
|
+
### `t(strings, ...keys)`
|
|
56
|
+
|
|
57
|
+
Tagged template literal. Returns a function `(values, opts?) => string`.
|
|
58
|
+
|
|
59
|
+
- **Placeholders**: `${'key'}` — property name from the data object.
|
|
60
|
+
- **Default values**: `${'key=value'}` — JSON-parsable default when the key is
|
|
61
|
+
missing. Example: `${'greeting="Hello"'}` or `${'count=0'}`.
|
|
62
|
+
- **Arrays**: Values are joined with `,` by default. Use `opts.delimiter` to
|
|
63
|
+
customize (e.g. `{ delimiter: ', ' }`).
|
|
64
|
+
|
|
65
|
+
### `templ(values, opts?)`
|
|
66
|
+
|
|
67
|
+
Renders the template.
|
|
68
|
+
|
|
69
|
+
- **values**: Data object. Keys not present render as empty string. `null` and
|
|
70
|
+
`undefined` are treated as `{}`.
|
|
71
|
+
- **opts.delimiter**: String (or coercible value) used to join array elements.
|
|
72
|
+
Default: `','`.
|
|
73
|
+
|
|
74
|
+
## Examples
|
|
75
|
+
|
|
76
|
+
### With delimiter
|
|
77
|
+
|
|
78
|
+
```js
|
|
34
79
|
console.log(templ(data, { delimiter: ', ' }));
|
|
80
|
+
// Ave! Marcus Aurelius, great emperor, philosopher, writer of Rome
|
|
35
81
|
```
|
|
36
82
|
|
|
37
|
-
With default values
|
|
83
|
+
### With default values
|
|
38
84
|
|
|
39
85
|
```js
|
|
40
|
-
const t =
|
|
86
|
+
const templ = t`${'greeting='} ${'person="Marcus Aurelius"'}, great ${'positions=["emperor", "philosopher"]'} of Rome from ${'ruleFrom=161'} to ${'ruleTo=180'} AD`;
|
|
41
87
|
|
|
42
88
|
const data = {
|
|
43
89
|
greeting: 'Valē!',
|
|
44
90
|
person: {
|
|
45
|
-
name: 'Lucius
|
|
91
|
+
name: 'Lucius Verus',
|
|
46
92
|
toString() {
|
|
47
93
|
return this.name;
|
|
48
94
|
},
|
|
@@ -52,13 +98,12 @@ const data = {
|
|
|
52
98
|
ruleTo: 169,
|
|
53
99
|
};
|
|
54
100
|
|
|
55
|
-
const templ = t`${'greeting='} ${'person="Marcus Aurelius"'}, great ${'positions=["emperor", "philosopher"]'} of Rome from ${'ruleFrom=161'} to ${'ruleTo=180'} AD`;
|
|
56
|
-
|
|
57
101
|
console.log(templ(data));
|
|
102
|
+
// Valē! Lucius Verus, great brother,emperor,co-emperor of Rome from 161 to 180 AD
|
|
58
103
|
```
|
|
59
104
|
|
|
60
105
|
## License & Contributors
|
|
61
106
|
|
|
62
|
-
Copyright (c) 2017-
|
|
63
|
-
Tickplate is [MIT licensed](./LICENSE)
|
|
107
|
+
Copyright (c) 2017-2026 [Metarhia contributors](https://github.com/metarhia/tickplate/graphs/contributors).
|
|
108
|
+
Tickplate is [MIT licensed](./LICENSE).
|
|
64
109
|
Tickplate is a part of [Metarhia](https://github.com/metarhia) technology stack.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tickplate",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>",
|
|
5
5
|
"description": "Back-tick templates for JavaScript",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,7 +27,15 @@
|
|
|
27
27
|
},
|
|
28
28
|
"main": "tickplate.js",
|
|
29
29
|
"types": "tickplate.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"require": "./tickplate.js",
|
|
33
|
+
"import": "./tickplate.js",
|
|
34
|
+
"types": "./tickplate.d.ts"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
30
37
|
"files": [
|
|
38
|
+
"tickplate.js",
|
|
31
39
|
"tickplate.d.ts"
|
|
32
40
|
],
|
|
33
41
|
"engines": {
|
|
@@ -42,11 +50,8 @@
|
|
|
42
50
|
},
|
|
43
51
|
"devDependencies": {
|
|
44
52
|
"eslint": "^9.39.2",
|
|
45
|
-
"eslint-config-metarhia": "^9.1.
|
|
53
|
+
"eslint-config-metarhia": "^9.1.8",
|
|
46
54
|
"prettier": "^3.7.4",
|
|
47
55
|
"typescript": "^5.9.3"
|
|
48
|
-
},
|
|
49
|
-
"dependencies": {
|
|
50
|
-
"metautil": "^5.4.0"
|
|
51
56
|
}
|
|
52
57
|
}
|
package/tickplate.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
interface TickplateOptions {
|
|
2
|
-
delimiter?:
|
|
2
|
+
delimiter?: string;
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
+
type TickplateTemplate = (
|
|
6
|
+
values?: Record<string, unknown> | null,
|
|
7
|
+
opts?: TickplateOptions,
|
|
8
|
+
) => string;
|
|
9
|
+
|
|
5
10
|
declare function tickplate(
|
|
6
|
-
strings:
|
|
7
|
-
...keys:
|
|
8
|
-
):
|
|
11
|
+
strings: TemplateStringsArray,
|
|
12
|
+
...keys: string[]
|
|
13
|
+
): TickplateTemplate;
|
|
9
14
|
|
|
10
15
|
export = tickplate;
|
package/tickplate.js
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const metautil = require('metautil');
|
|
4
|
-
|
|
5
3
|
const SEPARATOR = '=';
|
|
6
4
|
|
|
5
|
+
const jsonParse = (value) => {
|
|
6
|
+
let result;
|
|
7
|
+
try {
|
|
8
|
+
result = JSON.parse(value);
|
|
9
|
+
} catch {
|
|
10
|
+
result = undefined;
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
};
|
|
14
|
+
|
|
7
15
|
const parseKeyValuePair = (value, sep = SEPARATOR) => {
|
|
8
16
|
const [lhs, ...rhs] = value.split(sep);
|
|
9
17
|
const key = lhs.trim();
|
|
10
18
|
if (rhs.length === 0) return { key };
|
|
11
19
|
const rhsRestored = rhs.join(sep).trim();
|
|
12
|
-
const parsed =
|
|
20
|
+
const parsed = jsonParse(rhsRestored);
|
|
13
21
|
return { key, value: parsed };
|
|
14
22
|
};
|
|
15
23
|
|
|
@@ -30,7 +38,7 @@ const serialize = (value, opts) => {
|
|
|
30
38
|
|
|
31
39
|
const tickplate = (strings, ...keys) => {
|
|
32
40
|
const { keys: tickplateKeys, defaults } = parseKeys(keys);
|
|
33
|
-
return (values, opts = {}) => {
|
|
41
|
+
return (values = {}, opts = {}) => {
|
|
34
42
|
const tickplateValues = { ...defaults, ...values };
|
|
35
43
|
const result = [strings[0]];
|
|
36
44
|
for (let i = 0; i < tickplateKeys.length; i++) {
|