@ngha/nested-value
Version:
nestedValue the value at `path` of `object`
95 lines (87 loc) • 3.77 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
typeof define === 'function' && define.amd ? define('@ngha/nested-value', ['exports', '@angular/core'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.ngha = global.ngha || {}, global.ngha['nested-value'] = {}), global.ng.core));
}(this, (function (exports, core) { 'use strict';
var NestedValueService = /** @class */ (function () {
function NestedValueService() {
/**
* nestedValue the value at `path` of `object`. If the resolved value is
* `undefined`, the `defaultValue` is returned in its place.
*
* @param {Object} object The object to query.
* @param {Array|string} path The path of the property to get.
* @param defaultValue The value returned for `undefined` resolved values.
* @example
*
* const object = { 'a': 'aa', 'b': [123, '456'], c: [{ d: '123', e: 'abc' }, { d: '456', e: 'def' }] };
*
* nestedValue(object, 'a')
* // => 'aa'
*
* nestedValue(object, 'b')
* // => [123 , '456']
*
* nestedValue(object, ['c[0].d'])
* // => '123'
*
* nestedValue(object, ['c', [1], 'e'])
* // => 'abc'
*
* nestedValue(object, ['c', [1], 'f'])
* // => undefine
*
* nestedValue(object, ['c', [3], 'f'], 'default')
* // => 'default'
*
*/
this.nestedValue = function (object, path, defaultValue) {
var travel = function (regexp) { return String.prototype.split
.call(path, regexp)
.filter(Boolean)
.reduce(function (res, key) { return (res !== null && res !== undefined ? res[key] : res); }, object); };
var result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
return result === undefined || result === object ? defaultValue : result;
};
}
return NestedValueService;
}());
NestedValueService.decorators = [
{ type: core.Injectable }
];
var NestedValuePipe = /** @class */ (function () {
function NestedValuePipe(nestedValueService) {
this.nestedValueService = nestedValueService;
}
NestedValuePipe.prototype.transform = function (object, path, defaultValue) {
return this.nestedValueService.nestedValue(object, path, defaultValue);
};
return NestedValuePipe;
}());
NestedValuePipe.decorators = [
{ type: core.Pipe, args: [{ name: 'nestedValue' },] }
];
NestedValuePipe.ctorParameters = function () { return [
{ type: NestedValueService }
]; };
var NestedValueModule = /** @class */ (function () {
function NestedValueModule() {
}
return NestedValueModule;
}());
NestedValueModule.decorators = [
{ type: core.NgModule, args: [{
declarations: [NestedValuePipe],
exports: [NestedValuePipe],
providers: [NestedValueService]
},] }
];
/**
* Generated bundle index. Do not edit.
*/
exports.NestedValueModule = NestedValueModule;
exports.NestedValuePipe = NestedValuePipe;
exports.NestedValueService = NestedValueService;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=ngha-nested-value.umd.js.map