Last active
July 7, 2017 13:28
-
-
Save codeofnode/29099a8d889e0dea1588b63e2d6e371a to your computer and use it in GitHub Desktop.
JSON extender that works even with stringified son
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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