Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/southpolesteve/required-template
JS template strings that throw when passed undefined or null
https://github.com/southpolesteve/required-template
javascript literal tagged-template-literals template
Last synced: 7 days ago
JSON representation
JS template strings that throw when passed undefined or null
- Host: GitHub
- URL: https://github.com/southpolesteve/required-template
- Owner: southpolesteve
- Created: 2018-05-17T20:59:14.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-01-06T23:35:35.000Z (about 6 years ago)
- Last Synced: 2024-04-25T11:21:37.019Z (9 months ago)
- Topics: javascript, literal, tagged-template-literals, template
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# required-template
https://twitter.com/southpolesteve/status/997209218364166145
Seeing `undefined` or `null` show up in you or ES6 template strings? Instead of printing those literal values, this tagged template literal function will throw errors.
Here is an example with code:
```js
let works = 'works', isNull = null, isUndefined
// The default ES6 behavior is to print the strings "undefined" and "null"
`This ${works} correctly` // Prints "This works correctly"
`This ${isUndefined}` // Prints "This undefined"
`This ${isNull}` // Prints "This null"// 99.9% of time this is not what I want to happen!
// So tag your literals with `required-template` instead!
const r = require('required-template')r`This ${works} correctly` // Prints "This works correctly"
r`This ${isUndefined}` // Throws an error
r`This ${isNull}` // Throws an error
```