You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-33Lines changed: 32 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,19 +78,19 @@ A tag's default behaviour:
78
78
***interpolates placeholders** exactly like _untagged_ template literals and indents them accordingly;
79
79
***produces an object** which can be converted to a string.
80
80
81
-
If the first line is not empty and doesn't contain only indentation characters, it will not have its indentation removed, nor will it be taken into account when computing the common indentation.
81
+
Single-line templates do not have their indentation adjusted.
82
82
83
-
Single-line templates do not have their indentation changed.
83
+
Multi-line templates preserve the first line if is not empty and doesn't contain only indentation characters; it is also not taken into account when computing the common indentation.
84
84
85
85
86
86
### Constructing tags
87
87
88
-
A single argument is passed to the [constructor](https://github.com/civicnet/strop/blob/master/index.js#L18-L71): the tag's `name`. It won't influence functionality, but can be useful for debugging.
88
+
A single argument is passed to the [constructor](https://github.com/civicnet/strop/blob/master/index.js#L18-L71): a `name` for the tag. It won't influence functionality, but can be useful for debugging.
89
89
90
90
91
91
### Tag results
92
92
93
-
By default, a tag produces an object which contains all the information needed to be _lazily_ converted to a string. This can be done in several ways:
93
+
By default, a tag produces an object which can be _lazily_ converted to a string:
While the default implementation aims to prevent later modifications of the tag result, it is possible for the original interpolated values to change before the result is converted or between separate invocations. [Custom implementations][Customizing tags] can prevent, restrict and/or handle these situations if needed.
102
102
103
-
104
-
The result object is also an instance of the tag:
103
+
The result is also an instance of the tag:
105
104
106
105
```javascript
107
106
console.log(greeting instanceof sample); // true
@@ -118,25 +117,23 @@ let greet = sample.file('./examples/greeting.in');
118
117
console.log(`${greet(person) }`);
119
118
```
120
119
121
-
The file must contain a bare template, i.e. without the opening and closing `` ` ``;
122
-
123
-
An error will be thrown if the file is not found or can't be parsed.
120
+
The file must contain a bare template, i.e. without the opening and closing `` ` ``. An error will be thrown if the file is not found or can't be parsed.
124
121
125
122
**Security note: Do not load untrusted templates.**
126
123
127
-
Loading a file returns a function which should be called with object arguments to provide context, i.e. the template will search their properties for any referenced values. The arguments are searched in the order they are passed; an error will be thrown if a required value is not found.
124
+
Loading a file returns a function which should be called with object arguments; the template will search their properties for any referenced values. The arguments are searched in the order they are passed. An error will be thrown if a required value is not found as a property in any of the arguments.
128
125
129
126
130
127
## Customizing tags
131
128
132
-
The removable[indentation characters] can be changed, interpolation can be customized using [rules and types], and the overall behaviour can be altered by overriding tags' [methods].
129
+
The adjustable[indentation characters] can be changed, interpolation can be customized using [rules and types], and the overall behaviour can be altered by overriding tags' [methods].
133
130
134
131
135
132
### Indentation characters
136
133
137
-
The indentation characters that a tag may remove are located in its `indent` property string; the default value of `'\t '` enables tags to remove tabs and spaces.
134
+
The indentation characters that a tag may adjust are located in its `indent` property string; the default value of `'\t '` enables tags to remove tabs and spaces.
138
135
139
-
An example:
136
+
The property can be changed:
140
137
141
138
```javascript
142
139
constcustom=newStrOP('Custom indentation');
@@ -151,7 +148,7 @@ let todo = custom` TODO:
151
148
console.log(`${ todo }`);
152
149
```
153
150
154
-
Its output:
151
+
The output:
155
152
156
153
```
157
154
TODO:
@@ -161,7 +158,7 @@ Test code
161
158
162
159
_Reminder: the first line will not have its indentation adjusted if it is not empty and doesn't contain only indentation characters._
163
160
164
-
If a mix of different indentation characters is used in the template, only _identical_ sequences at the beginning of _every_ eligible line are considered "common" (and removed).
161
+
If a mix of different indentation characters is used in the template, only _identical_ sequences at the beginning of _every_ eligible line are considered "common" (and adjusted).
165
162
166
163
167
164
### Rules and types
@@ -195,36 +192,36 @@ console.log(`${ score }`); // Your score is: 93.33%
195
192
196
193
Type handlers are called with `this` set to the calling tag and the value as an argument.
197
194
198
-
Every interpolated value's prototype (inheritance) chain is searched; only the most specialized type's handler will be called.
195
+
Every interpolated value's prototype (inheritance) chain is searched; only the most specialized type's handler is called.
199
196
200
197
Rules and types take precedence over the interpolated values' string conversion methods.
201
198
202
199
203
200
### Methods
204
201
205
-
A tag's behaviour can be customized by overriding its methods.
202
+
A tag's behaviour can be further customized by overriding its methods.
206
203
207
204
208
205
#### file(path)
209
206
210
207
This method is not called internally.
211
208
212
-
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L74-L87) loads the template file indicated by `path` and returns a function which can be called with objects, the keys of which are searched (in the order they are passed) during interpolation.
209
+
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L74-L87) loads the template file indicated by `path` and returns a function which can be called with objects, the keys of which are searched in the order they are passed during interpolation.
213
210
214
211
Custom implementations could be used to:
215
212
* locate template files;
216
213
* provide default values;
217
214
* validate and/or sanitize the arguments;
218
-
* alter invocation details.
219
-
220
-
The result of the (final) function call _should not_ be altered, as it is assumed to be identical to tagging template literals. Both can be customized by overriding the [**`pass`** method][pass] instead.
215
+
* change the invocation interface (e.g. argument order, currying).
221
216
222
217
Custom implementations should (but are not required to) call the default implementation.
223
218
219
+
The result of the (final) function call _should not_ be altered, as it is assumed to be identical to tagging template literals. If necessary, this can be achieved by overriding the [**`pass`** method][pass].
220
+
224
221
225
222
#### pass({ raw }, ...values)
226
223
227
-
This method is the actual tag function; it is called internally to prepare the result of a tag operation, after indendation is removed from the `raw` strings, and with the original `values`. It is used both for template literals and the default [**`file`** method][file]'s returned functions.
224
+
This method is called to prepare the result of a tag operation, after indendation is removed from the `raw` strings by the [**`unindent`** method][unindent], and with the original `values`. It is used by template literals and the default [**`file`** method][file]'s returned functions.
228
225
229
226
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L90-L118) returns an array-like object which wraps the parameters; when converted to a string, it calls the [**`render`** method][render] for every interpolated value. Indentation will be adjusted for the rendered results that span multiple lines.
230
227
@@ -233,33 +230,35 @@ Custom implementations could be used to:
The result is always processed to ensure it is an instance of the tag; this may break typed objects and does not work for primitives. Lastly, the result is [frozen](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze).
233
+
Custom implementations should (but are not required to) call the default implementation.
237
234
238
-
If built-in `Boolean`, `Date`, `Number` or `String` objects are returned, the default [**`unwrap`** method][unwrap] will (correctly) convert them to primitives.
235
+
If the returned value is an object, it is further processed internally to ensure it is an instance of the tag and [frozen](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze); this may break typed objects.
239
236
240
-
Custom implementations should (but are not required to) call the default implementation.
237
+
If built-in `Boolean`, `Date`, `Number` or `String` objects are returned, the default [**`unwrap`** method][unwrap] will be called when they are converted to strings to produce a correct representation.
241
238
242
239
243
240
#### render(value)
244
241
245
242
This method is used by objects returned by the default [**`pass`** method][pass] when they are converted to strings; it is called to produce a string representation for every interpolated `value`.
246
243
247
-
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L121-L140) searches [rules and types] for substitution and converts the value (or substitute) to a string.
244
+
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L121-L140) searches [rules and types] for substitution and converts the final result to a string.
248
245
249
246
Custom implementations could be used to:
250
-
* freeze the value;
247
+
* freeze the input value;
251
248
* change the substitution logic;
252
249
* quote, decorate and/or escape the result;
253
250
* cache conversion result.
254
251
255
252
Custom implementations must call the original implementation to apply rules and types, as there are no other means to achieve this.
256
253
254
+
The returned value is always converted to a string by objects returned by the default [**`pass`** method][pass] when they are converted to strings.
255
+
257
256
258
257
#### rule(value, as)
259
258
260
259
This method is not called internally.
261
260
262
-
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L143-L160)instructs the tag to replace every interpolated `value` with `as`.
261
+
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L143-L160)enables the default [**`render`** method][render] to replace every interpolated `value` with `as`.
263
262
264
263
There are no discernible use cases that would require overriding this method.
265
264
@@ -270,9 +269,9 @@ Custom implementations must call the original implementation to register effecti
270
269
271
270
This method is not called internally.
272
271
273
-
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L163-L173)instructs the tag to replace every interpolated value that is an instace of the `factory` function by calling the `handler` function with `this` set to the calling tag and the value as an argument.
272
+
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L163-L173)enables the default [**`render`** method][render] to replace every interpolated value that is an instace of the `factory` function by calling the `handler` function with `this` set to the calling tag and the value as an argument.
274
273
275
-
Every interpolated value's entire prototype (inheritance) chain is searched; if the value matches multiple types, only the most specialized one's handler will be called.
274
+
Every interpolated value's entire prototype (inheritance) chain is searched; if the value matches multiple registered types, only the most specialized one's handler will be called.
276
275
277
276
There are no discernible use cases that would require overriding this method.
278
277
@@ -281,7 +280,7 @@ Custom implementations must call the original implementation to register effecti
281
280
282
281
#### unindent(...strings)
283
282
284
-
This method is called during interpolation with the template's raw `strings`to remove [indentation characters].
283
+
This method is called during interpolation with the template's raw `strings`; the returned value is provided to the [**`pass`** method][pass].
285
284
286
285
The [default implementation](https://github.com/civicnet/strop/blob/master/index.js#L176-L249) trims leading and/or trailing lines that are empty (or contain only indentation characters) and removes any common indentation from all remaining non-empty lines.
287
286
@@ -295,7 +294,7 @@ Custom implementations could be used to:
295
294
* disable or change indentation processing;
296
295
* restrict certain usage patterns.
297
296
298
-
Custom implementations should (but are not required to) call the default implementation and must **always** return an array with the same length as `strings`.
297
+
Custom implementations should (but are not required to) call the default implementation; they must **always** return an array with the same length as `strings`.
299
298
300
299
301
300
#### unwrap(value, hint = 'default')
@@ -310,7 +309,7 @@ Custom implementations could be used to:
310
309
* alter the way built-in objects are converted;
311
310
* convert additional types of objects.
312
311
313
-
Custom implementations should (but are not required to) call the default implementation.
312
+
Custom implementations should (but are not required to) call the default implementation; they must **always** return a primitive value.
0 commit comments