template-to-collection
Object, Map and WeakMap creation sugar using tagged template strings.
Installation
npm install template-to-collection
Note: The source is written in ES2015. It should fit right into your transpilation pipeline.
Usage
Object
with null
prototype using the Obj
tag
Create an let obj = CObj`a b fn ` // will create {a: 10, b: 10, fn: [Function]} whose __proto__ will be `null`
Map
using the Map
tag
Create a let map = CMap`a b fn ` // will create Map { 'a' => 10, 'fn' => [Function], 'b' => 'what' }
WeakMap
using the WMap
tag
Create a let key = a: 200let wmap = CWMap` ` // will create a WeakMap where wmap.has(key) === true
Note: If you intermix strings as keys and ${} expressions for keys, then things are gonna blow up.
For example, this is bad:
Obj${'key1'} ${10} key2 ${20}