Skip to content

Commit 683a202

Browse files
Documentation
1 parent 55fb562 commit 683a202

1 file changed

Lines changed: 32 additions & 33 deletions

File tree

‎README.md‎

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ A tag's default behaviour:
7878
* **interpolates placeholders** exactly like _untagged_ template literals and indents them accordingly;
7979
* **produces an object** which can be converted to a string.
8080

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.
8282

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.
8484

8585

8686
### Constructing tags
8787

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.
8989

9090

9191
### Tag results
9292

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:
9494

9595
```javascript
9696
console.log(`${ greeting }`);
@@ -100,8 +100,7 @@ console.log(String.raw(...greeting));
100100

101101
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.
102102

103-
104-
The result object is also an instance of the tag:
103+
The result is also an instance of the tag:
105104

106105
```javascript
107106
console.log(greeting instanceof sample); // true
@@ -118,25 +117,23 @@ let greet = sample.file('./examples/greeting.in');
118117
console.log(`${ greet(person) }`);
119118
```
120119

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.
124121

125122
**Security note: Do not load untrusted templates.**
126123

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.
128125

129126

130127
## Customizing tags
131128

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].
133130

134131

135132
### Indentation characters
136133

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.
138135

139-
An example:
136+
The property can be changed:
140137

141138
```javascript
142139
const custom = new StrOP('Custom indentation');
@@ -151,7 +148,7 @@ let todo = custom` TODO:
151148
console.log(`${ todo }`);
152149
```
153150

154-
Its output:
151+
The output:
155152

156153
```
157154
TODO:
@@ -161,7 +158,7 @@ Test code
161158

162159
_Reminder: the first line will not have its indentation adjusted if it is not empty and doesn't contain only indentation characters._
163160

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).
165162

166163

167164
### Rules and types
@@ -195,36 +192,36 @@ console.log(`${ score }`); // Your score is: 93.33%
195192

196193
Type handlers are called with `this` set to the calling tag and the value as an argument.
197194

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.
199196

200197
Rules and types take precedence over the interpolated values' string conversion methods.
201198

202199

203200
### Methods
204201

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.
206203

207204

208205
#### file(path)
209206

210207
This method is not called internally.
211208

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.
213210

214211
Custom implementations could be used to:
215212
* locate template files;
216213
* provide default values;
217214
* 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).
221216

222217
Custom implementations should (but are not required to) call the default implementation.
223218

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+
224221

225222
#### pass({ raw }, ...values)
226223

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.
228225

229226
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.
230227

@@ -233,33 +230,35 @@ Custom implementations could be used to:
233230
* perform additional processing (e.g. translation, [DSLs](https://en.wikipedia.org/wiki/Domain-specific_language));
234231
* alter the result.
235232

236-
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.
237234

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.
239236

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.
241238

242239

243240
#### render(value)
244241

245242
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`.
246243

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.
248245

249246
Custom implementations could be used to:
250-
* freeze the value;
247+
* freeze the input value;
251248
* change the substitution logic;
252249
* quote, decorate and/or escape the result;
253250
* cache conversion result.
254251

255252
Custom implementations must call the original implementation to apply rules and types, as there are no other means to achieve this.
256253

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+
257256

258257
#### rule(value, as)
259258

260259
This method is not called internally.
261260

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`.
263262

264263
There are no discernible use cases that would require overriding this method.
265264

@@ -270,9 +269,9 @@ Custom implementations must call the original implementation to register effecti
270269

271270
This method is not called internally.
272271

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.
274273

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.
276275

277276
There are no discernible use cases that would require overriding this method.
278277

@@ -281,7 +280,7 @@ Custom implementations must call the original implementation to register effecti
281280

282281
#### unindent(...strings)
283282

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].
285284

286285
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.
287286

@@ -295,7 +294,7 @@ Custom implementations could be used to:
295294
* disable or change indentation processing;
296295
* restrict certain usage patterns.
297296

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`.
299298

300299

301300
#### unwrap(value, hint = 'default')
@@ -310,7 +309,7 @@ Custom implementations could be used to:
310309
* alter the way built-in objects are converted;
311310
* convert additional types of objects.
312311

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.
314313

315314

316315
## Tests

0 commit comments

Comments
 (0)