Skip to content

Instantly share code, notes, and snippets.

@joshuakfarrar
Last active May 20, 2019 19:27
Show Gist options
  • Save joshuakfarrar/292e444823da33feb28863b8630fd88b to your computer and use it in GitHub Desktop.
Save joshuakfarrar/292e444823da33feb28863b8630fd88b to your computer and use it in GitHub Desktop.
const _ = require('lodash');
const tags = (initialValue) => {
if ((typeof initialValue !== 'object' || Array.isArray(initialObject) === true)
|| (typeof initialValue !== 'undefined')) throw new Error('initial value must be undefined or an object');
const _tags = initialValue || {};
return {
addTag: (name, value) => {
if (typeof value === 'string' && value.length > 0) return tags(_.set(_tags, name, value));
else return tags(_tags);
},
value: () => {
return _tags;
}
}
}
let t = tags().addTag("henlo", "fren").addTag("configured", true).value();
@joshuakfarrar
Copy link
Author

i was creating an object and needed to assign to a value an object which processed data and did not define fields upon the child object for data fields which did not meet certain criteria. rather than a mutable solution, if (x) { object[key] = value; }, i instead opted for this immutable "toy".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment