paraphrase 2.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 +19 -0
- package/README.md +58 -31
- package/index.js +114 -82
- package/index.mjs +88 -0
- package/package.json +34 -13
- 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/dollar/index.js +0 -15
- package/double/index.js +0 -15
- package/hash/index.js +0 -15
- package/percent/index.js +0 -15
- package/single/index.js +0 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
|
+
# 3.0.0 2021-09-21
|
|
2
|
+
|
|
3
|
+
## Breaking change
|
|
4
|
+
|
|
5
|
+
### All exports are named
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { paraphrase } from "paraphrase";
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Flavoured imports included
|
|
12
|
+
|
|
13
|
+
Flavoured pre-made versions should be imported from main entry and not from directory.
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { dollar } from "paraphrase";
|
|
17
|
+
```
|
|
18
|
+
|
|
1
19
|
# 2.0.0 2021-05-11
|
|
2
20
|
|
|
3
21
|
## Breaking change
|
|
4
22
|
|
|
5
23
|
### Remove compiled version.
|
|
24
|
+
|
|
6
25
|
If you are using a bundler with babel, make sure you transpile this package to your liking
|
package/README.md
CHANGED
|
@@ -11,116 +11,143 @@ npm i paraphrase
|
|
|
11
11
|
Creates new paraphrase method instance
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
|
|
131
|
+
```js
|
|
132
|
+
import { hash as phrase } from "paraphrase";
|
|
133
|
+
|
|
134
|
+
phrase("Hello, #{name}", { name: "Martin" }); // Hello, Martin
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### loose. Accommodate all of the above
|
|
138
|
+
|
|
114
139
|
```js
|
|
115
|
-
|
|
140
|
+
import { loose as phrase } from 'paraphrase';
|
|
116
141
|
|
|
117
|
-
phrase('Hello, #{name}', {name: 'Martin'}); // Hello, Martin
|
|
142
|
+
phrase('Hello, #{name.first} {name.last}', {name: { first: 'Martin', last: 'Prince' }); // Hello, Martin Prince
|
|
118
143
|
```
|
|
119
144
|
|
|
120
145
|
## patterns
|
|
121
|
-
|
|
146
|
+
|
|
147
|
+
A paraphrase instance exposes view to its patterns array (immutable)
|
|
148
|
+
|
|
122
149
|
```js
|
|
123
|
-
|
|
150
|
+
import { hash as phrase } from "paraphrase";
|
|
124
151
|
|
|
125
|
-
phrase.patterns // [ /#{([^{}]*)}/gm ]
|
|
152
|
+
phrase.patterns; // [ /#{([^{}]*)}/gm ]
|
|
126
153
|
```
|
package/index.js
CHANGED
|
@@ -1,88 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* @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
|
|
21
|
-
* @returns {Function} phraser function instance
|
|
22
|
-
*
|
|
23
|
-
* @example const phraser = paraphrase(/\${([^{}]*)}/gm);
|
|
24
|
-
*
|
|
25
|
-
* phraser('Hello, ${name}', {name: 'Martin'})
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
module.exports = function paraphrase(...replacers) {
|
|
29
|
-
const options = {
|
|
30
|
-
recursive: true,
|
|
31
|
-
resolve: true,
|
|
32
|
-
clean: false,
|
|
33
|
-
};
|
|
34
|
-
if (replacers.length && isObject(replacers[replacers.length - 1])) {
|
|
35
|
-
Object.assign(options, replacers.pop());
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* phraser description
|
|
40
|
-
* @param {String} string Template
|
|
41
|
-
* @param {Object|(String|number)} data Data for filling
|
|
42
|
-
* @param {...(String|number)} replacements Replacement for filling
|
|
43
|
-
* @return {String} Result
|
|
44
|
-
*/
|
|
45
|
-
function phraser(string = '', data, ...replacements) {
|
|
46
|
-
if (typeof string !== 'string') {
|
|
47
|
-
throw new TypeError(`paraphrase expects first argument to be a string, got a ${typeof string} (${string})`);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (!data) {
|
|
51
|
-
return string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (VALID_RESULT_TYPES.includes(typeof data)) {
|
|
55
|
-
data = [ data, ...replacements ];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Replace method build with internal reference to the passed in data structure
|
|
60
|
-
* @param {String} haystack The full string match
|
|
61
|
-
* @param {String} needle The content to identify as data member
|
|
62
|
-
* @return {String} Found value
|
|
63
|
-
*/
|
|
64
|
-
function replace(haystack, needle) {
|
|
65
|
-
const replacement = options.resolve ? notate(data, needle.trim()) : data[needle.trim()];
|
|
66
|
-
|
|
67
|
-
return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : options.clean ? '' : haystack;
|
|
68
|
-
}
|
|
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
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
69
18
|
|
|
70
|
-
|
|
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);
|
|
71
31
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return string.split(".").reduce(
|
|
40
|
+
(previous, current) => typeof previous === "object" && previous ? previous[current] : previous,
|
|
41
|
+
source
|
|
42
|
+
);
|
|
43
|
+
}
|
|
77
44
|
|
|
78
|
-
|
|
45
|
+
// src/isObject/index.ts
|
|
46
|
+
var isObject = (obj) => `${obj}` === "[object Object]";
|
|
79
47
|
|
|
80
|
-
|
|
48
|
+
// src/flavours/index.ts
|
|
49
|
+
var flavours = {
|
|
50
|
+
dollar: /\${([^{}]*)}/gm,
|
|
51
|
+
double: /{{([^{}]*)}}/gm,
|
|
52
|
+
single: /{([^{}]*)}/gm,
|
|
53
|
+
hash: /#{([^{}]*)}/gm,
|
|
54
|
+
percent: /%{([^{}]*)}/gm
|
|
81
55
|
};
|
|
82
56
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
57
|
+
// src/index.ts
|
|
58
|
+
var VALID_RESULT_TYPES = Object.seal([
|
|
59
|
+
"string",
|
|
60
|
+
"number"
|
|
61
|
+
]);
|
|
62
|
+
function paraphrase(...patterns) {
|
|
63
|
+
const options = {
|
|
64
|
+
recursive: true,
|
|
65
|
+
resolve: true,
|
|
66
|
+
clean: false
|
|
67
|
+
};
|
|
68
|
+
if (patterns.length && isObject(patterns[patterns.length - 1])) {
|
|
69
|
+
Object.assign(options, patterns.pop());
|
|
70
|
+
}
|
|
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
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (!data) {
|
|
79
|
+
return string;
|
|
80
|
+
}
|
|
81
|
+
if (VALID_RESULT_TYPES.includes(typeof data)) {
|
|
82
|
+
data = [data, ...replacements];
|
|
83
|
+
}
|
|
84
|
+
function replace(haystack, needle) {
|
|
85
|
+
const replacement = options.resolve ? notate(data, needle.trim()) : data[needle.trim()];
|
|
86
|
+
return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : options.clean ? "" : haystack;
|
|
87
|
+
}
|
|
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);
|
|
93
|
+
}
|
|
94
|
+
Object.defineProperty(phraser, "patterns", {
|
|
95
|
+
get: () => patterns
|
|
96
|
+
});
|
|
97
|
+
return phraser;
|
|
98
|
+
}
|
|
99
|
+
var dollar = paraphrase(flavours.dollar);
|
|
100
|
+
var double = paraphrase(flavours.double);
|
|
101
|
+
var single = paraphrase(flavours.single);
|
|
102
|
+
var percent = paraphrase(flavours.percent);
|
|
103
|
+
var hash = paraphrase(flavours.hash);
|
|
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
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
);
|
|
7
|
+
}
|
|
8
|
+
return string.split(".").reduce(
|
|
9
|
+
(previous, current) => typeof previous === "object" && previous ? previous[current] : previous,
|
|
10
|
+
source
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/isObject/index.ts
|
|
15
|
+
var isObject = (obj) => `${obj}` === "[object Object]";
|
|
16
|
+
|
|
17
|
+
// src/flavours/index.ts
|
|
18
|
+
var flavours = {
|
|
19
|
+
dollar: /\${([^{}]*)}/gm,
|
|
20
|
+
double: /{{([^{}]*)}}/gm,
|
|
21
|
+
single: /{([^{}]*)}/gm,
|
|
22
|
+
hash: /#{([^{}]*)}/gm,
|
|
23
|
+
percent: /%{([^{}]*)}/gm
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var VALID_RESULT_TYPES = Object.seal([
|
|
28
|
+
"string",
|
|
29
|
+
"number"
|
|
30
|
+
]);
|
|
31
|
+
function paraphrase(...patterns) {
|
|
32
|
+
const options = {
|
|
33
|
+
recursive: true,
|
|
34
|
+
resolve: true,
|
|
35
|
+
clean: false
|
|
36
|
+
};
|
|
37
|
+
if (patterns.length && isObject(patterns[patterns.length - 1])) {
|
|
38
|
+
Object.assign(options, patterns.pop());
|
|
39
|
+
}
|
|
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
|
+
);
|
|
46
|
+
}
|
|
47
|
+
if (!data) {
|
|
48
|
+
return string;
|
|
49
|
+
}
|
|
50
|
+
if (VALID_RESULT_TYPES.includes(typeof data)) {
|
|
51
|
+
data = [data, ...replacements];
|
|
52
|
+
}
|
|
53
|
+
function replace(haystack, needle) {
|
|
54
|
+
const replacement = options.resolve ? notate(data, needle.trim()) : data[needle.trim()];
|
|
55
|
+
return VALID_RESULT_TYPES.includes(typeof replacement) ? replacement : options.clean ? "" : haystack;
|
|
56
|
+
}
|
|
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);
|
|
62
|
+
}
|
|
63
|
+
Object.defineProperty(phraser, "patterns", {
|
|
64
|
+
get: () => patterns
|
|
65
|
+
});
|
|
66
|
+
return phraser;
|
|
67
|
+
}
|
|
68
|
+
var dollar = paraphrase(flavours.dollar);
|
|
69
|
+
var double = paraphrase(flavours.double);
|
|
70
|
+
var single = paraphrase(flavours.single);
|
|
71
|
+
var percent = paraphrase(flavours.percent);
|
|
72
|
+
var hash = paraphrase(flavours.hash);
|
|
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
|
+
"version": "3.1.0-rc-dcd4648",
|
|
4
4
|
"description": "🧩 Create flavoured string template interpolation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"string",
|
|
@@ -19,20 +19,41 @@
|
|
|
19
19
|
"url": "git+https://github.com/omrilotan/paraphrase.git"
|
|
20
20
|
},
|
|
21
21
|
"homepage": "https://omrilotan.com/paraphrase/",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
"type": "commonjs",
|
|
23
|
+
"main": "./index.js",
|
|
24
|
+
"module": "./index.mjs",
|
|
25
|
+
"browser": "./index.mjs",
|
|
26
|
+
"exports": {
|
|
27
|
+
"./package.json": "./package.json",
|
|
28
|
+
".": {
|
|
29
|
+
"browser": {
|
|
30
|
+
"import": "./index.mjs",
|
|
31
|
+
"require": "./index.js"
|
|
32
|
+
},
|
|
33
|
+
"node": {
|
|
34
|
+
"import": "./index.mjs",
|
|
35
|
+
"require": "./index.js"
|
|
36
|
+
},
|
|
37
|
+
"import": "./index.mjs",
|
|
38
|
+
"require": "./index.js",
|
|
39
|
+
"default": "./index.js"
|
|
40
|
+
}
|
|
27
41
|
},
|
|
28
|
-
"
|
|
29
|
-
|
|
42
|
+
"types": "./types/index.d.ts",
|
|
43
|
+
"scripts": {
|
|
44
|
+
"test": "jest",
|
|
45
|
+
"format": "prettier --write .",
|
|
46
|
+
"build": "./scripts/build.sh",
|
|
47
|
+
"prepublishOnly": "npm run build"
|
|
30
48
|
},
|
|
31
49
|
"devDependencies": {
|
|
32
|
-
"@
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
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"
|
|
37
58
|
}
|
|
38
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/dollar/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module dollar
|
|
3
|
-
* @since 1.1.0
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A paraphraser: Replace method build with internal reference to the passed in data structure
|
|
8
|
-
* @param {String} haystack The full string match
|
|
9
|
-
* @param {String} needle The content to identify as data member
|
|
10
|
-
* @return {String} Found value
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* dollar('Hello, ${name}', {name: 'Martin'}) // 'Hello, Martin'
|
|
14
|
-
*/
|
|
15
|
-
module.exports = require('../')(/\${([^{}]*)}/gm);
|
package/double/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module double
|
|
3
|
-
* @since 1.1.0
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A paraphraser: Replace method build with internal reference to the passed in data structure
|
|
8
|
-
* @param {String} haystack The full string match
|
|
9
|
-
* @param {String} needle The content to identify as data member
|
|
10
|
-
* @return {String} Found value
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* double('Hello, {{name}}', {name: 'Martin'}) // 'Hello, Martin'
|
|
14
|
-
*/
|
|
15
|
-
module.exports = require('../')(/{{([^{}]*)}}/gm);
|
package/hash/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module hash
|
|
3
|
-
* @since 1.1.0
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A paraphraser: Replace method build with internal reference to the passed in data structure
|
|
8
|
-
* @param {String} haystack The full string match
|
|
9
|
-
* @param {String} needle The content to identify as data member
|
|
10
|
-
* @return {String} Found value
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* hash('Hello, #{name}', {name: 'Martin'}) // 'Hello, Martin'
|
|
14
|
-
*/
|
|
15
|
-
module.exports = require('../')(/#{([^{}]*)}/gm);
|
package/percent/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module percent
|
|
3
|
-
* @since 1.1.0
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A paraphraser: Replace method build with internal reference to the passed in data structure
|
|
8
|
-
* @param {String} haystack The full string match
|
|
9
|
-
* @param {String} needle The content to identify as data member
|
|
10
|
-
* @return {String} Found value
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* percent('Hello, %{name}', {name: 'Martin'}) // 'Hello, Martin'
|
|
14
|
-
*/
|
|
15
|
-
module.exports = require('../')(/%{([^{}]*)}/gm);
|
package/single/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module single
|
|
3
|
-
* @since 1.5.0
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A paraphraser: Replace method build with internal reference to the passed in data structure
|
|
8
|
-
* @param {String} haystack The full string match
|
|
9
|
-
* @param {String} needle The content to identify as data member
|
|
10
|
-
* @return {String} Found value
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* double('Hello, {name}', {name: 'Martin'}) // 'Hello, Martin'
|
|
14
|
-
*/
|
|
15
|
-
module.exports = require('../')(/{([^{}]*)}/gm);
|