Comparing version 1.0.1 to 1.0.2
declare type ClassItem = (new (...args: any[]) => any); | ||
declare module 'mixmix'; | ||
declare function mixmix(...classes: ClassItem[]): ClassItem; | ||
declare module 'mixmix'; |
@@ -1,1 +0,1 @@ | ||
export default mixmix;function mixmix(...e){const t=class{constructor(t){const r=e.map(e=>Object.getOwnPropertyDescriptors(e).name.value);Object.keys(Object(t)).forEach(t=>{r.includes(t)&&Object.defineProperties(this,Object.getOwnPropertyDescriptors(new(e.filter(e=>e.name===t)[0])))})}};return e.forEach(e=>{r(t,e),r(t.prototype,e.prototype)}),t;function r(e,t){const r=Object.getOwnPropertyNames(t),c=Object.getOwnPropertyDescriptors(t),o=Object.getOwnPropertyNames(class{}).concat(["constructor"]),n=e;return r.forEach(e=>{o.includes(e)||Object.defineProperty(n,e,c[e])}),n}} | ||
export default mixmix;var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function mixmix(){for(var o=[],t=0;t<arguments.length;t++)o[t]=arguments[t];function e(t){var n=this,r=o.map(function(t){return Object.getOwnPropertyDescriptors(t).name.value});Object.keys(Object(t)).forEach(function(e){r.includes(e)&&Object.defineProperties(n,Object.getOwnPropertyDescriptors(new(o.filter(function(t){return t.name===e})[0])))})}return o.forEach(function(t){n(e,t),n(e.prototype,t.prototype)}),e;function n(t,e){var n=Object.getOwnPropertyNames(e),r=Object.getOwnPropertyDescriptors(e),o=Object.getOwnPropertyNames(function(){}).concat(["constructor"]),c=t;return n.forEach(function(t){o.includes(t)||Object.defineProperty(c,t,__assign(__assign({},r[t]),{enumerable:0}))}),c}} |
@@ -1,23 +0,56 @@ | ||
typeof window!='undefined'&&(window.mixmix=mixmix);typeof exports!='undefined'&&(module.exports=exports.default=mixmix,exports.__esModule={value:!0});typeof define=='function'&&define(()=>{return mixmix});function mixmix(...classes) { | ||
const processedTargetClass = class MixMixed { | ||
constructor(args) { | ||
const classNames = classes | ||
.map((sourceClass) => Object | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function mixmix() { | ||
var classes = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
classes[_i] = arguments[_i]; | ||
} | ||
var processedTargetClass = (function () { | ||
function MixMixed(args) { | ||
var _this = this; | ||
var classNames = classes | ||
.map(function (sourceClass) { return Object | ||
.getOwnPropertyDescriptors(sourceClass) | ||
.name | ||
.value); | ||
const argsKeys = Object.keys(Object(args)); | ||
argsKeys.forEach((argsKey) => { | ||
.value; }); | ||
var argsKeys = (function () { | ||
var keys = Object.keys(Object(args)); | ||
if (keys.length === 0) { | ||
return classNames; | ||
} | ||
return keys; | ||
})(); | ||
argsKeys.forEach(function (argsKey) { | ||
if (!classNames.includes(argsKey)) { | ||
return; | ||
} | ||
var classPropertyDescriptors = Object | ||
.getOwnPropertyDescriptors(new (classes | ||
.filter(function (sourceClass) { return sourceClass.name === argsKey; })[0])()); | ||
var classPropertyDescriptorsKeys = Object.keys(classPropertyDescriptors); | ||
var classPropertyDescriptorsValues = Object.values(classPropertyDescriptors); | ||
var enumerableClassPropertyDescriptors = (function () { | ||
var workingObject = {}; | ||
classPropertyDescriptorsKeys.forEach(function (classPropertyDescriptorsKey, i) { | ||
workingObject[classPropertyDescriptorsKey] = __assign(__assign({}, classPropertyDescriptorsValues[i]), { enumerable: true }); | ||
}); | ||
return workingObject; | ||
})(); | ||
Object | ||
.defineProperties(this, Object | ||
.getOwnPropertyDescriptors(new (classes | ||
.filter((sourceClass) => sourceClass.name === argsKey)[0])())); | ||
.defineProperties(_this, enumerableClassPropertyDescriptors); | ||
}); | ||
} | ||
}; | ||
return MixMixed; | ||
}()); | ||
classes | ||
.forEach((sourceClassItem) => { | ||
.forEach(function (sourceClassItem) { | ||
defineClassProperties(processedTargetClass, sourceClassItem); | ||
@@ -28,13 +61,16 @@ defineClassProperties(processedTargetClass.prototype, sourceClassItem.prototype); | ||
function defineClassProperties(targetClassItem, sourceClassItem) { | ||
const propertyNames = Object.getOwnPropertyNames(sourceClassItem); | ||
const propertyDescriptors = Object.getOwnPropertyDescriptors(sourceClassItem); | ||
const basePropertyNames = Object.getOwnPropertyNames(class X { | ||
}).concat(['constructor']); | ||
const processedTargetClassItem = targetClassItem; | ||
var propertyNames = Object.getOwnPropertyNames(sourceClassItem); | ||
var propertyDescriptors = Object.getOwnPropertyDescriptors(sourceClassItem); | ||
var basePropertyNames = Object.getOwnPropertyNames((function () { | ||
function X() { | ||
} | ||
return X; | ||
}())).concat(['constructor']); | ||
var processedTargetClassItem = targetClassItem; | ||
propertyNames | ||
.forEach((propertyName) => { | ||
.forEach(function (propertyName) { | ||
if (basePropertyNames.includes(propertyName)) { | ||
return; | ||
} | ||
Object.defineProperty(processedTargetClassItem, propertyName, propertyDescriptors[propertyName]); | ||
Object.defineProperty(processedTargetClassItem, propertyName, __assign(__assign({}, propertyDescriptors[propertyName]), { enumerable: true })); | ||
}); | ||
@@ -41,0 +77,0 @@ return processedTargetClassItem; |
@@ -1,1 +0,1 @@ | ||
typeof window!='undefined'&&(window.mixmix=mixmix);typeof exports!='undefined'&&(module.exports=exports.default=mixmix,exports.__esModule={value:!0});typeof define=='function'&&define(()=>{return mixmix});function mixmix(...e){const t=class{constructor(t){const r=e.map(e=>Object.getOwnPropertyDescriptors(e).name.value);Object.keys(Object(t)).forEach(t=>{r.includes(t)&&Object.defineProperties(this,Object.getOwnPropertyDescriptors(new(e.filter(e=>e.name===t)[0])))})}};return e.forEach(e=>{r(t,e),r(t.prototype,e.prototype)}),t;function r(e,t){const r=Object.getOwnPropertyNames(t),c=Object.getOwnPropertyDescriptors(t),o=Object.getOwnPropertyNames(class{}).concat(["constructor"]),n=e;return r.forEach(e=>{o.includes(e)||Object.defineProperty(n,e,c[e])}),n}} | ||
typeof window!='undefined'&&(window.mixmix=mixmix);typeof exports!='undefined'&&(module.exports=exports.default=mixmix,exports.__esModule={value:!0});typeof define=='function'&&define(()=>{return mixmix});var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function mixmix(){for(var o=[],t=0;t<arguments.length;t++)o[t]=arguments[t];function e(t){var n=this,r=o.map(function(t){return Object.getOwnPropertyDescriptors(t).name.value});Object.keys(Object(t)).forEach(function(e){r.includes(e)&&Object.defineProperties(n,Object.getOwnPropertyDescriptors(new(o.filter(function(t){return t.name===e})[0])))})}return o.forEach(function(t){n(e,t),n(e.prototype,t.prototype)}),e;function n(t,e){var n=Object.getOwnPropertyNames(e),r=Object.getOwnPropertyDescriptors(e),o=Object.getOwnPropertyNames(function(){}).concat(["constructor"]),c=t;return n.forEach(function(t){o.includes(t)||Object.defineProperty(c,t,__assign(__assign({},r[t]),{enumerable:0}))}),c}} |
{ | ||
"name": "mixmix", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "mixin and mingles, for multiple classes", | ||
@@ -5,0 +5,0 @@ "main": "dist/mixmix.js", |
@@ -0,1 +1,2 @@ | ||
# mixmix | ||
@@ -86,14 +87,20 @@ | ||
`mixmix()` will return a new class with a modified constructor. This constructor is sort of a "master" constructor as it is in charge of instantiating all of the combined child constructors. | ||
`mixmix()` will return a new class with a modified "master" constructor that invokes all the child constructors: | ||
```ts | ||
const Sandwich = mixmix(Sand, Witch); | ||
const mSandwich = new Sandwich({Sand: [], Witch: []}) | ||
// ...mixmix() | ||
return class MixMixed { | ||
constructor(args?: Record<string, any>) { | ||
// master constructor | ||
} | ||
} | ||
``` | ||
The name of the class will be a `string` as the key; | ||
Arguments intended to be passed to the invoked constructor will be an `array` in the value: | ||
It will then go one key at a time (sequentially) and invoke the constructor with the name of the key. | ||
As seen above, `mixmix()` will take in an object of type: `Record<string, any[]>`. The name of the class will be the key, while the arguments intended to be passed to the invoked constructor will be an array in the value. It will then go one key at a time (sequentially) and invoke the constructor with the name of the key. | ||
For example, these lines of code: | ||
For example, this line of code: | ||
```js | ||
```ts | ||
const Sandwich = mixmix(Sand, Witch); | ||
const mSandwich = new Sandwich({ | ||
@@ -107,3 +114,3 @@ Sand: [], | ||
```js | ||
```ts | ||
new Sand(); | ||
@@ -129,4 +136,23 @@ new Witch('Son', 'of', 'a', NaN); | ||
will result in `mFoo` having `bar` inside its instance. | ||
will result in: | ||
```ts | ||
console.log(mFoo.bar) | ||
// 69 | ||
``` | ||
> Note: if `undefined` is passed into the constructor, it will instantiate all of the classes according to the order of `Object.getOwnPropertyDescriptors()`: | ||
> | ||
> For example, these lines of code: | ||
> ```ts | ||
> const Sandwich = mixmix(Sand, Witch); | ||
> const mSandwich = new Sandwich(); | ||
> ``` | ||
> will equal to: | ||
> ```ts | ||
> const Sandwich = mixmix(Sand, Witch); | ||
> const mSandwich = new Sandwich({ | ||
> Sand: [], | ||
> Wich: [], | ||
> }); | ||
> ``` | ||
@@ -136,10 +162,10 @@ | ||
A `build.bat` file is provided in the `./src` directory (sorry linux/mac users). | ||
* A `build.bat` file is provided in the `./src` directory (sorry linux/mac users). | ||
* `%1` (the first argument) will determine the output file names | ||
* `header.js` and `header.esm.js` files are used to prefix the final built files. | ||
`header.js` and `header.esm.js` files are used to prefix the final built files. | ||
### Contributing | ||
If you fork this repo and find ways to make improvements (or find one of the probably many bugs), feel free to submit a pull request! | ||
If you find ways to make improvements (or find one of the probably many bugs), feel free to submit a pull request! |
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"target": "es5", | ||
"sourceMap": true, | ||
@@ -5,0 +5,0 @@ "lib": ["es6", "es2017", "ES2015", "ESNext"], |
Sorry, the diff of this file is not supported yet
14833
163
167