Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
```
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
19 changes: 8 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,17 @@ 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', () => {
const template = parse({ title: '{{foo-}}' });
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', () => {
Expand Down Expand Up @@ -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',
});
});
});
});