UNPKG

jsmoo

Version:

JavaScript Minimalist Object Orientation

53 lines (43 loc) 2.06 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } // Merges all the attributes from the role to the base (which can be a Role or a Jsmoo class) // it does not coppy functions defined on the Role and in the Base in favor of the Base and also the internal functions function mergeAttributes(_ref) { var base = _ref.base; var role = _ref.role; var isJsmoo = _ref.isJsmoo; //TODO: See if some one has another work arround for Symbols //Object.getOwnPropertyNames(role).concat(Object.getOwnPropertySymbols(role)).forEach((prop) => { Object.getOwnPropertyNames(role).forEach(function (prop) { if (!prop.match(/^(?:_has_|_attributes_|constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/) && !base[prop]) { Object.defineProperty(base, prop, Object.getOwnPropertyDescriptor(role, prop)); } }); if (isJsmoo && role.prototype._has_) { Object.keys(role.prototype._has_).forEach(function (attr) { base.has(_defineProperty({}, attr, role.prototype._has_[attr])); }); } } // Validates that the Role is infact a Role and if the THIS object is a Jsmoo class function composeRole(role) { if (Object.getPrototypeOf(role).name !== 'Role') throw new TypeError('Only Roles can be composed'); var isJsmoo = Object.getPrototypeOf(this).name === 'Jsmoo'; [{ base: this, role: role, isJsmoo: isJsmoo }, { base: this.prototype, role: role.prototype }].forEach(function (proto) { return mergeAttributes(proto); }); } // The function exported to Jsmoo.js habilitate the Role composition function does() { var _this = this; for (var _len = arguments.length, roles = Array(_len), _key = 0; _key < _len; _key++) { roles[_key] = arguments[_key]; } roles.forEach(function (r) { return composeRole.bind(_this, r)(); }); } exports.default = does;