Skip to content

Commit 8e427b9

Browse files
committed
Add Version 1
1 parent f2e3414 commit 8e427b9

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
# template-literals
1+
# template-literals
2+
3+
String templates using tagged template literals
4+
5+
```js
6+
// Create a template
7+
const template = tag `hi, ${'name'}!`
8+
9+
// Render the template
10+
template({ name: 'amsul' }) // 'hi, amsul!'
11+
```

index.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Creates a tagged template to use for interpolating values into a string.
3+
* Primarily "inspired" by https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals
4+
*
5+
* @example
6+
* const message = tag `${0} ${'name'}!`
7+
* message('Hello', { name: 'Amsul' }) // 'Hello Amsul!'
8+
*
9+
* @example
10+
* const message = tag `It goes ${0} ${1} and back to ${0}`
11+
* message('one', 'two') // 'It goes one two and back to one'
12+
*
13+
* @param {String[]} strings The array that contains the string templates.
14+
* @param {...String|Number} keys The keys to use for interpolating values.
15+
* @return {Function} The tagged template.
16+
*/
17+
export const tag = (strings, ...keys) => (...values) => {
18+
19+
const dict = values[values.length - 1] || {}
20+
const result = [strings[0]]
21+
const getValue = createGetValue(values, dict)
22+
23+
keys.forEach((key, index) => {
24+
const value = getValue(key)
25+
result.push(value, strings[index + 1])
26+
})
27+
28+
return result.join('')
29+
}
30+
31+
const createGetValue = (values, dict) => (key) => {
32+
const value = Number.isInteger(key) ? values[key] : dict[key]
33+
const valueType = typeof value
34+
if (valueType !== 'number' && valueType !== 'string') {
35+
console.error('The value for the key %o must be a string or number: %o', key, value)
36+
return ''
37+
}
38+
return value
39+
}

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "template-literals",
3+
"version": "1.0.0",
4+
"description": "String templates using tagged template literals",
5+
"main": "index.js",
6+
"repository": "https://github.com/amsul/template-literals",
7+
"author": "Amsul <[email protected]>",
8+
"license": "MIT"
9+
}

0 commit comments

Comments
 (0)