UNPKG

redis-type

Version:
58 lines (57 loc) 1.7 kB
"use strict"; /** * Set of methods to parse/stringify objects, arrays etc */ Object.defineProperty(exports, "__esModule", { value: true }); exports.parseArray = exports.stringifyObjectValues = exports.parseObjectValues = exports.toJSON = exports.parse = void 0; /** * JSON.stringify provider * @type {Function} */ var json = JSON.stringify; exports.toJSON = json; /** * JSON.parse provider * @type {Function} */ exports.parse = JSON.parse; /** * If data is represented as object with JSON in it's property values, * do parse each property value of this object and return resulting object * * @param {Object} obj Object to parse values of * @return {Object} Object with parsed values */ function parseObjectValues(obj) { obj !== null && Object.keys(obj).forEach(function (prop) { obj[prop] = (0, exports.parse)(obj[prop]); }); return obj; } exports.parseObjectValues = parseObjectValues; /** * If you need to apply operation like HMSET and you need to encode/stringify * Object's values for it -> this one should help * * @param {Object} obj Object to JSON it's values * @return {Object} Object with JSON-ed values */ function stringifyObjectValues(obj) { obj !== null && Object.keys(obj).forEach(function (prop) { obj[prop] = json(obj[prop]); }); return obj; } exports.stringifyObjectValues = stringifyObjectValues; /** * Parse Array of JSON strings * * @param {Array} arr Array to map each value with JSON.parse * @return {Array} Array of mapped values */ function parseArray(arr) { return arr.map(function (el) { return (0, exports.parse)(el); }); } exports.parseArray = parseArray;