stringify-object-strings
Version:
Stringify an object’s strings
26 lines (19 loc) • 567 B
JavaScript
;
const isPlainObj = require('is-plain-obj');
const stringify = obj => {
if (!isPlainObj(obj)) {
throw new TypeError('Expected a plain object');
}
return Object.keys(obj).reduce((newObj, key) => {
const value = obj[key];
if (typeof value === 'string') {
newObj[key] = JSON.stringify(value);
} else if (isPlainObj(value)) {
newObj[key] = stringify(value);
} else {
newObj[key] = value;
}
return newObj;
}, {});
};
module.exports = stringify;