moltres-utils
Version:
Utils for Moltres apps
58 lines (48 loc) • 1.5 kB
JavaScript
require("core-js/modules/es6.object.define-property");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _curry = _interopRequireDefault(require("../common/curry"));
var _getProp = _interopRequireDefault(require("./getProp"));
var _hasProp = _interopRequireDefault(require("./hasProp"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Returns whether or not a path exists in an object. Only the object's
* own properties are checked.
*
* @function
* @since v0.0.3
* @category data
* @typedefn Idx = String | Int
* @sig [Idx] -> {a} -> Boolean
* @param {Array} path The path to use.
* @param {Object} obj The object to check the path in.
* @returns {Boolean} Whether the path exists.
* @example
*
* hasPath(['a', 'b'], {a: {b: 2}}) // => true
* hasPath(['a', 'b'], {a: {b: undefined}}) // => true
* hasPath('a.b', {a: {c: 2}}) // => false
* hasPath([], {}) // => true
*/
var hasPath = (0, _curry.default)(function (path, obj) {
if (path.length === 0) {
return !!obj;
}
var val = obj;
var idx = 0;
while (idx < path.length) {
if ((0, _hasProp.default)(path[idx], val)) {
val = (0, _getProp.default)(path[idx], val);
idx += 1;
} else {
return false;
}
}
return true;
});
var _default = hasPath;
exports.default = _default;
//# sourceMappingURL=hasPath.js.map
;