-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfulfill.js
100 lines (78 loc) · 2.94 KB
/
fulfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// fulfill.js
// Douglas Crockford
// 2018-10-03
// Public Domain
/*property
freeze, reduce, replace, split
*/
const rx_delete_default = /[<>&%"\\]/g;
const rx_syntactic_variable = /\{([^{}:\s]+)(?::([^{}:\s]+))?\}/g;
//. Capturing groups:
//. [0] original (symbolic variable wrapped in braces)
//. [1] path
//. [2] encoding
function default_encoder(replacement) {
return String(replacement).replace(rx_delete_default, "");
}
export default Object.freeze(function fulfill(
string,
container,
encoder = default_encoder
) {
// The 'fulfill' function takes a string containing symbolic variables, a
// generator function or an object or array containing values to replace
// the symbolic variables, and an optional encoder function or object of encoder
// functions. The default encoder removes all angle brackets.
// Most of the work is done by the string 'replace' method that finds the
// symbolic variables, presenting them as the original substring, a path string,
// and an optional encoding string.
return string.replace(
rx_syntactic_variable,
function (original, path, encoding = "") {
try {
// Use the path to obtain a single replacement from the container of values.
// The path contains wun or more names (or numbers) separated by periods.
let replacement = (
typeof container === "function"
? container
: path.split(".").reduce(
function (refinement, element) {
return refinement[element];
},
container
)
);
// If the replacement value is a function,
// call it to obtain a replacement value.
if (typeof replacement === "function") {
replacement = replacement(path, encoding);
}
// If an encoder object was provided, call wun of its functions.
// If the encoder is a function, call it.
replacement = (
typeof encoder === "object"
? encoder[encoding]
: encoder
)(replacement, path, encoding);
// If the replacement is a number or boolean, convert it to a string.
if (
typeof replacement === "number"
|| typeof replacement === "boolean"
) {
replacement = String(replacement);
}
// If the replacement is a string, then do the substitution.
// Otherwise, leave the symbolic variable in its original state.
return (
typeof replacement === "string"
? replacement
: original
);
// If anything goes wrong, then leave the symbolic variable
// in its original state.
} catch (ignore) {
return original;
}
}
);
});