Skip to content

Commit 73365a1

Browse files
committedJan 16, 2019
✨ paraphrase - clean option: remove unmatched templates
1 parent 9962ba1 commit 73365a1

File tree

4 files changed

+88
-47
lines changed

4 files changed

+88
-47
lines changed
 

‎packages/paraphrase/README.md

+27-8
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,37 @@ phrase('Hello, ${name}', {name: 'Martin'}); // Hello, Martin
1717

1818
Acceptable replacements (values) are strings and numbers
1919

20-
### Arguments
21-
One or more replacers, an optional options object at the end
20+
### Arguments and Options
21+
One or more RegExp replacers, an optional options object at the end
22+
23+
| option | meaning | type | default
24+
| - | - | - | -
25+
| resolve | Should resolve dot notations within the template | `Boolean` | `true`
26+
| clean | Should remove unmatched template instances | `Boolean` | `false`
27+
28+
29+
##### Multiple replacers
2230
```js
23-
const phrase = paraphrase(/\${([^{}]*)}/gm, /\{{([^{}]*)}}/gm, {resolve: false});
31+
const phrase = paraphrase(/\${([^{}]*)}/gm, /\{{([^{}]*)}}/gm);
2432

25-
phrase('Hello, ${name} {{last.name}}', {name: 'Martin', 'last.name': 'Prince'}); // Hello, Martin Prince
33+
phrase('Hello, ${firstname} {{lastname}}', {firstname: 'Martin', 'lastname': 'Prince'}); // Hello, Martin Prince
2634
```
2735

28-
### Options
29-
| option | meaning | type | default
30-
| - | - | - | -
31-
| resolve | Resolve dot notations | `Boolean` | `true`
36+
##### Dot notation resolve
37+
Treat dots as part of the key instead of notation marks
38+
```js
39+
const phrase = paraphrase(/\${([^{}]*)}/gm, {resolve: false});
40+
41+
phrase('Hello, ${name} ${last.name}', {name: 'Martin', 'last.name': 'Prince'}); // Hello, Martin Prince
42+
```
43+
44+
##### Unmatched cleanup
45+
Remove unmatched template instances from the result string
46+
```js
47+
const phrase = paraphrase(/\${([^{}]*)}/gm, {clean: true});
48+
49+
phrase('Hello, ${firstname} ${lastname}', {firstname: 'Martin'}); // Hello, Martin
50+
```
3251

3352
## Examples
3453
### Objects

‎packages/paraphrase/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const VALID_RESULT_TYPES = Object.seal(['string', 'number']);
1616
/**
1717
* Create new paraphrase method instance
1818
* @param {...RegExp[]} replacers
19+
* @param {Boolean} [options.resolve=true] Should resolve dot notation within template
20+
* @param {Boolean} [options.clean=false] Should remove unmatched template instances
1921
* @returns {Function} phraser function instance
2022
*
2123
* @example const phraser = paraphrase(/\${([^{}]*)}/gm);
@@ -26,6 +28,7 @@ const VALID_RESULT_TYPES = Object.seal(['string', 'number']);
2628
module.exports = function paraphrase(...replacers) {
2729
const options = {
2830
resolve: true,
31+
clean: false,
2932
};
3033
if (replacers.length && isObject(replacers[replacers.length - 1])) {
3134
Object.assign(options, replacers.pop());
@@ -60,7 +63,7 @@ module.exports = function paraphrase(...replacers) {
6063
function replace(haystack, needle) {
6164
const replacement = options.resolve ? notate(data, needle.trim()) : data[needle.trim()];
6265

63-
return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : haystack;
66+
return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : options.clean ? '' : haystack;
6467
}
6568

6669
return replacers.reduce((string, replacer) => string.replace(replacer, replace), string);

‎packages/paraphrase/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "paraphrase",
3-
"version": "1.6.0",
3+
"version": "1.7.0",
44
"description": "Create flavoured string phraser",
55
"keywords": [
66
"string",

‎packages/paraphrase/spec.js

+56-37
Original file line numberDiff line numberDiff line change
@@ -67,50 +67,69 @@ describe('paraphrase', () => {
6767
});
6868
});
6969

70-
describe('Resolve nested data', () => {
71-
const phrase = paraphrase(/\${([^{}]*)}/g);
72-
const phraseNoResolve = paraphrase(/\${([^{}]*)}/g, {resolve: false});
73-
74-
it('resolves dot notation', () => {
75-
const string = 'Hello, ${name.first} ${name.last}';
76-
const data = {
77-
name: {
78-
first: 'Martin',
79-
last: 'Prince',
80-
},
81-
};
82-
83-
expect(phrase(string, data)).to.equal('Hello, Martin Prince');
84-
});
70+
describe('options', () => {
71+
describe('resolve nested data', () => {
72+
const phrase = paraphrase(/\${([^{}]*)}/g);
73+
const phraseNoResolve = paraphrase(/\${([^{}]*)}/g, {resolve: false});
74+
75+
it('resolves dot notation', () => {
76+
const string = 'Hello, ${name.first} ${name.last}';
77+
const data = {
78+
name: {
79+
first: 'Martin',
80+
last: 'Prince',
81+
},
82+
};
83+
84+
expect(phrase(string, data)).to.equal('Hello, Martin Prince');
85+
});
8586

86-
it('resolves arrays', () => {
87-
const string = 'Hello, ${0} ${1}';
88-
const name = [
89-
'Martin',
90-
'Prince',
91-
];
87+
it('resolves arrays', () => {
88+
const string = 'Hello, ${0} ${1}';
89+
const name = [
90+
'Martin',
91+
'Prince',
92+
];
9293

93-
expect(phrase(string, name)).to.equal('Hello, Martin Prince');
94-
});
94+
expect(phrase(string, name)).to.equal('Hello, Martin Prince');
95+
});
9596

96-
it('misses keys with dots', () => {
97-
const string = 'Hello, ${name.first} ${name.last}';
98-
const data = {
99-
'name.first': 'Martin',
100-
'name.last': 'Prince',
101-
};
97+
it('misses keys with dots', () => {
98+
const string = 'Hello, ${name.first} ${name.last}';
99+
const data = {
100+
'name.first': 'Martin',
101+
'name.last': 'Prince',
102+
};
102103

103-
expect(phrase(string, data)).to.equal('Hello, ${name.first} ${name.last}');
104+
expect(phrase(string, data)).to.equal('Hello, ${name.first} ${name.last}');
105+
});
106+
107+
it('does not resolve dot notation (explicit)', () => {
108+
const string = 'Hello, ${name.first} ${name.last}';
109+
const data = {
110+
'name.first': 'Martin',
111+
'name.last': 'Prince',
112+
};
113+
114+
expect(phraseNoResolve(string, data)).to.equal('Hello, Martin Prince');
115+
});
104116
});
105117

106-
it('does not resolve dot notation (explicit)', () => {
107-
const string = 'Hello, ${name.first} ${name.last}';
108-
const data = {
109-
'name.first': 'Martin',
110-
'name.last': 'Prince',
111-
};
118+
describe('clean parsing', () => {
119+
it('Should leave unmatched template combinations', () => {
120+
const parser = paraphrase(/\${([^{}]*)}/g, {clean: false});
121+
const string = 'Hello, ${name.first} ${name.last}';
122+
const data = {};
112123

113-
expect(phraseNoResolve(string, data)).to.equal('Hello, Martin Prince');
124+
expect(parser(string, data)).to.equal('Hello, ${name.first} ${name.last}');
125+
});
126+
it('Should remove unmatched template combinations', () => {
127+
const parser = paraphrase(/\${([^{}]*)}/g, {clean: true});
128+
const string = 'Hello, ${name.first} ${name.last}';
129+
const data = {};
130+
131+
expect(parser(string, data)).to.equal('Hello, ');
132+
});
114133
});
115134
});
116135

0 commit comments

Comments
 (0)
Please sign in to comment.