+12
-20
| let undefined | ||
| const TARGET_PROPERTY_NAME = 'target' | ||
| const storage = new WeakMap | ||
@@ -26,11 +27,6 @@ const key = Symbol() | ||
| const { targetPropertyName } = constructor | ||
| if(targetPropertyName !== undefined | ||
| && init && init.constructor === Object && init.hasOwnProperty(targetPropertyName)) { | ||
| if(init && init.constructor === Object && init.hasOwnProperty(targetPropertyName)) { | ||
| constructor.setTargetOf(this, init[targetPropertyName]) | ||
| } | ||
| else { | ||
| constructor.setTargetOf(this, init && init.constructor === Object? | ||
| constructor.create(init) : | ||
| constructor.create()) | ||
| } | ||
| else constructor.setTargetOf(this, constructor.create()) | ||
| } | ||
@@ -118,6 +114,5 @@ | ||
| * Create a target specified by interface | ||
| * @param {{}} [init] | ||
| * @returns {Object} | ||
| */ | ||
| static create(init) { | ||
| static create() { | ||
| return new this.interface | ||
@@ -127,3 +122,3 @@ } | ||
| /** | ||
| * Get an instance of the target or the instance itself or null if it's not a target | ||
| * Get an instance of the target or the instance itself or new instance for this target if it has no one | ||
| * @param {Assembler|Object|*} object | ||
@@ -135,3 +130,3 @@ * @returns {Assembler|*|null} | ||
| object : | ||
| storage.get(object) || null | ||
| storage.get(object) || new this({ [this.targetPropertyName] : object }) | ||
| } | ||
@@ -156,8 +151,4 @@ | ||
| static setTargetOf(instance, target) { | ||
| const _interface = instance.constructor.interface | ||
| if(target instanceof _interface || target.constructor === _interface) { | ||
| instance[key] = target | ||
| storage.set(target, instance) | ||
| } | ||
| else throw TypeError(`Failed to execute 'setTargetOf' on '${ instance.constructor.name }': target is not of expected type.`) | ||
| instance[key] = target | ||
| storage.set(target, instance) | ||
| } | ||
@@ -174,6 +165,7 @@ | ||
| * This property name may be used to explicitly set target of the instance | ||
| * @returns {undefined|string} | ||
| * @abstract | ||
| * @returns {string} | ||
| */ | ||
| static get targetPropertyName() {} | ||
| static get targetPropertyName() { | ||
| return TARGET_PROPERTY_NAME | ||
| } | ||
@@ -180,0 +172,0 @@ /** |
+1
-1
| { | ||
| "name": "esmodule", | ||
| "version": "0.0.3", | ||
| "version": "0.1.0", | ||
| "description": "ES object assembler library", | ||
@@ -5,0 +5,0 @@ "files": [ |
8765
-4.06%163
-4.68%