set and get metadata on objects. Inspired by clojure/meta
npm install object-metadata{ default: meta, get, META } meta has the signature meta(data) => obj => obj. This writes the given data to obj[META]get(obj) => data, meta.get(obj) => data, or obj[META] => datajavascript
import meta from 'object-metadata'const arrayify = meta({
description:
wraps defined non-array elements in an array,,
examples: [
{input: ['foo'], output: ['foo']},
{input: 'foo', output: ['foo']},
{input: undefined, output: []}
]
})(
function arrayify(val){
return Array.isArray(val) ? val : (val !== undefined ? [val] : [])
}
)Object.keys(meta.get(arrayify))
//=> ['description', 'examples']
meta.get(meta)
/* =>
{
description: "Add meta data to an object under the symbol exported by this module as META",
usefulness: "attaching metadata to objects can be used in domain driven development, documentation, and testing"
}
*/
``Note: metadata can not be set on primitive types.