-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
39 lines (35 loc) · 1.32 KB
/
index.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
/**
* Creates a tagged template to use for interpolating values into a string.
* Primarily "inspired" by https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals
*
* @example
* const message = tag `${0} ${'name'}!`
* message('Hello', { name: 'Amsul' }) // 'Hello Amsul!'
*
* @example
* const message = tag `It goes ${0} ${1} and back to ${0}`
* message('one', 'two') // 'It goes one two and back to one'
*
* @param {String[]} strings The array that contains the string templates.
* @param {...String|Number} keys The keys to use for interpolating values.
* @return {Function} The tagged template.
*/
export const tag = (strings, ...keys) => (...values) => {
const dict = values[values.length - 1] || {}
const result = [strings[0]]
const getValue = createGetValue(values, dict)
keys.forEach((key, index) => {
const value = getValue(key)
result.push(value, strings[index + 1])
})
return result.join('')
}
const createGetValue = (values, dict) => (key) => {
const value = Number.isInteger(key) ? values[key] : dict[key]
const valueType = typeof value
if (valueType !== 'number' && valueType !== 'string') {
console.error('The value for the key %o must be a string or number: %o', key, value)
return ''
}
return value
}