@sakuli/commons
Version:
78 lines • 2.94 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
var maybe_1 = require("../../maybe");
var PropertyDecoratorMetadata = Symbol('PropertyDecoratorMetadata');
var BooleanPropertyReader = function (v) {
return !(["false", "null", "0", "", false, 0, undefined, null].includes(v));
};
var identity = function (x) { return x; };
function Property(path, reader) {
if (reader === void 0) { reader = identity; }
return function (_a, property) {
var constructor = _a.constructor;
var propertiesInClass = getPropertyDecoratorDefinitions(constructor);
Reflect.defineMetadata(PropertyDecoratorMetadata, __spread(propertiesInClass, [
{ path: path, property: property, reader: reader }
]), constructor);
};
}
exports.Property = Property;
var onlyValue = function (reader) { return function (v) { return maybe_1.ifPresent(v, reader, function () { return v; }); }; };
/**
* Converts value to Boolean if present
* @param path
* @constructor
*/
exports.BooleanProperty = function (path) { return Property(path, BooleanPropertyReader); };
/**
* Converts value to Number if present
* @param path
* @constructor
*/
exports.NumberProperty = function (path) { return Property(path, onlyValue(Number)); };
/**
* Converts value to String if present
* @param path
* @constructor
*/
exports.StringProperty = function (path) { return Property(path, onlyValue(String)); };
/**
* Assuming a string string data (all inputs are converted to string).
*
* @param path
* @param delimiter - default: ,
* @param mapper - trims each string value
* @constructor
*/
exports.ListProperty = function (path, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b.delimiter, delimiter = _c === void 0 ? ',' : _c, _d = _b.mapper, mapper = _d === void 0 ? function (s) { return s.trim(); } : _d;
return Property(path, onlyValue(function (v) {
return Array.isArray(v) ? v.map(mapper) : String(v).split(delimiter).map(mapper);
}));
};
function getPropertyDecoratorDefinitions(type) {
return Reflect.getMetadata(PropertyDecoratorMetadata, type) || [];
}
exports.getPropertyDecoratorDefinitions = getPropertyDecoratorDefinitions;
//# sourceMappingURL=properties.decorator.js.map