Skip to content

Commit 25937c8

Browse files
authored
feat: Support string as an argument to the tag function (#132)
1 parent a1d8901 commit 25937c8

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

readme.md

+15
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [`oneLineCommaListsAnd`](#onelinecommalistsand)
4949
- [:wrench: Advanced Usage](#wrench-advanced-usage)
5050
- [Tail Processing](#tail-processing)
51+
- [Calling with strings](#calling-with-strings)
5152
- [Make Your Own Template Tag](#make-your-own-template-tag)
5253
- [Class is in Session: TemplateTag](#class-is-in-session-templatetag)
5354
- [The Anatomy of a Transformer](#the-anatomy-of-a-transformer)
@@ -482,6 +483,20 @@ oneLine(String.raw)`
482483

483484

484485

486+
487+
### Calling with strings
488+
489+
Sometimes you might want to use a tag on a normal string (e.g. for stripping the indentation). For that purpose just call a tag as a function with the passed string:
490+
491+
```js
492+
import {stripIndent} from 'common-tags'
493+
494+
stripIndent(" foo\n bar")
495+
// "foo\n bar"
496+
```
497+
498+
499+
485500
### Make Your Own Template Tag
486501

487502
`common-tags` exposes an interface that allows you to painlessly create your own template tags.

src/TemplateTag/TemplateTag.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class TemplateTag {
1313
*/
1414
constructor (...transformers) {
1515
// if first argument is an array, extrude it as a list of transformers
16-
if (transformers.length && Array.isArray(transformers[0])) {
16+
if (transformers.length > 0 && Array.isArray(transformers[0])) {
1717
transformers = transformers[0]
1818
}
1919

@@ -32,21 +32,26 @@ export default class TemplateTag {
3232
* Applies all transformers to a template literal tagged with this method.
3333
* If a function is passed as the first argument, assumes the function is a template tag
3434
* and applies it to the template, returning a template tag.
35-
* @param {(Function|Array<String>)} args[0] - Either a template tag or an array containing template strings separated by identifier
36-
* @param {...*} [args[1]] - Optional list of substitution values.
37-
* @return {(String|Function)} - Either an intermediary tag function or the results of processing the template.
35+
* @param {(Function|String|Array<String>)} strings - Either a template tag or an array containing template strings separated by identifier
36+
* @param {...*} ...expressions - Optional list of substitution values.
37+
* @return {(String|Function)} - Either an intermediary tag function or the results of processing the template.
3838
*/
39-
tag = (...args) => {
40-
// if the first argument passed is a function, assume it is a template tag and return
41-
// an intermediary tag that processes the template using the aforementioned tag, passing the
42-
// result to our tag
43-
if (typeof args[0] === 'function') {
44-
return this.interimTag.bind(this, args.shift())
39+
tag = (strings, ...expressions) => {
40+
if (typeof strings === 'function') {
41+
// if the first argument passed is a function, assume it is a template tag and return
42+
// an intermediary tag that processes the template using the aforementioned tag, passing the
43+
// result to our tag
44+
return this.interimTag.bind(this, strings)
45+
}
46+
47+
if (typeof strings === 'string') {
48+
// if the first argument passed is a string, just transform it
49+
return this.transformEndResult(strings)
4550
}
4651

4752
// else, return a transformed end result of processing the template with our tag
4853
return this.transformEndResult(
49-
args.shift().reduce(this.processSubstitutions.bind(this, args))
54+
strings.reduce(this.processSubstitutions.bind(this, expressions))
5055
)
5156
}
5257

src/TemplateTag/TemplateTag.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,21 @@ test('supports tail processing of another tag if first argument to tag is a tag'
8989
`
9090
t.is(raw, 'FOO BAR\n 500')
9191
})
92+
93+
test('supports passing string as a first argument', (t) => {
94+
let onSubstitutionCalls = 0
95+
const tag = new TemplateTag({
96+
onSubstitution () {
97+
onSubstitutionCalls += 1
98+
},
99+
onEndResult (endResult) {
100+
return endResult.toUpperCase().trim()
101+
}
102+
})
103+
const raw = tag(`
104+
foo bar
105+
${500}
106+
`)
107+
t.is(raw, 'FOO BAR\n 500')
108+
t.is(onSubstitutionCalls, 0, 'The `onSubstitution` hook shouldn\'t be called')
109+
})

0 commit comments

Comments
 (0)