diff --git a/README.md b/README.md index c7b80c8..f4ccd41 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ You can use templates in object keys, not just values: ```js const template = parse({ - 'A simple {{message}} to': 'value' + 'A simple {{message}} to': 'value', }); console.log(template({ message: 'hello' })); // Prints { "A simple hello to": "value" } ``` @@ -91,7 +91,7 @@ const template2 = parse('{{foo$}}'); // - symbol can be used anywhere except as first character const template3 = parse('{{foo-bar}}'); // Works -const template4 = parse('{{-foo}}'); // Won't work +const template4 = parse('{{-foo}}'); // Won't work ``` ### Unicode Support @@ -100,7 +100,7 @@ Parameter names can include Unicode characters: ```js const template = parse('{{中文}}'); -console.log(template({ '中文': 'value' })); // Prints "value" +console.log(template({ 中文: 'value' })); // Prints "value" ``` ### Function Values @@ -109,9 +109,11 @@ Templates can handle functions as values: ```js const template = parse('{{userCard}}'); -console.log(template({ - userCard: () => ({ id: 1, user: 'John' }) -})); // Prints { id: 1, user: 'John' } +console.log( + template({ + userCard: () => ({ id: 1, user: 'John' }), + }), +); // Prints { id: 1, user: 'John' } ``` ### Date Objects diff --git a/index.js b/index.js index f4da357..40c3ab3 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,7 @@ function parseString(str, options = {}) { let templateFn = () => str; - const matches = Array.from(str.matchAll(regex)) + const matches = Array.from(str.matchAll(regex)); const parameters = matches.map((match) => { const r = { key: match[1], @@ -72,7 +72,10 @@ function parseString(str, options = {}) { templateFn = (context = {}) => { return matches.reduce((result, match, i) => { const parameter = parameters[i]; - let value = objectPath.get(context, options.rawKey ? [parameter.key] : parameter.key); + let value = objectPath.get( + context, + options.rawKey ? [parameter.key] : parameter.key, + ); if (typeof value === 'undefined') { value = parameter.defaultValue; diff --git a/package.json b/package.json index 05fea13..31ce9f8 100644 --- a/package.json +++ b/package.json @@ -26,9 +26,9 @@ }, "homepage": "https://github.com/datavis-tech/json-templates#readme", "devDependencies": { - "mocha": "^10.4.0", - "prettier": "^3.2.5", - "rollup": "^4.16.4", + "mocha": "^11.1.0", + "prettier": "^3.5.3", + "rollup": "^4.40.0", "rollup-plugin-buble": "^0.19.8" }, "dependencies": { diff --git a/test.js b/test.js index 98311e8..2d05632 100644 --- a/test.js +++ b/test.js @@ -202,7 +202,7 @@ describe('json-template', () => { it('should use a $ symbol in a name, any place', () => { const template = parse({ title: '{{$foo$}}' }); assert.deepEqual(template.parameters, [{ key: '$foo$' }]); - assert.deepEqual(template({ '$foo$': 'bar' }), { title: 'bar' }); + assert.deepEqual(template({ $foo$: 'bar' }), { title: 'bar' }); }); it('should use a - symbol in a name, any place except first letter', () => { @@ -210,7 +210,9 @@ describe('json-template', () => { assert.deepEqual(template.parameters, [{ key: 'foo-' }]); assert.deepEqual(template({ 'foo-': 'bar' }), { title: 'bar' }); - assert.deepEqual(parse({ title: '{{-a}}' })({ '-a': 'bar' }), { title: '{{-a}}' }); + assert.deepEqual(parse({ title: '{{-a}}' })({ '-a': 'bar' }), { + title: '{{-a}}', + }); }); it('should compute template with an object with multiple parameters', () => { @@ -732,16 +734,11 @@ describe('json-template', () => { title: '{{中文}}', }); - assert.deepEqual(template.parameters, [ - { key: '中文' }, - ]); + assert.deepEqual(template.parameters, [{ key: '中文' }]); - assert.deepEqual( - template({ 中文: 'foo' }), - { - title: 'foo', - }, - ); + assert.deepEqual(template({ 中文: 'foo' }), { + title: 'foo', + }); }); }); });