fluxtuate
Version:
a javascript ES7 library for handling complex data transactions
54 lines (38 loc) • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (propertyType) {
var isPrimary = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var JSONPropertyName = arguments[2];
if ((0, _lang.isBoolean)(isPrimary)) {
return applyOnTarget.bind(this, propertyType, isPrimary, JSONPropertyName);
} else {
return applyOnTarget.apply(this, [Object, false, undefined, propertyType, isPrimary, JSONPropertyName]);
}
};
var _internals = require("./_internals");
var _lang = require("lodash/lang");
var _reserved = require("./reserved");
var _reserved2 = _interopRequireDefault(_reserved);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function applyOnTarget(propertyType, isPrimary, JSONPropertyName, target, key, descriptor) {
if (key === undefined) throw new Error("You can't inject values to classes, only to class properties of type function!");
if (_reserved2.default.indexOf(key) !== -1) {
throw new Error(key + " is a reserved word, you can't define a property on a model with this name! Please use a different name on the model but define a alternative property as the third parameter in the property descriptor for parsing JSON data with that name.");
}
if (!JSONPropertyName) {
JSONPropertyName = key;
}
if (typeof descriptor.value === "function") throw new Error("You can only inject values to properties of the class!");
if (isPrimary) {
if (target[_internals.primaryKey]) throw new Error("You are trying to set property " + key + " as a primary key on a model that already has a primary key of value " + target[_internals.primaryKey]);
target[_internals.primaryKey] = key;
}
if (Object.getOwnPropertySymbols(target).indexOf(_internals.properties) === -1) {
target[_internals.properties] = {};
}
target[_internals.properties][JSONPropertyName] = { convert: propertyType, modelKey: key, defaultValue: target[key] };
descriptor.configurable = true;
return descriptor;
}