Comparing version 1.1.0 to 2.0.0
declare type Class = (new (...args: any[]) => any); | ||
export default function mixmix(...classes: Class[]): Class; | ||
export {}; | ||
declare function mixmix(...classes: Class[]): Class; | ||
declare namespace mixmix { | ||
var constructorIndex: any; | ||
var options: { | ||
constructorIndex: number; | ||
isUsingSameParamsIntoConstructors: boolean; | ||
}; | ||
var withConstructorAt: (index?: number, ...classes: Class[]) => Class; | ||
var withSameParamsIntoConstructors: (...classes: Class[]) => Class; | ||
var withOptions: (options: { | ||
constructorIndex: number; | ||
isUsingSameParamsIntoConstructors: boolean; | ||
}, ...classes: Class[]) => Class; | ||
} | ||
export default mixmix; |
@@ -13,12 +13,39 @@ (function (global, factory) { | ||
const BASE_CLASS_PROTOTYPE_PROPERTIES = ['constructor']; | ||
const BASE_OPTIONS = { | ||
constructorIndex: null, | ||
isUsingSameParamsIntoConstructors: null, | ||
}; | ||
mixmix.constructorIndex = null; | ||
// todo: use deep copy if using nested objects | ||
mixmix.options = { ...BASE_OPTIONS }; | ||
mixmix.withConstructorAt = (index = 0, ...classes) => { | ||
mixmix.options.constructorIndex = index; | ||
return mixmix(...classes); | ||
}; | ||
mixmix.withSameParamsIntoConstructors = (...classes) => { | ||
mixmix.options.isUsingSameParamsIntoConstructors = true; | ||
return mixmix(...classes); | ||
}; | ||
// don't use directly, | ||
// use `withX` methods for now until nested options objects are implemented | ||
/** @deprecated */ | ||
mixmix.withOptions = (options, ...classes) => { | ||
mixmix.options = options; | ||
return mixmix(...classes); | ||
}; | ||
function mixmix(...classes) { | ||
let mixedClassClasses = classes; | ||
const { isUsingSameParamsIntoConstructors, constructorIndex, } = mixmix.options; | ||
if (constructorIndex != null) { | ||
mixedClassClasses = [classes[constructorIndex]]; | ||
} | ||
class MixedClass { | ||
constructor(parametersMap = null) { | ||
const allClassNames = classes | ||
constructor(...parametersMap) { | ||
const allClassNames = mixedClassClasses | ||
.map((sourceClass) => sourceClass?.name); | ||
const isIntantiatingAllWithOverrideValue = (Array.isArray(parametersMap) | ||
|| parametersMap == null); | ||
const isIntantiatingAllWithOverrideValue = !!isUsingSameParamsIntoConstructors | ||
|| constructorIndex != null; | ||
const parametersClassNames = isIntantiatingAllWithOverrideValue | ||
? allClassNames | ||
: Object.keys(parametersMap); | ||
: Object.keys(parametersMap[0] ?? {}); | ||
// use 'argsKeys' instead of 'className' to invoke in order of keys in 'args' | ||
@@ -38,9 +65,11 @@ for (let i = 0, l = parametersClassNames.length; i < l; ++i) { | ||
.defineProperties(this, Object | ||
.getOwnPropertyDescriptors(new classes[indexOfParametersClassName](...(isIntantiatingAllWithOverrideValue | ||
.getOwnPropertyDescriptors(new mixedClassClasses[indexOfParametersClassName](...(isIntantiatingAllWithOverrideValue | ||
// will be array since it's override value | ||
? parametersMap | ||
: parametersMap[parametersClassName])))); | ||
? parametersMap ?? [] | ||
: parametersMap[0][parametersClassName] ?? [])))); | ||
} | ||
} | ||
} | ||
// reset options (fake instance things) | ||
mixmix.options = { ...BASE_OPTIONS }; | ||
const mixedClassName = classes | ||
@@ -47,0 +76,0 @@ .map((sourceClass) => sourceClass.name) |
@@ -1,2 +0,2 @@ | ||
!function(t,e){if("function"==typeof define&&define.amd)define(["exports"],e);else if("undefined"!=typeof exports)e(exports);else{var n={exports:{}};e(n.exports),t.unknown=n.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(t){function e(t,o,i){return(e=n()?Reflect.construct:function(t,e,n){var o=[null];o.push.apply(o,e);var i=new(Function.bind.apply(t,o));return n&&r(i,n.prototype),i}).apply(null,arguments)}function n(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function r(t,e){return(r=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function o(t){return function(t){if(Array.isArray(t))return i(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||function(t,e){if(!t)return;if("string"==typeof t)return i(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return i(t,e)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function f(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var u=Object.getOwnPropertyNames(function(){return function t(){f(this,t)}}()).concat("name"),c=["constructor"];function a(t,e,n){for(var r=Object.getOwnPropertyDescriptors(e),o=Object.keys(r),i=0,f=o.length;i<f;++i){var u=o[i];-1===n.indexOf(u)&&Object.defineProperty(t,u,r[u])}}var l=function(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];var i=function t(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;f(this,t);for(var i=n.map((function(t){return null==t?void 0:t.name})),u=Array.isArray(r)||null==r,c=u?i:Object.keys(r),a=0,l=c.length;a<l;++a){var p=c[a],s=u?a:i.indexOf(p);-1!==s&&Object.defineProperties(this,Object.getOwnPropertyDescriptors(e(n[s],o(u?r:r[p]))))}},l=n.map((function(t){return t.name})).join("");Object.defineProperty(i,"name",{value:l});for(var p=0,s=n.length;p<s;++p){var y=n[p];a(i,y,u),a(i.prototype,y.prototype,c)}return i};t.default=l})); | ||
!function(t,r){if("function"==typeof define&&define.amd)define(["exports"],r);else if("undefined"!=typeof exports)r(exports);else{var e={exports:{}};r(e.exports),t.unknown=e.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(t){function r(t,o,i){return(r=e()?Reflect.construct:function(t,r,e){var o=[null];o.push.apply(o,r);var i=new(Function.bind.apply(t,o));return e&&n(i,e.prototype),i}).apply(null,arguments)}function e(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function n(t,r){return(n=Object.setPrototypeOf||function(t,r){return t.__proto__=r,t})(t,r)}function o(t){return function(t){if(Array.isArray(t))return i(t)}(t)||function(t){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(t))return Array.from(t)}(t)||function(t,r){if(!t)return;if("string"==typeof t)return i(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);"Object"===e&&t.constructor&&(e=t.constructor.name);if("Map"===e||"Set"===e)return Array.from(t);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return i(t,r)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function c(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable}))),e.push.apply(e,n)}return e}function u(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{};r%2?c(Object(e),!0).forEach((function(r){a(t,r,e[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):c(Object(e)).forEach((function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))}))}return t}function a(t,r,e){return r in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t}function f(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var s=Object.getOwnPropertyNames(function(){return function t(){f(this,t)}}()).concat("name"),l=["constructor"],p={constructorIndex:null,isUsingSameParamsIntoConstructors:null};function y(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];var i=e,c=y.options,a=c.isUsingSameParamsIntoConstructors,b=c.constructorIndex;null!=b&&(i=[e[b]]);var v=function t(){var e;f(this,t);for(var n=i.map((function(t){return null==t?void 0:t.name})),c=!!a||null!=b,u=arguments.length,s=new Array(u),l=0;l<u;l++)s[l]=arguments[l];for(var p=c?n:Object.keys(null!==(e=s[0])&&void 0!==e?e:{}),y=0,d=p.length;y<d;++y){var v,O=p[y],m=c?y:n.indexOf(O);-1!==m&&Object.defineProperties(this,Object.getOwnPropertyDescriptors(r(i[m],o(c?null!=s?s:[]:null!==(v=s[0][O])&&void 0!==v?v:[]))))}};y.options=u({},p);var O=e.map((function(t){return t.name})).join("");Object.defineProperty(v,"name",{value:O});for(var m=0,g=e.length;m<g;++m){var h=e[m];d(v,h,s),d(v.prototype,h.prototype,l)}return v}function d(t,r,e){for(var n=Object.getOwnPropertyDescriptors(r),o=Object.keys(n),i=0,c=o.length;i<c;++i){var u=o[i];-1===e.indexOf(u)&&Object.defineProperty(t,u,n[u])}}y.constructorIndex=null,y.options=u({},p),y.withConstructorAt=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;y.options.constructorIndex=t;for(var r=arguments.length,e=new Array(r>1?r-1:0),n=1;n<r;n++)e[n-1]=arguments[n];return y.apply(void 0,e)},y.withSameParamsIntoConstructors=function(){return y.options.isUsingSameParamsIntoConstructors=!0,y.apply(void 0,arguments)},y.withOptions=function(t){y.options=t;for(var r=arguments.length,e=new Array(r>1?r-1:0),n=1;n<r;n++)e[n-1]=arguments[n];return y.apply(void 0,e)};var b=y;t.default=b})); | ||
//# sourceMappingURL=mixmix.min.es5.js.map |
@@ -1,2 +0,2 @@ | ||
const e=Object.getOwnPropertyNames(class{}).concat("name"),t=["constructor"];function n(...n){class o{constructor(e=null){const t=n.map((e=>e?.name)),r=Array.isArray(e)||null==e,o=r?t:Object.keys(e);for(let c=0,s=o.length;c<s;++c){const s=o[c],p=r?c:t.indexOf(s);-1!==p&&Object.defineProperties(this,Object.getOwnPropertyDescriptors(new n[p](...r?e:e[s])))}}}const c=n.map((e=>e.name)).join("");Object.defineProperty(o,"name",{value:c});for(let c=0,s=n.length;c<s;++c){const s=n[c];r(o,s,e),r(o.prototype,s.prototype,t)}return o}function r(e,t,n){const r=Object.getOwnPropertyDescriptors(t),o=Object.keys(r);for(let t=0,c=o.length;t<c;++t){const c=o[t];-1===n.indexOf(c)&&Object.defineProperty(e,c,r[c])}}export default n; | ||
const t=Object.getOwnPropertyNames(class{}).concat("name"),o=["constructor"],n={constructorIndex:null,isUsingSameParamsIntoConstructors:null};function e(...r){let c=r;const{isUsingSameParamsIntoConstructors:i,constructorIndex:a}=e.options;null!=a&&(c=[r[a]]);class p{constructor(...t){const o=c.map((t=>t?.name)),n=!!i||null!=a,e=n?o:Object.keys(t[0]??{});for(let s=0,r=e.length;s<r;++s){const r=e[s],i=n?s:o.indexOf(r);-1!==i&&Object.defineProperties(this,Object.getOwnPropertyDescriptors(new c[i](...n?t??[]:t[0][r]??[])))}}}e.options={...n};const l=r.map((t=>t.name)).join("");Object.defineProperty(p,"name",{value:l});for(let n=0,e=r.length;n<e;++n){const e=r[n];s(p,e,t),s(p.prototype,e.prototype,o)}return p}function s(t,o,n){const e=Object.getOwnPropertyDescriptors(o),s=Object.keys(e);for(let o=0,r=s.length;o<r;++o){const r=s[o];-1===n.indexOf(r)&&Object.defineProperty(t,r,e[r])}}e.constructorIndex=null,e.options={...n},e.withConstructorAt=(t=0,...o)=>(e.options.constructorIndex=t,e(...o)),e.withSameParamsIntoConstructors=(...t)=>(e.options.isUsingSameParamsIntoConstructors=!0,e(...t)),e.withOptions=(t,...o)=>(e.options=t,e(...o));export default e; | ||
//# sourceMappingURL=mixmix.min.esm.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).mixmix=t()}(this,(function(){"use strict";const e=Object.getOwnPropertyNames(class{}).concat("name"),t=["constructor"];function n(e,t,n){const o=Object.getOwnPropertyDescriptors(t),r=Object.keys(o);for(let t=0,c=r.length;t<c;++t){const c=r[t];-1===n.indexOf(c)&&Object.defineProperty(e,c,o[c])}}return function(...o){class r{constructor(e=null){const t=o.map((e=>e?.name)),n=Array.isArray(e)||null==e,r=n?t:Object.keys(e);for(let c=0,s=r.length;c<s;++c){const s=r[c],i=n?c:t.indexOf(s);-1!==i&&Object.defineProperties(this,Object.getOwnPropertyDescriptors(new o[i](...n?e:e[s])))}}}const c=o.map((e=>e.name)).join("");Object.defineProperty(r,"name",{value:c});for(let c=0,s=o.length;c<s;++c){const s=o[c];n(r,s,e),n(r.prototype,s.prototype,t)}return r}})); | ||
!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?module.exports=o():"function"==typeof define&&define.amd?define(o):(t="undefined"!=typeof globalThis?globalThis:t||self).mixmix=o()}(this,(function(){"use strict";const t=Object.getOwnPropertyNames(class{}).concat("name"),o=["constructor"],n={constructorIndex:null,isUsingSameParamsIntoConstructors:null};function e(...r){let c=r;const{isUsingSameParamsIntoConstructors:i,constructorIndex:u}=e.options;null!=u&&(c=[r[u]]);class l{constructor(...t){const o=c.map((t=>t?.name)),n=!!i||null!=u,e=n?o:Object.keys(t[0]??{});for(let s=0,r=e.length;s<r;++s){const r=e[s],i=n?s:o.indexOf(r);-1!==i&&Object.defineProperties(this,Object.getOwnPropertyDescriptors(new c[i](...n?t??[]:t[0][r]??[])))}}}e.options={...n};const p=r.map((t=>t.name)).join("");Object.defineProperty(l,"name",{value:p});for(let n=0,e=r.length;n<e;++n){const e=r[n];s(l,e,t),s(l.prototype,e.prototype,o)}return l}function s(t,o,n){const e=Object.getOwnPropertyDescriptors(o),s=Object.keys(e);for(let o=0,r=s.length;o<r;++o){const r=s[o];-1===n.indexOf(r)&&Object.defineProperty(t,r,e[r])}}return e.constructorIndex=null,e.options={...n},e.withConstructorAt=(t=0,...o)=>(e.options.constructorIndex=t,e(...o)),e.withSameParamsIntoConstructors=(...t)=>(e.options.isUsingSameParamsIntoConstructors=!0,e(...t)),e.withOptions=(t,...o)=>(e.options=t,e(...o)),e})); | ||
//# sourceMappingURL=mixmix.min.js.map |
{ | ||
"name": "mixmix", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "mixin and mingles, for multiple classes", | ||
@@ -5,0 +5,0 @@ "main": "dist/mixmix.js", |
@@ -44,3 +44,3 @@ | ||
## Usage | ||
## Quick Start | ||
@@ -108,3 +108,3 @@ #### Initial setup: | ||
class SandWitch { | ||
constructor(parametersMap: Record<ClassNameString, any> | any[] = null) { | ||
constructor(parametersMap: Record<ClassNameString, any[]> | any[] = null) { | ||
// master constructor | ||
@@ -118,3 +118,3 @@ } | ||
```ts | ||
const sandwich = new Sandwich(/* ...parametersMap (see below) */); | ||
const sandwich = new Sandwich(/* parametersMap (see below) */); | ||
``` | ||
@@ -135,18 +135,29 @@ | ||
If you would like to use the constructor of one of the classes passed in, instead of this Frankenstein's monster, you can use `mixmix.withConstructorAt` | ||
```ts | ||
const Sandwich = mixmix.withConstructorAt(0, Sand, Witch) /* (see below) */ | ||
const sandwich = new Sandwich(); | ||
// Only `Sand.constructor` is executed | ||
``` | ||
##### `parametersMap` | ||
`Record<ClassNameString, any> | any[] | null | undefined` | ||
It takes in: | ||
## API | ||
1. Object with the target class's name ("`Sand`" or "`Witch`") as the key, and an array with parameters to pass into its constructor as the value. | ||
2. Array with parameters to pass into all of the constructors. | ||
3. `null` or `undefined` | ||
##### mixmix(...class) | ||
###### `parametersMap`'s Examples: | ||
* `...class`: Class[] | ||
1. `Record<ClassNameString, any[]>` | ||
```ts | ||
const Sandwich = mixmix(Sand, Witch); | ||
const sandwich = new Sandwich(/* parametersMap (see below) */); | ||
``` | ||
###### parametersMap | ||
`Record<ClassNameString, any[]> | any[] | null | undefined` | ||
It takes in an object with the target class's name ("`Sand`" or "`Witch`") as the key, and an array with parameters to pass into its constructor as the value. | ||
```ts | ||
@@ -166,43 +177,37 @@ const Sandwich = mixmix(Sand, Witch); | ||
2. `any[]` | ||
##### mixmix.withConstructorAt(index, ...class) | ||
* `index`: number | ||
* `...class`: Class[] | ||
```ts | ||
const Sandwich = mixmix(Sand, Witch); | ||
const sandwich = new Sandwich(['Sand', 'of', 'a', NaN]); | ||
const Sandwich = mixmix.withConstructorAt(0, Sand, Witch); | ||
const sandwich = new Sandwich(/* `...parameters` into `Sand` */); | ||
/* | ||
Executes: | ||
new Sand('Sand', 'of', 'a', NaN); | ||
new Witch('Sand', 'of', 'a', NaN); | ||
Is Equivalent to: | ||
new Sandwich({ | ||
Sand: ['Sand', 'of', 'a', NaN], | ||
Witch: ['Sand', 'of', 'a', NaN], | ||
}) | ||
new Sand(...parameters); | ||
// loop through all Witch's keys, something like this | ||
Sandwich.prototype[key] = Witch.prototype[key] | ||
*/ | ||
``` | ||
3. `null | undefined` | ||
##### mixmix.withSameParamsIntoConstructors(...class) | ||
* `...class`: Class[] | ||
```ts | ||
const Sandwich = mixmix(Sand, Witch); | ||
const sandwich = new Sandwich(); | ||
const Sandwich = mixmix.withSameParamsIntoConstructors(Sand, Witch); | ||
const sandwich = new Sandwich(/* `...parameters` into `Sand` and `Witch` */); | ||
/* | ||
Executes: | ||
new Sand(); | ||
new Witch(); | ||
Is Equivalent to: | ||
new Sandwich({ | ||
Sand: [], | ||
Witch: [], | ||
}) | ||
new Sand(...parameters); | ||
new Witch(...parameters); | ||
*/ | ||
@@ -213,2 +218,4 @@ ``` | ||
### Building/Testing | ||
@@ -215,0 +222,0 @@ |
@@ -93,4 +93,5 @@ import { getBabelOutputPlugin } from '@rollup/plugin-babel'; | ||
config.pop(); | ||
config.pop(); | ||
} | ||
export default config; |
@@ -87,2 +87,18 @@ const mixmix = require('../dist/mixmix.js'); | ||
.toBe(Sand.name + Witch.name); | ||
}); | ||
test('Override value', () => { | ||
const MixMixed = mixmix.withSameParamsIntoConstructors(Sand, Witch); | ||
const mixMixed = new MixMixed(10, 10); | ||
expect(mixMixed.wand) | ||
.toBe(10); | ||
}) | ||
test('With constructor at index', () => { | ||
const MixMixed = mixmix.withConstructorAt(0, Sand, Witch); | ||
const mixMixed = new MixMixed(); | ||
expect(mixMixed.id) | ||
.toBe('007'); | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
34692
420
224
0