tickplate 1.0.6 → 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 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.6",
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": ["tickplate.d.ts"],
30
+ "files": [
31
+ "tickplate.d.ts"
32
+ ],
31
33
  "engines": {
32
- "node": "^14.18 || 16 || 18 || 19"
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.34.0",
44
+ "eslint": "^8.54.0",
43
45
  "eslint-config-metarhia": "^8.1.0",
44
- "eslint-config-prettier": "^8.6.0",
45
- "eslint-plugin-import": "^2.27.5",
46
- "eslint-plugin-prettier": "^4.2.1",
47
- "prettier": "^2.8.4",
48
- "typescript": "^4.9.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
- declare function tickplate(strings: Array<string>, ...keys: Array<string>): (values: object) => string;
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 tickplate =
4
- (strings, ...keys) =>
5
- (values) => {
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 < keys.length; i++) {
8
- const key = keys[i];
9
- result.push(values[key], strings[i + 1]);
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;
package/CHANGELOG.md DELETED
@@ -1,45 +0,0 @@
1
- # Changelog
2
-
3
- ## [Unreleased][unreleased]
4
-
5
- ## [1.0.6][] - 2023-02-20
6
-
7
- - Package maintenance
8
-
9
- ## [1.0.5][] - 2022-07-07
10
-
11
- - Package maintenance
12
-
13
- ## [1.0.4][] - 2022-05-12
14
-
15
- - Package maintenance
16
-
17
- ## [1.0.3][] - 2021-07-17
18
-
19
- - Fix d.ts typings and include typings to package publishing files
20
- - Chore: update package files, copyrights, links
21
-
22
- ## [1.0.2][] - 2021-07-15
23
-
24
- - Add d.ts typings
25
- - Package maintenance
26
-
27
- ## [1.0.1][] - 2021-06-14
28
-
29
- - Refresh package (dependencies, ci, badges, code style)
30
-
31
- ## [1.0.0][] - 2020-12-16
32
-
33
- - Reflect data structures to template with tick syntax
34
-
35
- ## [0.0.x][] Pre-release versions
36
-
37
- [unreleased]: https://github.com/metarhia/tickplate/compare/v1.0.6...HEAD
38
- [1.0.6]: https://github.com/metarhia/tickplate/compare/v1.0.5...v1.0.6
39
- [1.0.5]: https://github.com/metarhia/tickplate/compare/v1.0.4...v1.0.5
40
- [1.0.4]: https://github.com/metarhia/tickplate/compare/v1.0.3...v1.0.4
41
- [1.0.3]: https://github.com/metarhia/tickplate/compare/v1.0.2...v1.0.3
42
- [1.0.2]: https://github.com/metarhia/tickplate/compare/v1.0.1...v1.0.2
43
- [1.0.1]: https://github.com/metarhia/tickplate/compare/v1.0.0...v1.0.1
44
- [1.0.0]: https://github.com/metarhia/tickplate/compare/v0.0.x...v1.0.0
45
- [0.0.x]: https://github.com/metarhia/tickplate/releases/tag/v0.0.x