+16
| import { Assembler } from './Assembler' | ||
| const setTargetOf = Assembler.setTargetOf | ||
| Assembler.setTargetOf = function(instance, target) { | ||
| setTargetOf.call(this, instance, target) | ||
| Object.defineProperty(target, '__instance__', { value : instance }) | ||
| } | ||
| if(typeof global !== 'undefined') { | ||
| global.Assembler = Assembler | ||
| } | ||
| if(typeof window !== 'undefined') { | ||
| window.Assembler = Assembler | ||
| } |
+76
-24
| let undefined | ||
| const TARGET_PROPERTY_NAME = 'target' | ||
| const DEFAULT_PROPERTY_NAME = 'input' | ||
| const storage = new WeakMap | ||
@@ -13,13 +14,13 @@ const key = Symbol() | ||
| * Create and initialize target by specified initialing object | ||
| * @param {*} [init={}] | ||
| * @param {*} [input={}] | ||
| */ | ||
| constructor(init = {}) { | ||
| if(init === null || init.constructor !== Object) { | ||
| const { defaultPropertyName } = this.constructor | ||
| init = defaultPropertyName === undefined? | ||
| {} : | ||
| { [defaultPropertyName] : init } | ||
| } | ||
| constructor(input = {}) { | ||
| const init = this.constructor.normalize(input) | ||
| Object.defineProperty(this, '__init', { | ||
| writable : true, | ||
| value : init | ||
| }) | ||
| this.create(init) | ||
| this.init(init) | ||
| this.assign(init) | ||
| } | ||
@@ -41,8 +42,16 @@ | ||
| /** | ||
| * @param {{}} init | ||
| */ | ||
| init(init) { | ||
| this.__init = init | ||
| } | ||
| /** | ||
| * Initialize target by specified initializing object | ||
| * @param {{}} init | ||
| */ | ||
| init(init) { | ||
| assign(init) { | ||
| const { defaultPropertyName } = this.constructor | ||
| if(defaultPropertyName !== undefined && init.hasOwnProperty(defaultPropertyName)) { | ||
| this.__init = init | ||
| if(init.hasOwnProperty(defaultPropertyName)) { | ||
| this.setProperty(defaultPropertyName, init[defaultPropertyName]) | ||
@@ -58,2 +67,17 @@ } | ||
| /** | ||
| * @param {string} name | ||
| */ | ||
| defineAccessors(name) { | ||
| Object.defineProperty(this, name, { | ||
| configurable : true, | ||
| set(value) { | ||
| Assembler.getTargetOf(this)[name] = value | ||
| }, | ||
| get() { | ||
| return Assembler.getTargetOf(this)[name] | ||
| } | ||
| }) | ||
| } | ||
| /** | ||
| * Set a single property if it is in this and is not undefined or fallback otherwise | ||
@@ -82,5 +106,6 @@ * @param {string} name | ||
| target[name] = value | ||
| this.defineAccessors(name) | ||
| } | ||
| } | ||
| else this.setPropertyMismatch(name) | ||
| else this.setPropertyMismatch(name, value) | ||
| } | ||
@@ -93,4 +118,3 @@ | ||
| setPropertyFilter(name) { | ||
| const { defaultPropertyName, targetPropertyName } = this.constructor | ||
| return name !== targetPropertyName && name !== defaultPropertyName | ||
| return !this.constructor.setPropertyStopList.includes(name) | ||
| } | ||
@@ -101,5 +125,6 @@ | ||
| * @param {string} name | ||
| * @param {string} value | ||
| */ | ||
| setPropertyMismatch(name) { | ||
| throw Error(`The property '${ name }' is not found on the '${ this.constructor.name }' instance.`) | ||
| setPropertyMismatch(name, value) { | ||
| this[name] = value | ||
| } | ||
@@ -139,2 +164,12 @@ | ||
| /** | ||
| * @param {*} input | ||
| * @returns {{}} | ||
| */ | ||
| static normalize(input) { | ||
| return input === null || input.constructor !== Object? | ||
| { [this.defaultPropertyName] : input } : | ||
| input | ||
| } | ||
| /** | ||
| * Link target and the assembler instance together | ||
@@ -150,14 +185,8 @@ * @param {Assembler|*} instance | ||
| /** | ||
| * This property name is used for the `init.constructor !== Object` case | ||
| * This property name is used for the `input.constructor !== Object` case | ||
| * @returns {undefined|string} | ||
| * @abstract | ||
| */ | ||
| static get defaultPropertyName() {} | ||
| /** | ||
| * This property name may be used to explicitly set target of the instance | ||
| * @returns {string} | ||
| */ | ||
| static get targetPropertyName() { | ||
| return TARGET_PROPERTY_NAME | ||
| static get defaultPropertyName() { | ||
| return DEFAULT_PROPERTY_NAME | ||
| } | ||
@@ -172,2 +201,25 @@ | ||
| } | ||
| /** | ||
| * @returns {string[]} | ||
| */ | ||
| static get setPropertyStopList() { | ||
| return [ | ||
| this.targetPropertyName, | ||
| this.defaultPropertyName | ||
| ] | ||
| } | ||
| /** | ||
| * This property name may be used to explicitly set target of the instance | ||
| * @returns {string} | ||
| */ | ||
| static get targetPropertyName() { | ||
| return TARGET_PROPERTY_NAME | ||
| } | ||
| } | ||
| /** | ||
| * @type {Assembler} | ||
| */ | ||
| Object.defineProperty(Assembler, 'Assembler', { value : Assembler }) |
+7
-4
| { | ||
| "name": "esmodule", | ||
| "version": "0.1.1", | ||
| "version": "0.2.0", | ||
| "description": "ES object assembler library", | ||
@@ -13,2 +13,3 @@ "files": [ | ||
| "scripts": { | ||
| "watch": "webpack -w", | ||
| "api": "rm -rf dist/api && npm run jsdoc && npm run esdoc", | ||
@@ -48,7 +49,9 @@ "coverage": "nyc report --reporter=text-lcov | coveralls", | ||
| "babel-register": "^6.26.0", | ||
| "coveralls": "^3.0.2", | ||
| "coveralls": "^3.0.7", | ||
| "esdoc": "^1.1.0", | ||
| "esdoc-standard-plugin": "^1.0.0", | ||
| "jsdoc": "^3.5.5", | ||
| "nyc": "^12.0.2" | ||
| "jsdoc": "^3.6.3", | ||
| "nyc": "^14.1.1", | ||
| "webpack": "^4.41.2", | ||
| "webpack-cli": "^3.3.10" | ||
| }, | ||
@@ -55,0 +58,0 @@ "ava": { |
+7
-0
@@ -13,2 +13,9 @@ # esmodule | ||
| ## Examples | ||
| - [docs/example.js](https://github.com/aristov/esmodule/blob/master/docs/example.js) | ||
| - [dommodule](https://github.com/aristov/dommodule) | ||
| - [urlmodule](https://github.com/aristov/urlmodule) | ||
| ## Installation | ||
@@ -15,0 +22,0 @@ |
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
10148
18.61%6
20%214
38.06%38
22.58%0
-100%11
22.22%