paraphrase 3.0.0 → 3.1.0-rc-dcd4648
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/CHANGELOG.md +6 -2
- package/README.md +50 -30
- package/index.js +86 -146
- package/index.mjs +55 -139
- package/package.json +23 -15
- package/types/flavours/index.d.ts +1 -0
- package/types/index.d.ts +36 -0
- package/types/isObject/index.d.ts +6 -0
- package/types/notate/index.d.ts +23 -0
- package/index.js.map +0 -1
- package/index.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,14 +3,17 @@
|
|
|
3
3
|
## Breaking change
|
|
4
4
|
|
|
5
5
|
### All exports are named
|
|
6
|
+
|
|
6
7
|
```js
|
|
7
|
-
import { paraphrase } from
|
|
8
|
+
import { paraphrase } from "paraphrase";
|
|
8
9
|
```
|
|
9
10
|
|
|
10
11
|
### Flavoured imports included
|
|
12
|
+
|
|
11
13
|
Flavoured pre-made versions should be imported from main entry and not from directory.
|
|
14
|
+
|
|
12
15
|
```js
|
|
13
|
-
import { dollar } from
|
|
16
|
+
import { dollar } from "paraphrase";
|
|
14
17
|
```
|
|
15
18
|
|
|
16
19
|
# 2.0.0 2021-05-11
|
|
@@ -18,4 +21,5 @@ import { dollar } from 'paraphrase'
|
|
|
18
21
|
## Breaking change
|
|
19
22
|
|
|
20
23
|
### Remove compiled version.
|
|
24
|
+
|
|
21
25
|
If you are using a bundler with babel, make sure you transpile this package to your liking
|
package/README.md
CHANGED
|
@@ -11,113 +11,131 @@ npm i paraphrase
|
|
|
11
11
|
Creates new paraphrase method instance
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
import { paraphrase } from
|
|
14
|
+
import { paraphrase } from "paraphrase";
|
|
15
15
|
const phrase = paraphrase(/\${([^{}]*)}/gm); // Create a new phrase function using a RegExp match
|
|
16
16
|
|
|
17
|
-
phrase(
|
|
17
|
+
phrase("Hello, ${name}", { name: "Martin" }); // Hello, Martin
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
Acceptable replacements (values) are strings and numbers
|
|
21
21
|
|
|
22
22
|
### Arguments and Options
|
|
23
|
-
One or more RegExp replacers, an optional options object at the end
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
| - | - | - | -
|
|
27
|
-
| recursive | Should continue to resolve result string until replacements have been exhausted | `Boolean` | `true`
|
|
28
|
-
| resolve | Should resolve dot notations within the template | `Boolean` | `true`
|
|
29
|
-
| clean | Should remove unmatched template instances | `Boolean` | `false`
|
|
24
|
+
One or more RegExp replacers, an optional options object at the end
|
|
30
25
|
|
|
26
|
+
| option | meaning | type | default |
|
|
27
|
+
| --------- | ------------------------------------------------------------------------------- | --------- | ------- |
|
|
28
|
+
| recursive | Should continue to resolve result string until replacements have been exhausted | `Boolean` | `true` |
|
|
29
|
+
| resolve | Should resolve dot notations within the template | `Boolean` | `true` |
|
|
30
|
+
| clean | Should remove unmatched template instances | `Boolean` | `false` |
|
|
31
31
|
|
|
32
32
|
##### Multiple replacers
|
|
33
|
+
|
|
33
34
|
```js
|
|
34
35
|
const phrase = paraphrase(/\${([^{}]*)}/gm, /\{{([^{}]*)}}/gm);
|
|
35
36
|
|
|
36
|
-
phrase(
|
|
37
|
+
phrase("Hello, ${firstname} {{lastname}}", {
|
|
38
|
+
firstname: "Martin",
|
|
39
|
+
lastname: "Prince",
|
|
40
|
+
}); // Hello, Martin Prince
|
|
37
41
|
```
|
|
38
42
|
|
|
39
43
|
##### Dot notation resolve
|
|
44
|
+
|
|
40
45
|
Treat dots as part of the key instead of notation marks
|
|
46
|
+
|
|
41
47
|
```js
|
|
42
|
-
const phrase = paraphrase(/\${([^{}]*)}/gm, {resolve: false});
|
|
48
|
+
const phrase = paraphrase(/\${([^{}]*)}/gm, { resolve: false });
|
|
43
49
|
|
|
44
|
-
phrase(
|
|
50
|
+
phrase("Hello, ${name} ${last.name}", {
|
|
51
|
+
name: "Martin",
|
|
52
|
+
"last.name": "Prince",
|
|
53
|
+
}); // Hello, Martin Prince
|
|
45
54
|
```
|
|
46
55
|
|
|
47
56
|
##### Unmatched cleanup
|
|
57
|
+
|
|
48
58
|
Remove unmatched template instances from the result string
|
|
59
|
+
|
|
49
60
|
```js
|
|
50
|
-
const phrase = paraphrase(/\${([^{}]*)}/gm, {clean: true});
|
|
61
|
+
const phrase = paraphrase(/\${([^{}]*)}/gm, { clean: true });
|
|
51
62
|
|
|
52
|
-
phrase(
|
|
63
|
+
phrase("Hello, ${firstname} ${lastname}", { firstname: "Martin" }); // Hello, Martin
|
|
53
64
|
```
|
|
54
65
|
|
|
55
66
|
## Examples
|
|
67
|
+
|
|
56
68
|
### Objects
|
|
57
69
|
|
|
58
70
|
```js
|
|
59
|
-
phrase(
|
|
71
|
+
phrase("Hello, ${name}", { name: "Martin" }); // Hello, Martin
|
|
60
72
|
```
|
|
61
73
|
|
|
62
74
|
### Objects with dot notation
|
|
63
75
|
|
|
64
76
|
```js
|
|
65
77
|
const user = {
|
|
66
|
-
|
|
78
|
+
name: { first: "Martin", last: "Prince" },
|
|
67
79
|
};
|
|
68
|
-
phrase(
|
|
80
|
+
phrase("Hello, ${name.first} ${name.last}", user); // Hello, Martin Prince
|
|
69
81
|
```
|
|
70
82
|
|
|
71
83
|
### Arrays
|
|
72
84
|
|
|
73
85
|
```js
|
|
74
|
-
phrase(
|
|
86
|
+
phrase("Hello, ${0} ${1}", ["Martin", "Prince"]); // Hello, Martin Prince
|
|
75
87
|
```
|
|
76
88
|
|
|
77
89
|
### Spread arguments
|
|
78
90
|
|
|
79
91
|
```js
|
|
80
|
-
phrase(
|
|
92
|
+
phrase("Hello, ${0} ${1}", "Martin", "Prince"); // Hello, Martin Prince
|
|
81
93
|
```
|
|
82
94
|
|
|
83
95
|
## Premade
|
|
84
96
|
|
|
85
97
|
### dollar `${...}`
|
|
98
|
+
|
|
86
99
|
```js
|
|
87
|
-
import { dollar as phrase } from
|
|
100
|
+
import { dollar as phrase } from "paraphrase";
|
|
88
101
|
|
|
89
|
-
phrase(
|
|
102
|
+
phrase("Hello, ${name}", { name: "Martin" }); // Hello, Martin
|
|
90
103
|
```
|
|
91
104
|
|
|
92
105
|
### double `{{...}}`
|
|
106
|
+
|
|
93
107
|
```js
|
|
94
|
-
import { double as phrase } from
|
|
108
|
+
import { double as phrase } from "paraphrase";
|
|
95
109
|
|
|
96
|
-
phrase(
|
|
110
|
+
phrase("Hello, {{name}}", { name: "Martin" }); // Hello, Martin
|
|
97
111
|
```
|
|
98
112
|
|
|
99
113
|
### single `{...}`
|
|
114
|
+
|
|
100
115
|
```js
|
|
101
|
-
import { single as phrase } from
|
|
116
|
+
import { single as phrase } from "paraphrase";
|
|
102
117
|
|
|
103
|
-
phrase(
|
|
118
|
+
phrase("Hello, {name}", { name: "Martin" }); // Hello, Martin
|
|
104
119
|
```
|
|
105
120
|
|
|
106
121
|
### percent `%{...}` (i18n style)
|
|
122
|
+
|
|
107
123
|
```js
|
|
108
|
-
|
|
124
|
+
import { percent as phrase } from "paraphrase";
|
|
109
125
|
|
|
110
|
-
phrase(
|
|
126
|
+
phrase("Hello, %{name}", { name: "Martin" }); // Hello, Martin
|
|
111
127
|
```
|
|
112
128
|
|
|
113
129
|
### hash `#{...}` (ruby style)
|
|
130
|
+
|
|
114
131
|
```js
|
|
115
|
-
import { hash as phrase } from
|
|
132
|
+
import { hash as phrase } from "paraphrase";
|
|
116
133
|
|
|
117
|
-
phrase(
|
|
134
|
+
phrase("Hello, #{name}", { name: "Martin" }); // Hello, Martin
|
|
118
135
|
```
|
|
119
136
|
|
|
120
137
|
### loose. Accommodate all of the above
|
|
138
|
+
|
|
121
139
|
```js
|
|
122
140
|
import { loose as phrase } from 'paraphrase';
|
|
123
141
|
|
|
@@ -125,9 +143,11 @@ phrase('Hello, #{name.first} {name.last}', {name: { first: 'Martin', last: 'Prin
|
|
|
125
143
|
```
|
|
126
144
|
|
|
127
145
|
## patterns
|
|
146
|
+
|
|
128
147
|
A paraphrase instance exposes view to its patterns array (immutable)
|
|
148
|
+
|
|
129
149
|
```js
|
|
130
|
-
import { hash as phrase } from
|
|
150
|
+
import { hash as phrase } from "paraphrase";
|
|
131
151
|
|
|
132
|
-
phrase.patterns // [ /#{([^{}]*)}/gm ]
|
|
152
|
+
phrase.patterns; // [ /#{([^{}]*)}/gm ]
|
|
133
153
|
```
|
package/index.js
CHANGED
|
@@ -1,71 +1,51 @@
|
|
|
1
|
-
Object.defineProperty
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
* notate(obj, 'top_level.missing.value');
|
|
39
|
-
* // undefined
|
|
40
|
-
*/
|
|
41
|
-
function notate(source) {
|
|
42
|
-
var string = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
43
|
-
|
|
44
|
-
if (typeof string !== 'string') {
|
|
45
|
-
throw new TypeError("Expected notation query to be a string, instead got ".concat(_typeof(string), " (").concat(string, ")"));
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var src_exports = {};
|
|
21
|
+
__export(src_exports, {
|
|
22
|
+
dollar: () => dollar,
|
|
23
|
+
double: () => double,
|
|
24
|
+
hash: () => hash,
|
|
25
|
+
loose: () => loose,
|
|
26
|
+
paraphrase: () => paraphrase,
|
|
27
|
+
percent: () => percent,
|
|
28
|
+
single: () => single
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(src_exports);
|
|
31
|
+
|
|
32
|
+
// src/notate/index.ts
|
|
33
|
+
function notate(source, string = "") {
|
|
34
|
+
if (typeof string !== "string") {
|
|
35
|
+
throw new TypeError(
|
|
36
|
+
`Expected notation query to be a string, instead got ${typeof string} (${string})`
|
|
37
|
+
);
|
|
46
38
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
return string.split(".").reduce(
|
|
40
|
+
(previous, current) => typeof previous === "object" && previous ? previous[current] : previous,
|
|
41
|
+
source
|
|
42
|
+
);
|
|
51
43
|
}
|
|
52
44
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
* @param {any} obj
|
|
56
|
-
* @return {boolean}
|
|
57
|
-
*/
|
|
58
|
-
var isObject = function isObject(obj) {
|
|
59
|
-
return "".concat(obj) === '[object Object]';
|
|
60
|
-
};
|
|
45
|
+
// src/isObject/index.ts
|
|
46
|
+
var isObject = (obj) => `${obj}` === "[object Object]";
|
|
61
47
|
|
|
62
|
-
|
|
63
|
-
* @property {RegExp} dollar 'Hello, ${name}'
|
|
64
|
-
* @property {RegExp} double 'Hello, {{name}}'
|
|
65
|
-
* @property {RegExp} single 'Hello, {name}'
|
|
66
|
-
* @property {RegExp} hash 'Hello, #{name}'
|
|
67
|
-
* @property {RegExp} percent 'Hello, %{name}'
|
|
68
|
-
*/
|
|
48
|
+
// src/flavours/index.ts
|
|
69
49
|
var flavours = {
|
|
70
50
|
dollar: /\${([^{}]*)}/gm,
|
|
71
51
|
double: /{{([^{}]*)}}/gm,
|
|
@@ -74,92 +54,45 @@ var flavours = {
|
|
|
74
54
|
percent: /%{([^{}]*)}/gm
|
|
75
55
|
};
|
|
76
56
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
var VALID_RESULT_TYPES = Object.seal(['string', 'number']);
|
|
85
|
-
/**
|
|
86
|
-
* Create new paraphrase method instance
|
|
87
|
-
* @param {...RegExp[]} replacers
|
|
88
|
-
* @param {Boolean} [options.resolve=true] Should resolve dot notation within template
|
|
89
|
-
* @param {Boolean} [options.clean=false] Should remove unmatched template instances
|
|
90
|
-
* @returns {Function} phraser function instance
|
|
91
|
-
*
|
|
92
|
-
* @example const phraser = paraphrase(/\${([^{}]*)}/gm);
|
|
93
|
-
*
|
|
94
|
-
* phraser('Hello, ${name}', {name: 'Martin'})
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
function paraphrase() {
|
|
98
|
-
for (var _len = arguments.length, replacers = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
99
|
-
replacers[_key] = arguments[_key];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
var options = {
|
|
57
|
+
// src/index.ts
|
|
58
|
+
var VALID_RESULT_TYPES = Object.seal([
|
|
59
|
+
"string",
|
|
60
|
+
"number"
|
|
61
|
+
]);
|
|
62
|
+
function paraphrase(...patterns) {
|
|
63
|
+
const options = {
|
|
103
64
|
recursive: true,
|
|
104
65
|
resolve: true,
|
|
105
66
|
clean: false
|
|
106
67
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
Object.assign(options, replacers.pop());
|
|
68
|
+
if (patterns.length && isObject(patterns[patterns.length - 1])) {
|
|
69
|
+
Object.assign(options, patterns.pop());
|
|
110
70
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
* @param {...(String|number)} replacements Replacement for filling
|
|
118
|
-
* @return {String} Result
|
|
119
|
-
*/
|
|
120
|
-
|
|
121
|
-
function phraser() {
|
|
122
|
-
var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
123
|
-
var data = arguments.length > 1 ? arguments[1] : undefined;
|
|
124
|
-
|
|
125
|
-
if (typeof string !== 'string') {
|
|
126
|
-
throw new TypeError("paraphrase expects first argument to be a string, got a ".concat(_typeof(string), " (").concat(string, ")"));
|
|
71
|
+
Object.freeze(patterns);
|
|
72
|
+
function phraser(string = "", data, ...replacements) {
|
|
73
|
+
if (typeof string !== "string") {
|
|
74
|
+
throw new TypeError(
|
|
75
|
+
`paraphrase expects first argument to be a string, got a ${typeof string} (${string})`
|
|
76
|
+
);
|
|
127
77
|
}
|
|
128
|
-
|
|
129
78
|
if (!data) {
|
|
130
79
|
return string;
|
|
131
80
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
replacements[_key2 - 2] = arguments[_key2];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (VALID_RESULT_TYPES.includes(_typeof(data))) {
|
|
138
|
-
data = [data].concat(replacements);
|
|
81
|
+
if (VALID_RESULT_TYPES.includes(typeof data)) {
|
|
82
|
+
data = [data, ...replacements];
|
|
139
83
|
}
|
|
140
|
-
/**
|
|
141
|
-
* Replace method build with internal reference to the passed in data structure
|
|
142
|
-
* @param {String} haystack The full string match
|
|
143
|
-
* @param {String} needle The content to identify as data member
|
|
144
|
-
* @return {String} Found value
|
|
145
|
-
*/
|
|
146
|
-
|
|
147
|
-
|
|
148
84
|
function replace(haystack, needle) {
|
|
149
|
-
|
|
150
|
-
return VALID_RESULT_TYPES.includes(
|
|
85
|
+
const replacement = options.resolve ? notate(data, needle.trim()) : data[needle.trim()];
|
|
86
|
+
return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : options.clean ? "" : haystack;
|
|
151
87
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
return !options.recursive || string === result ? result : phraser
|
|
88
|
+
const result = patterns.reduce(
|
|
89
|
+
(string2, pattern) => string2.replace(pattern, replace),
|
|
90
|
+
string
|
|
91
|
+
);
|
|
92
|
+
return !options.recursive || string === result ? result : phraser(result, data, ...replacements);
|
|
157
93
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
get: function get() {
|
|
161
|
-
return replacers;
|
|
162
|
-
}
|
|
94
|
+
Object.defineProperty(phraser, "patterns", {
|
|
95
|
+
get: () => patterns
|
|
163
96
|
});
|
|
164
97
|
return phraser;
|
|
165
98
|
}
|
|
@@ -168,13 +101,20 @@ var double = paraphrase(flavours.double);
|
|
|
168
101
|
var single = paraphrase(flavours.single);
|
|
169
102
|
var percent = paraphrase(flavours.percent);
|
|
170
103
|
var hash = paraphrase(flavours.hash);
|
|
171
|
-
var loose = paraphrase(
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
exports
|
|
180
|
-
|
|
104
|
+
var loose = paraphrase(
|
|
105
|
+
flavours.dollar,
|
|
106
|
+
flavours.double,
|
|
107
|
+
flavours.percent,
|
|
108
|
+
flavours.hash,
|
|
109
|
+
flavours.single
|
|
110
|
+
);
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
dollar,
|
|
114
|
+
double,
|
|
115
|
+
hash,
|
|
116
|
+
loose,
|
|
117
|
+
paraphrase,
|
|
118
|
+
percent,
|
|
119
|
+
single
|
|
120
|
+
});
|
package/index.mjs
CHANGED
|
@@ -1,69 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
} else {
|
|
9
|
-
_typeof = function (obj) {
|
|
10
|
-
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
|
11
|
-
};
|
|
1
|
+
// src/notate/index.ts
|
|
2
|
+
function notate(source, string = "") {
|
|
3
|
+
if (typeof string !== "string") {
|
|
4
|
+
throw new TypeError(
|
|
5
|
+
`Expected notation query to be a string, instead got ${typeof string} (${string})`
|
|
6
|
+
);
|
|
12
7
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Resolve dot notation strings
|
|
19
|
-
*
|
|
20
|
-
* @param {Object} context Object to start notation search (defaults to global scope)
|
|
21
|
-
* @param {String} [string=''] Dot notation representation
|
|
22
|
-
* @return {Any} Whatever it finds / undefined
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* const obj = {
|
|
26
|
-
* top_level: {
|
|
27
|
-
* nested: {
|
|
28
|
-
* value: 'My Value'
|
|
29
|
-
* }
|
|
30
|
-
* }
|
|
31
|
-
* };
|
|
32
|
-
*
|
|
33
|
-
* notate(obj, 'top_level.nested.value');
|
|
34
|
-
* // 'My Value'
|
|
35
|
-
*
|
|
36
|
-
* notate(obj, 'top_level.missing.value');
|
|
37
|
-
* // undefined
|
|
38
|
-
*/
|
|
39
|
-
function notate(source) {
|
|
40
|
-
var string = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
41
|
-
|
|
42
|
-
if (typeof string !== 'string') {
|
|
43
|
-
throw new TypeError("Expected notation query to be a string, instead got ".concat(_typeof(string), " (").concat(string, ")"));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return string.split('.').reduce(function (previous, current) {
|
|
47
|
-
return _typeof(previous) === 'object' && previous ? previous[current] : previous;
|
|
48
|
-
}, source);
|
|
8
|
+
return string.split(".").reduce(
|
|
9
|
+
(previous, current) => typeof previous === "object" && previous ? previous[current] : previous,
|
|
10
|
+
source
|
|
11
|
+
);
|
|
49
12
|
}
|
|
50
13
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
* @param {any} obj
|
|
54
|
-
* @return {boolean}
|
|
55
|
-
*/
|
|
56
|
-
var isObject = function isObject(obj) {
|
|
57
|
-
return "".concat(obj) === '[object Object]';
|
|
58
|
-
};
|
|
14
|
+
// src/isObject/index.ts
|
|
15
|
+
var isObject = (obj) => `${obj}` === "[object Object]";
|
|
59
16
|
|
|
60
|
-
|
|
61
|
-
* @property {RegExp} dollar 'Hello, ${name}'
|
|
62
|
-
* @property {RegExp} double 'Hello, {{name}}'
|
|
63
|
-
* @property {RegExp} single 'Hello, {name}'
|
|
64
|
-
* @property {RegExp} hash 'Hello, #{name}'
|
|
65
|
-
* @property {RegExp} percent 'Hello, %{name}'
|
|
66
|
-
*/
|
|
17
|
+
// src/flavours/index.ts
|
|
67
18
|
var flavours = {
|
|
68
19
|
dollar: /\${([^{}]*)}/gm,
|
|
69
20
|
double: /{{([^{}]*)}}/gm,
|
|
@@ -72,92 +23,45 @@ var flavours = {
|
|
|
72
23
|
percent: /%{([^{}]*)}/gm
|
|
73
24
|
};
|
|
74
25
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
var VALID_RESULT_TYPES = Object.seal(['string', 'number']);
|
|
83
|
-
/**
|
|
84
|
-
* Create new paraphrase method instance
|
|
85
|
-
* @param {...RegExp[]} replacers
|
|
86
|
-
* @param {Boolean} [options.resolve=true] Should resolve dot notation within template
|
|
87
|
-
* @param {Boolean} [options.clean=false] Should remove unmatched template instances
|
|
88
|
-
* @returns {Function} phraser function instance
|
|
89
|
-
*
|
|
90
|
-
* @example const phraser = paraphrase(/\${([^{}]*)}/gm);
|
|
91
|
-
*
|
|
92
|
-
* phraser('Hello, ${name}', {name: 'Martin'})
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
function paraphrase() {
|
|
96
|
-
for (var _len = arguments.length, replacers = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
97
|
-
replacers[_key] = arguments[_key];
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
var options = {
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var VALID_RESULT_TYPES = Object.seal([
|
|
28
|
+
"string",
|
|
29
|
+
"number"
|
|
30
|
+
]);
|
|
31
|
+
function paraphrase(...patterns) {
|
|
32
|
+
const options = {
|
|
101
33
|
recursive: true,
|
|
102
34
|
resolve: true,
|
|
103
35
|
clean: false
|
|
104
36
|
};
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
Object.assign(options, replacers.pop());
|
|
37
|
+
if (patterns.length && isObject(patterns[patterns.length - 1])) {
|
|
38
|
+
Object.assign(options, patterns.pop());
|
|
108
39
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
* @param {...(String|number)} replacements Replacement for filling
|
|
116
|
-
* @return {String} Result
|
|
117
|
-
*/
|
|
118
|
-
|
|
119
|
-
function phraser() {
|
|
120
|
-
var string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
121
|
-
var data = arguments.length > 1 ? arguments[1] : undefined;
|
|
122
|
-
|
|
123
|
-
if (typeof string !== 'string') {
|
|
124
|
-
throw new TypeError("paraphrase expects first argument to be a string, got a ".concat(_typeof(string), " (").concat(string, ")"));
|
|
40
|
+
Object.freeze(patterns);
|
|
41
|
+
function phraser(string = "", data, ...replacements) {
|
|
42
|
+
if (typeof string !== "string") {
|
|
43
|
+
throw new TypeError(
|
|
44
|
+
`paraphrase expects first argument to be a string, got a ${typeof string} (${string})`
|
|
45
|
+
);
|
|
125
46
|
}
|
|
126
|
-
|
|
127
47
|
if (!data) {
|
|
128
48
|
return string;
|
|
129
49
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
replacements[_key2 - 2] = arguments[_key2];
|
|
50
|
+
if (VALID_RESULT_TYPES.includes(typeof data)) {
|
|
51
|
+
data = [data, ...replacements];
|
|
133
52
|
}
|
|
134
|
-
|
|
135
|
-
if (VALID_RESULT_TYPES.includes(_typeof(data))) {
|
|
136
|
-
data = [data].concat(replacements);
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Replace method build with internal reference to the passed in data structure
|
|
140
|
-
* @param {String} haystack The full string match
|
|
141
|
-
* @param {String} needle The content to identify as data member
|
|
142
|
-
* @return {String} Found value
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
53
|
function replace(haystack, needle) {
|
|
147
|
-
|
|
148
|
-
return VALID_RESULT_TYPES.includes(
|
|
54
|
+
const replacement = options.resolve ? notate(data, needle.trim()) : data[needle.trim()];
|
|
55
|
+
return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : options.clean ? "" : haystack;
|
|
149
56
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return !options.recursive || string === result ? result : phraser
|
|
57
|
+
const result = patterns.reduce(
|
|
58
|
+
(string2, pattern) => string2.replace(pattern, replace),
|
|
59
|
+
string
|
|
60
|
+
);
|
|
61
|
+
return !options.recursive || string === result ? result : phraser(result, data, ...replacements);
|
|
155
62
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
get: function get() {
|
|
159
|
-
return replacers;
|
|
160
|
-
}
|
|
63
|
+
Object.defineProperty(phraser, "patterns", {
|
|
64
|
+
get: () => patterns
|
|
161
65
|
});
|
|
162
66
|
return phraser;
|
|
163
67
|
}
|
|
@@ -166,7 +70,19 @@ var double = paraphrase(flavours.double);
|
|
|
166
70
|
var single = paraphrase(flavours.single);
|
|
167
71
|
var percent = paraphrase(flavours.percent);
|
|
168
72
|
var hash = paraphrase(flavours.hash);
|
|
169
|
-
var loose = paraphrase(
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
73
|
+
var loose = paraphrase(
|
|
74
|
+
flavours.dollar,
|
|
75
|
+
flavours.double,
|
|
76
|
+
flavours.percent,
|
|
77
|
+
flavours.hash,
|
|
78
|
+
flavours.single
|
|
79
|
+
);
|
|
80
|
+
export {
|
|
81
|
+
dollar,
|
|
82
|
+
double,
|
|
83
|
+
hash,
|
|
84
|
+
loose,
|
|
85
|
+
paraphrase,
|
|
86
|
+
percent,
|
|
87
|
+
single
|
|
88
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "paraphrase",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0-rc-dcd4648",
|
|
4
4
|
"description": "🧩 Create flavoured string template interpolation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"string",
|
|
@@ -24,28 +24,36 @@
|
|
|
24
24
|
"module": "./index.mjs",
|
|
25
25
|
"browser": "./index.mjs",
|
|
26
26
|
"exports": {
|
|
27
|
+
"./package.json": "./package.json",
|
|
27
28
|
".": {
|
|
28
|
-
"browser":
|
|
29
|
+
"browser": {
|
|
30
|
+
"import": "./index.mjs",
|
|
31
|
+
"require": "./index.js"
|
|
32
|
+
},
|
|
33
|
+
"node": {
|
|
34
|
+
"import": "./index.mjs",
|
|
35
|
+
"require": "./index.js"
|
|
36
|
+
},
|
|
29
37
|
"import": "./index.mjs",
|
|
30
38
|
"require": "./index.js",
|
|
31
|
-
"node": "./index.js",
|
|
32
39
|
"default": "./index.js"
|
|
33
40
|
}
|
|
34
41
|
},
|
|
42
|
+
"types": "./types/index.d.ts",
|
|
35
43
|
"scripts": {
|
|
36
|
-
"test": "
|
|
37
|
-
"
|
|
38
|
-
"build": "
|
|
44
|
+
"test": "jest",
|
|
45
|
+
"format": "prettier --write .",
|
|
46
|
+
"build": "./scripts/build.sh",
|
|
47
|
+
"prepublishOnly": "npm run build"
|
|
39
48
|
},
|
|
40
49
|
"devDependencies": {
|
|
41
|
-
"@
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
"standard": "^16.0.3"
|
|
50
|
+
"@types/jest": "^29.2.3",
|
|
51
|
+
"esbuild": "^0.15.16",
|
|
52
|
+
"jest": "^29.3.1",
|
|
53
|
+
"prettier": "^2.8.0",
|
|
54
|
+
"ts-jest": "^29.0.3"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"tag": "typescript"
|
|
50
58
|
}
|
|
51
59
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const flavours: Record<string, RegExp>;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
interface IParaphraseOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Should continue to resolve result string until replacements have been exhausted
|
|
4
|
+
*/
|
|
5
|
+
recursive?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Should resolve dot notation within template
|
|
8
|
+
*/
|
|
9
|
+
resolve?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Should remove unmatched template instances
|
|
12
|
+
*/
|
|
13
|
+
clean?: boolean;
|
|
14
|
+
}
|
|
15
|
+
interface Parser {
|
|
16
|
+
(string: string | undefined, ...data: (Record<string, any> | string | number)[]): string;
|
|
17
|
+
patterns: RegExp[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create new paraphrase method instance
|
|
21
|
+
* @param {...RegExp[]} replacers
|
|
22
|
+
* @param {IParaphraseOptions} [options]
|
|
23
|
+
* @returns {Function} phraser function instance
|
|
24
|
+
*
|
|
25
|
+
* @example const phraser = paraphrase(/\${([^{}]*)}/gm);
|
|
26
|
+
*
|
|
27
|
+
* phraser('Hello, ${name}', {name: 'Martin'})
|
|
28
|
+
*/
|
|
29
|
+
export declare function paraphrase(...patterns: (RegExp | IParaphraseOptions)[]): Parser;
|
|
30
|
+
export declare const dollar: Parser;
|
|
31
|
+
export declare const double: Parser;
|
|
32
|
+
export declare const single: Parser;
|
|
33
|
+
export declare const percent: Parser;
|
|
34
|
+
export declare const hash: Parser;
|
|
35
|
+
export declare const loose: Parser;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve dot notation strings
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} context Object to start notation search (defaults to global scope)
|
|
5
|
+
* @param {String} [string=''] Dot notation representation
|
|
6
|
+
* @return {Any} Whatever it finds / undefined
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const obj = {
|
|
10
|
+
* top_level: {
|
|
11
|
+
* nested: {
|
|
12
|
+
* value: 'My Value'
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* notate(obj, 'top_level.nested.value');
|
|
18
|
+
* // 'My Value'
|
|
19
|
+
*
|
|
20
|
+
* notate(obj, 'top_level.missing.value');
|
|
21
|
+
* // undefined
|
|
22
|
+
*/
|
|
23
|
+
export declare function notate(source: any, string?: string): any;
|
package/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js.map","sources":["src/notate/index.js","src/isObject/index.js","src/flavours/index.js","src/index.js"],"sourcesContent":["/**\n * Resolve dot notation strings\n *\n * @param {Object} context Object to start notation search (defaults to global scope)\n * @param {String} [string=''] Dot notation representation\n * @return {Any} Whatever it finds / undefined\n *\n * @example\n * const obj = {\n * top_level: {\n * nested: {\n * value: 'My Value'\n * }\n * }\n * };\n *\n * notate(obj, 'top_level.nested.value');\n * // 'My Value'\n *\n * notate(obj, 'top_level.missing.value');\n * // undefined\n */\nexport function notate (source, string = '') {\n if (typeof string !== 'string') {\n throw new TypeError(`Expected notation query to be a string, instead got ${typeof string} (${string})`)\n }\n return string\n .split('.')\n .reduce(\n (previous, current) => typeof previous === 'object' && previous\n ? previous[current]\n : previous,\n source\n )\n}\n","/**\n * Is this a basic object?\n * @param {any} obj\n * @return {boolean}\n */\nexport const isObject = obj => `${obj}` === '[object Object]'\n","/**\n * @property {RegExp} dollar 'Hello, ${name}'\n * @property {RegExp} double 'Hello, {{name}}'\n * @property {RegExp} single 'Hello, {name}'\n * @property {RegExp} hash 'Hello, #{name}'\n * @property {RegExp} percent 'Hello, %{name}'\n */\nexport const flavours = {\n dollar: /\\${([^{}]*)}/gm,\n double: /{{([^{}]*)}}/gm,\n single: /{([^{}]*)}/gm,\n hash: /#{([^{}]*)}/gm,\n percent: /%{([^{}]*)}/gm\n}\n","import { notate } from './notate/index.js'\nimport { isObject } from './isObject/index.js'\nimport { flavours } from './flavours/index.js'\n\n/**\n * Valid types of results for the interpolated string\n * @private\n * @type {Array}\n * @member {String|Number}\n */\nconst VALID_RESULT_TYPES = Object.seal(['string', 'number'])\n\n/**\n * Create new paraphrase method instance\n * @param {...RegExp[]} replacers\n * @param {Boolean} [options.resolve=true] Should resolve dot notation within template\n * @param {Boolean} [options.clean=false] Should remove unmatched template instances\n * @returns {Function} phraser function instance\n *\n * @example const phraser = paraphrase(/\\${([^{}]*)}/gm);\n *\n * phraser('Hello, ${name}', {name: 'Martin'})\n */\n\nexport function paraphrase (...replacers) {\n const options = {\n recursive: true,\n resolve: true,\n clean: false\n }\n if (replacers.length && isObject(replacers[replacers.length - 1])) {\n Object.assign(options, replacers.pop())\n }\n\n Object.freeze(replacers)\n\n /**\n * phraser description\n * @param {String} string Template\n * @param {Object|(String|number)} data Data for filling\n * @param {...(String|number)} replacements Replacement for filling\n * @return {String} Result\n */\n function phraser (string = '', data, ...replacements) {\n if (typeof string !== 'string') {\n throw new TypeError(`paraphrase expects first argument to be a string, got a ${typeof string} (${string})`)\n }\n\n if (!data) {\n return string\n }\n\n if (VALID_RESULT_TYPES.includes(typeof data)) {\n data = [data, ...replacements]\n }\n\n /**\n * Replace method build with internal reference to the passed in data structure\n * @param {String} haystack The full string match\n * @param {String} needle The content to identify as data member\n * @return {String} Found value\n */\n function replace (haystack, needle) {\n const replacement = options.resolve ? notate(data, needle.trim()) : data[needle.trim()]\n\n return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : options.clean ? '' : haystack\n }\n\n const result = replacers.reduce((string, replacer) => string.replace(replacer, replace), string)\n\n return !options.recursive || string === result\n ? result\n : phraser(result, data, ...replacements)\n }\n\n Object.defineProperty(\n phraser,\n 'patterns',\n {\n get: () => replacers\n }\n )\n\n return phraser\n}\n\nexport const dollar = paraphrase(flavours.dollar)\nexport const double = paraphrase(flavours.double)\nexport const single = paraphrase(flavours.single)\nexport const percent = paraphrase(flavours.percent)\nexport const hash = paraphrase(flavours.hash)\nexport const loose = paraphrase(\n flavours.dollar,\n flavours.double,\n flavours.percent,\n flavours.hash,\n flavours.single\n)\n"],"names":["notate","source","string","TypeError","split","reduce","previous","current","isObject","obj","flavours","dollar","double","single","hash","percent","VALID_RESULT_TYPES","Object","seal","paraphrase","replacers","options","recursive","resolve","clean","length","assign","pop","freeze","phraser","data","replacements","includes","replace","haystack","needle","replacement","trim","result","replacer","defineProperty","get","loose"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,MAAT,CAAiBC,MAAjB,EAAsC;AAAA,MAAbC,MAAa,uEAAJ,EAAI;;AAC3C,MAAI,OAAOA,MAAP,KAAkB,QAAtB,EAAgC;AAC9B,UAAM,IAAIC,SAAJ,uEAA4ED,MAA5E,gBAAuFA,MAAvF,OAAN;AACD;;AACD,SAAOA,MAAM,CACVE,KADI,CACE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GAAG,EAAE;AAAA,aAAMrB,SAAN;AAAA;AADP,GAHF;AAQA,SAAOS,OAAP;AACD;IAEYlB,MAAM,GAAGQ,UAAU,CAACT,QAAQ,CAACC,MAAV;IACnBC,MAAM,GAAGO,UAAU,CAACT,QAAQ,CAACE,MAAV;IACnBC,MAAM,GAAGM,UAAU,CAACT,QAAQ,CAACG,MAAV;IACnBE,OAAO,GAAGI,UAAU,CAACT,QAAQ,CAACK,OAAV;IACpBD,IAAI,GAAGK,UAAU,CAACT,QAAQ,CAACI,IAAV;IACjB4B,KAAK,GAAGvB,UAAU,CAC7BT,QAAQ,CAACC,MADoB,EAE7BD,QAAQ,CAACE,MAFoB,EAG7BF,QAAQ,CAACK,OAHoB,EAI7BL,QAAQ,CAACI,IAJoB,EAK7BJ,QAAQ,CAACG,MALoB;;;;;;;;;;"}
|
package/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs.map","sources":["src/notate/index.js","src/isObject/index.js","src/flavours/index.js","src/index.js"],"sourcesContent":["/**\n * Resolve dot notation strings\n *\n * @param {Object} context Object to start notation search (defaults to global scope)\n * @param {String} [string=''] Dot notation representation\n * @return {Any} Whatever it finds / undefined\n *\n * @example\n * const obj = {\n * top_level: {\n * nested: {\n * value: 'My Value'\n * }\n * }\n * };\n *\n * notate(obj, 'top_level.nested.value');\n * // 'My Value'\n *\n * notate(obj, 'top_level.missing.value');\n * // undefined\n */\nexport function notate (source, string = '') {\n if (typeof string !== 'string') {\n throw new TypeError(`Expected notation query to be a string, instead got ${typeof string} (${string})`)\n }\n return string\n .split('.')\n .reduce(\n (previous, current) => typeof previous === 'object' && previous\n ? previous[current]\n : previous,\n source\n )\n}\n","/**\n * Is this a basic object?\n * @param {any} obj\n * @return {boolean}\n */\nexport const isObject = obj => `${obj}` === '[object Object]'\n","/**\n * @property {RegExp} dollar 'Hello, ${name}'\n * @property {RegExp} double 'Hello, {{name}}'\n * @property {RegExp} single 'Hello, {name}'\n * @property {RegExp} hash 'Hello, #{name}'\n * @property {RegExp} percent 'Hello, %{name}'\n */\nexport const flavours = {\n dollar: /\\${([^{}]*)}/gm,\n double: /{{([^{}]*)}}/gm,\n single: /{([^{}]*)}/gm,\n hash: /#{([^{}]*)}/gm,\n percent: /%{([^{}]*)}/gm\n}\n","import { notate } from './notate/index.js'\nimport { isObject } from './isObject/index.js'\nimport { flavours } from './flavours/index.js'\n\n/**\n * Valid types of results for the interpolated string\n * @private\n * @type {Array}\n * @member {String|Number}\n */\nconst VALID_RESULT_TYPES = Object.seal(['string', 'number'])\n\n/**\n * Create new paraphrase method instance\n * @param {...RegExp[]} replacers\n * @param {Boolean} [options.resolve=true] Should resolve dot notation within template\n * @param {Boolean} [options.clean=false] Should remove unmatched template instances\n * @returns {Function} phraser function instance\n *\n * @example const phraser = paraphrase(/\\${([^{}]*)}/gm);\n *\n * phraser('Hello, ${name}', {name: 'Martin'})\n */\n\nexport function paraphrase (...replacers) {\n const options = {\n recursive: true,\n resolve: true,\n clean: false\n }\n if (replacers.length && isObject(replacers[replacers.length - 1])) {\n Object.assign(options, replacers.pop())\n }\n\n Object.freeze(replacers)\n\n /**\n * phraser description\n * @param {String} string Template\n * @param {Object|(String|number)} data Data for filling\n * @param {...(String|number)} replacements Replacement for filling\n * @return {String} Result\n */\n function phraser (string = '', data, ...replacements) {\n if (typeof string !== 'string') {\n throw new TypeError(`paraphrase expects first argument to be a string, got a ${typeof string} (${string})`)\n }\n\n if (!data) {\n return string\n }\n\n if (VALID_RESULT_TYPES.includes(typeof data)) {\n data = [data, ...replacements]\n }\n\n /**\n * Replace method build with internal reference to the passed in data structure\n * @param {String} haystack The full string match\n * @param {String} needle The content to identify as data member\n * @return {String} Found value\n */\n function replace (haystack, needle) {\n const replacement = options.resolve ? notate(data, needle.trim()) : data[needle.trim()]\n\n return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : options.clean ? '' : haystack\n }\n\n const result = replacers.reduce((string, replacer) => string.replace(replacer, replace), string)\n\n return !options.recursive || string === result\n ? result\n : phraser(result, data, ...replacements)\n }\n\n Object.defineProperty(\n phraser,\n 'patterns',\n {\n get: () => replacers\n }\n )\n\n return phraser\n}\n\nexport const dollar = paraphrase(flavours.dollar)\nexport const double = paraphrase(flavours.double)\nexport const single = paraphrase(flavours.single)\nexport const percent = paraphrase(flavours.percent)\nexport const hash = paraphrase(flavours.hash)\nexport const loose = paraphrase(\n flavours.dollar,\n flavours.double,\n flavours.percent,\n flavours.hash,\n flavours.single\n)\n"],"names":["notate","source","string","TypeError","split","reduce","previous","current","isObject","obj","flavours","dollar","double","single","hash","percent","VALID_RESULT_TYPES","Object","seal","paraphrase","replacers","options","recursive","resolve","clean","length","assign","pop","freeze","phraser","data","replacements","includes","replace","haystack","needle","replacement","trim","result","replacer","defineProperty","get","loose"],"mappings":";;;;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,MAAT,CAAiBC,MAAjB,EAAsC;AAAA,MAAbC,MAAa,uEAAJ,EAAI;;AAC3C,MAAI,OAAOA,MAAP,KAAkB,QAAtB,EAAgC;AAC9B,UAAM,IAAIC,SAAJ,uEAA4ED,MAA5E,gBAAuFA,MAAvF,OAAN;AACD;;AACD,SAAOA,MAAM,CACVE,KADI,CACE,GADF,EAEJC,MAFI,CAGH,UAACC,QAAD,EAAWC,OAAX;AAAA,WAAuB,QAAOD,QAAP,MAAoB,QAApB,IAAgCA,QAAhC,GACnBA,QAAQ,CAACC,OAAD,CADW,GAEnBD,QAFJ;AAAA,GAHG,EAMHL,MANG,CAAP;AAQD;;AClCD;AACA;AACA;AACA;AACA;AACO,IAAMO,QAAQ,GAAG,SAAXA,QAAW,CAAAC,GAAG;AAAA,SAAI,UAAGA,GAAH,MAAa,iBAAjB;AAAA,CAApB;;ACLP;AACA;AACA;AACA;AACA;AACA;AACA;AACO,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