tickplate 1.0.7 → 1.0.9

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2023 Metarhia contributors
3
+ Copyright (c) 2017-2025 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
@@ -1,7 +1,6 @@
1
1
  ## Tickplate - Back-tick templates for JavaScript
2
2
 
3
3
  [![ci status](https://github.com/metarhia/tickplate/workflows/Testing%20CI/badge.svg)](https://github.com/metarhia/tickplate/actions?query=workflow%3A%22Testing+CI%22+branch%3Amaster)
4
- [![codacy](https://api.codacy.com/project/badge/Grade/69719502402b43598ffac0fd35f2192c)](https://www.codacy.com/app/metarhia/tickplate)
5
4
  [![snyk](https://snyk.io/test/github/metarhia/tickplate/badge.svg)](https://snyk.io/test/github/metarhia/tickplate)
6
5
  [![npm version](https://badge.fury.io/js/tickplate.svg)](https://badge.fury.io/js/tickplate)
7
6
  [![npm downloads/month](https://img.shields.io/npm/dm/tickplate.svg)](https://www.npmjs.com/package/tickplate)
@@ -31,11 +30,35 @@ const data = {
31
30
 
32
31
  const templ = t`${'hello'} ${'myFriend'}, great ${'positions'} of Rome`;
33
32
 
33
+ console.log(templ(data));
34
+ console.log(templ(data, { delimiter: ', ' }));
35
+ ```
36
+
37
+ With default values provided (optionally):
38
+
39
+ ```js
40
+ const t = require('tickplate');
41
+
42
+ const data = {
43
+ greeting: 'Valē!',
44
+ person: {
45
+ name: 'Lucius Aurelius Verus',
46
+ toString() {
47
+ return this.name;
48
+ },
49
+ },
50
+ positions: ['brother', 'emperor', 'co-emperor'],
51
+ ruleFrom: 161,
52
+ ruleTo: 169,
53
+ };
54
+
55
+ const templ = t`${'greeting='} ${'person="Marcus Aurelius"'}, great ${'positions=["emperor", "philosopher"]'} of Rome from ${'ruleFrom=161'} to ${'ruleTo=180'} AD`;
56
+
34
57
  console.log(templ(data));
35
58
  ```
36
59
 
37
60
  ## License & Contributors
38
61
 
39
- Copyright (c) 2017-2023 [Metarhia contributors](https://github.com/metarhia/tickplate/graphs/contributors).
62
+ Copyright (c) 2017-2025 [Metarhia contributors](https://github.com/metarhia/tickplate/graphs/contributors).
40
63
  Tickplate is [MIT licensed](./LICENSE).\
41
64
  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.7",
3
+ "version": "1.0.9",
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,24 +27,26 @@
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": "16 || 18 || 19 || 20"
34
+ "node": ">=18"
33
35
  },
34
36
  "readmeFilename": "README.md",
35
37
  "scripts": {
36
38
  "test": "npm run lint && npm run types && node ./test.js",
37
39
  "types": "tsc -p tsconfig.json",
38
- "lint": "eslint . && prettier -c \"**/*.js\" \"**/*.json\" \"**/*.md\" \".*rc\" \"**/*.yml\"",
39
- "fmt": "prettier --write \"**/*.js\" \"**/*.json\" \"**/*.md\" \".*rc\" \"**/*.yml\""
40
+ "lint": "eslint . && prettier -c \"**/*.js\" \"**/*.json\" \"**/*.md\"",
41
+ "fix": "prettier --write \"**/*.js\" \"**/*.json\" \"**/*.md\""
40
42
  },
41
43
  "devDependencies": {
42
- "eslint": "^8.34.0",
43
- "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": "^5.0.4"
44
+ "eslint": "^9.39.2",
45
+ "eslint-config-metarhia": "^9.1.5",
46
+ "prettier": "^3.7.4",
47
+ "typescript": "^5.9.3"
48
+ },
49
+ "dependencies": {
50
+ "metautil": "^5.4.0"
49
51
  }
50
52
  }
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;