UNPKG

@botonic/react

Version:

Build Chatbots using React

44 lines 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapObject = exports.strToBool = exports.getProperty = void 0; /** * given an object and a property, returns the property if exists (recursively), else undefined * ex: * let obj = { a: { b: { c: 5 } } } * getProperty(obj, 'a.b.c'), returns 5 * getProperty(obj, 'a.b.z'), returns undefined */ const getProperty = (obj, property) => { if (!property) return undefined; const properties = property.split('.'); for (let i = 0; i < properties.length; i++) { const prop = properties[i]; // eslint-disable-next-line no-prototype-builtins if (!obj || !obj.hasOwnProperty(prop)) { return undefined; } else { obj = obj[prop]; } } return obj; }; exports.getProperty = getProperty; function strToBool(string) { const regex = /^\s*(true|1|on)\s*$/i; string = String(string); return regex.test(string); } exports.strToBool = strToBool; const mapObject = (obj, conversion = ([key, value]) => [key, value]) => { return (obj && Object.entries(obj) .map(conversion) .reduce(function (prev, curr) { prev[curr[0]] = curr[1]; return prev; }, {})); }; exports.mapObject = mapObject; //# sourceMappingURL=objects.js.map