Skip to content

Instantly share code, notes, and snippets.

@codeofnode
Last active July 7, 2017 13:28
Show Gist options
  • Save codeofnode/29099a8d889e0dea1588b63e2d6e371a to your computer and use it in GitHub Desktop.
Save codeofnode/29099a8d889e0dea1588b63e2d6e371a to your computer and use it in GitHub Desktop.
JSON extender that works even with stringified son
/**
* clone a string or object
* @param {object|string} input - the input to the function.
* @return {object|string} output - the cloned data.
*/
const clone = function clone(input) {
if (typeof input === 'string') return input;
return Object.assign({}, input);
};
/**
* extend a string or object
* [TODO if input is array]
* @param {object|string} input - the input to the function.
* @param {object} toExtend - toExtend.
* @return {object|string} output - the extended data.
*/
const extend = function extend(input, toExtend) {
if (typeof input === 'object') {
return Object.assign(input, toExtend);
}
if (typeof toExtend !== 'object' || toExtend === null) {
return input;
}
return `${input.slice(0, -1)},${JSON.stringify(toExtend).slice(1)}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment