@lucaspaganini/value-objects
Version:
TypeScript first validation and class creation library
35 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectPath = void 0;
var guards_1 = require("./guards");
var DOT_PROPERTY_PATTERN = /^[a-z_][\w]*$/i;
var isDotProperty = function (prop) {
if (guards_1.isNumber(prop))
return false;
return DOT_PROPERTY_PATTERN.test(prop);
};
var ObjectPath = /** @class */ (function () {
function ObjectPath(_pathArray) {
this._pathArray = _pathArray;
}
ObjectPath.prototype.push = function (prop) {
this._pathArray.push(prop);
};
ObjectPath.prototype.toArray = function () {
return this._pathArray.concat([]);
};
ObjectPath.prototype.valueOf = function () {
return this._pathArray.reduce(function (path, currentProp, i, arr) {
var lastProp = arr[i - 1];
var propPrefix = guards_1.isDefined(lastProp) && isDotProperty(currentProp) ? '.' : '';
var propString = isDotProperty(currentProp) ? currentProp : "[" + currentProp + "]";
return path + propPrefix + propString;
}, '');
};
ObjectPath.prototype.toJSON = function () {
return this.valueOf();
};
return ObjectPath;
}());
exports.ObjectPath = ObjectPath;
//# sourceMappingURL=object-path.js.map