From 06901c853514213d19efe6a8cbb02570c863aabf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 09:17:02 +0200 Subject: [PATCH 01/14] [TASK] Reduce the number of arguments for the Constructor to 2 The first one represents the element and the second one is an optional options object closes #6 --- Dist/NodeProto.js | 9 ++++++--- Dist/NodeProto.min.js | 2 +- README.md | 9 ++++++--- Src/NodeProto.js | 9 ++++++--- Tests/API/Prop.js | 21 ++++++++++++++------- Tests/ExampleComponent.js | 4 ++-- 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index bf65991..f915ae4 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -199,20 +199,23 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons }; var Component = (function () { - function Component(element, props, propTypes) { + function Component(element, opts) { _classCallCheck(this, Component); + // Fail-Safe mechanism if someone is passing an array or the like as a second argument. + opts = _isObject(opts) ? opts : {}; + if (!element) { logger.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."); } - this._passedProps = props || {}; + this._passedProps = opts.props || {}; this.props = {}; this.states = {}; this.observers = {}; this.el = element || doc.createElement("div"); - this._validateAndSetProps(propTypes); + this._validateAndSetProps(opts.propTypes); } _createClass(Component, [{ diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 42ee48d..e2ed659 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,r){for(var t=0;t-1,o=function(e){return"function"==typeof e},n=function(e){return!isNaN(e)},i=function(e){return"object"==typeof e},s=function(e){return null!==e&&void 0!==e},a={isRequired:function(e,r,t){var o=s(e);return o||u.error('NodeProto Error: The prop "'+r+'" is required and wasn‘t found on: ',t),{result:o,value:e}},isOptional:function(e,r,t){var o=s(e);return o||u.info('NodeProto Info: The prop "'+r+'" is optional and wasn‘t found on: ',t),{result:!0,value:e}},isNumber:{isRequired:function(e,r,t){var o=n(e),i=!0;return a.isRequired.apply(this,arguments),o?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+r+'" is not a number. ',t),i=!1),{result:i,value:e}},isOptional:function(e,r,t){var o=n(e),i=!0;return e&&!o&&(u.error('NodeProto Error: The prop "'+r+'" is not a number. ',t),i=!1),e=Math.abs(e),{result:i,value:n(e)?e:void 0}}},isObject:{isRequired:function(e,r,t){var o=void 0,n=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return o=i(e),o||(u.error('NodeProto Error: The prop "'+r+'" is not an valid JSON object. ',t),n=!1),{result:n,value:e}},isOptional:function(e,r,t){var o=void 0,n=!0,a=s(e);try{e=JSON.parse(e)}catch(c){}return o=i(e),a&&!o&&(u.error('NodeProto Error: The prop "'+r+'" is not an valid JSON object. ',t),n=!1),{result:n,value:e}}}},u={log:function(e){if(!t)try{console.log(e)}catch(r){}},info:function(e){if(!t)try{console.info(e)}catch(r){}},warn:function(e){if(!t)try{console.warn(e)}catch(r){}},error:function(e){if(!t)try{console.error(e)}catch(r){}}},c=function(){function e(t,o,n){_classCallCheck(this,e),t||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=o||{},this.props={},this.states={},this.observers={},this.el=t||r.createElement("div"),this._validateAndSetProps(n)}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var r=this.el,t=this._passedProps,n=o(this.getDefaultProps)?this.getDefaultProps():{};for(var i in e){var s=t[i]||r.getAttribute("data-"+i.toLowerCase())||n[i],a=e[i],u=a(s,i,r);u.result&&this._setProp(i,u.value)}}},{key:"getElement",value:function(){return this.el}},{key:"_setProp",value:function(e,r){this.props[e]=r}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return s(this.props[e])}},{key:"setState",value:function(e,r){this.states[e]=r}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,r){return(this.observers[e]||(this.observers[e]=[])).push(r)}},{key:"trigger",value:function(e,r){var t=void 0,o=void 0;for(t=this.observers[e],o=0;t&&o-1;)t.splice(o,1);this.observers[e]=r?t:[]}},{key:"extend",value:function(e,r){for(var t in r){var n=r[t];o(n)&&(e.__proto__.hasOwnProperty(t)||(e.__proto__[t]=n))}}}]),e}();return{Component:c,propTypes:a}}); \ No newline at end of file +"use strict";function _classCallCheck(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,r){for(var t=0;t-1,o=function(e){return"function"==typeof e},n=function(e){return!isNaN(e)},i=function(e){return"object"==typeof e},s=function(e){return null!==e&&void 0!==e},a={isRequired:function(e,r,t){var o=s(e);return o||u.error('NodeProto Error: The prop "'+r+'" is required and wasn‘t found on: ',t),{result:o,value:e}},isOptional:function(e,r,t){var o=s(e);return o||u.info('NodeProto Info: The prop "'+r+'" is optional and wasn‘t found on: ',t),{result:!0,value:e}},isNumber:{isRequired:function(e,r,t){var o=n(e),i=!0;return a.isRequired.apply(this,arguments),o?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+r+'" is not a number. ',t),i=!1),{result:i,value:e}},isOptional:function(e,r,t){var o=n(e),i=!0;return e&&!o&&(u.error('NodeProto Error: The prop "'+r+'" is not a number. ',t),i=!1),e=Math.abs(e),{result:i,value:n(e)?e:void 0}}},isObject:{isRequired:function(e,r,t){var o=void 0,n=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return o=i(e),o||(u.error('NodeProto Error: The prop "'+r+'" is not an valid JSON object. ',t),n=!1),{result:n,value:e}},isOptional:function(e,r,t){var o=void 0,n=!0,a=s(e);try{e=JSON.parse(e)}catch(c){}return o=i(e),a&&!o&&(u.error('NodeProto Error: The prop "'+r+'" is not an valid JSON object. ',t),n=!1),{result:n,value:e}}}},u={log:function(e){if(!t)try{console.log(e)}catch(r){}},info:function(e){if(!t)try{console.info(e)}catch(r){}},warn:function(e){if(!t)try{console.warn(e)}catch(r){}},error:function(e){if(!t)try{console.error(e)}catch(r){}}},c=function(){function e(t,o){_classCallCheck(this,e),o=i(o)?o:{},t||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=o.props||{},this.props={},this.states={},this.observers={},this.el=t||r.createElement("div"),this._validateAndSetProps(o.propTypes)}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var r=this.el,t=this._passedProps,n=o(this.getDefaultProps)?this.getDefaultProps():{};for(var i in e){var s=t[i]||r.getAttribute("data-"+i.toLowerCase())||n[i],a=e[i],u=a(s,i,r);u.result&&this._setProp(i,u.value)}}},{key:"getElement",value:function(){return this.el}},{key:"_setProp",value:function(e,r){this.props[e]=r}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return s(this.props[e])}},{key:"setState",value:function(e,r){this.states[e]=r}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,r){return(this.observers[e]||(this.observers[e]=[])).push(r)}},{key:"trigger",value:function(e,r){var t=void 0,o=void 0;for(t=this.observers[e],o=0;t&&o-1;)t.splice(o,1);this.observers[e]=r?t:[]}},{key:"extend",value:function(e,r){for(var t in r){var n=r[t];o(n)&&(e.__proto__.hasOwnProperty(t)||(e.__proto__[t]=n))}}}]),e}();return{Component:c,propTypes:a}}); \ No newline at end of file diff --git a/README.md b/README.md index f282bfb..db9aca5 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,10 @@ const myComponentPropTypes = { class MyComponent extends nodeProto.Component { constructor(el, props) { - super(el, props, myComponentPropTypes); + super(el, { + 'props': props, + 'propTypes': myComponentPropTypes + }); this.on('logSomething', this.doSomething.bind(this)); } @@ -63,8 +66,8 @@ import MyComponent from 'MyComponent.js'; // Create a new instance, and pass in optional props. const targetElement = document.querySelectorAll('[data-myComponent]')[0]; const instance = new MyComponent(targetElement, { - 'myProp': 'myString', - 'myPropNumber': 2 + 'myProp': 'myString', + 'myPropNumber': 2 }); instance.trigger('logSomething') // LOG: 'myString' diff --git a/Src/NodeProto.js b/Src/NodeProto.js index cedd2cd..567f563 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -193,18 +193,21 @@ }; class Component { - constructor(element, props, propTypes) { + constructor(element, opts) { + // Fail-Safe mechanism if someone is passing an array or the like as a second argument. + opts = _isObject(opts) ? opts : {}; + if(!element) { logger.warn('NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead.'); } - this._passedProps = props || {}; + this._passedProps = opts.props || {}; this.props = {}; this.states = {}; this.observers = {}; this.el = element || doc.createElement('div'); - this._validateAndSetProps(propTypes); + this._validateAndSetProps(opts.propTypes); } _validateAndSetProps(propTypes) { diff --git a/Tests/API/Prop.js b/Tests/API/Prop.js index 1bffc12..f4fb9f5 100644 --- a/Tests/API/Prop.js +++ b/Tests/API/Prop.js @@ -24,9 +24,12 @@ describe('NodeProto: Prop API', () => { it('should validate and set the passed props when propTypes are given.', () => { let instance = new ExampleComponent(null, { - 'myProp': 2 - }, { - 'myProp': nodeProto.propTypes.isRequired + 'props': { + 'myProp': 2 + }, + 'propTypes': { + 'myProp': nodeProto.propTypes.isRequired + } }); expect(instance.getProp('myProp')).toBe(2); @@ -38,8 +41,10 @@ describe('NodeProto: Prop API', () => { element.setAttribute('data-myprop', 'value'); - instance = new ExampleComponent(element, null, { - 'myProp': nodeProto.propTypes.isRequired + instance = new ExampleComponent(element, { + 'propTypes': { + 'myProp': nodeProto.propTypes.isRequired + } }); expect(instance.getProp('myProp')).toBe('value'); @@ -48,8 +53,10 @@ describe('NodeProto: Prop API', () => { it('should fall back to the getDefaultProps method when propTypes are given but the prop wasn‘t found in either the passed props or the dataset.', () => { let instance; - instance = new ExampleComponent(null, null, { - 'anotherProp': nodeProto.propTypes.isRequired + instance = new ExampleComponent(null, { + 'propTypes': { + 'anotherProp': nodeProto.propTypes.isRequired + } }); expect(instance.getProp('anotherProp')).toBe(2); diff --git a/Tests/ExampleComponent.js b/Tests/ExampleComponent.js index 37ebb53..1c1ead0 100644 --- a/Tests/ExampleComponent.js +++ b/Tests/ExampleComponent.js @@ -2,8 +2,8 @@ var nodeProto = require('./../Src/NodeProto.js'); // An ExampleClass for testing purposes. class ExampleComponent extends nodeProto.Component { - constructor(el, props, propTypes) { - super(el, props, propTypes) + constructor(el, opts) { + super(el, opts); } getDefaultProps() { From 47041e34a3aa47fea63e1716452eeb1f558478a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 10:17:32 +0200 Subject: [PATCH 02/14] [FEATURE] Add support for the getInitialState method #2 --- Dist/NodeProto.js | 19 ++++++++++++++++++- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 15 +++++++++++++++ Tests/API/State.js | 4 ++++ Tests/ExampleComponent.js | 6 ++++++ 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index f915ae4..da85cf4 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -216,6 +216,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons this.el = element || doc.createElement("div"); this._validateAndSetProps(opts.propTypes); + this._setInitialState(); } _createClass(Component, [{ @@ -235,6 +236,17 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } } } + }, { + key: "_setInitialState", + value: function _setInitialState() { + var initialStates = this.getInitialState(); + + for (var stateKey in initialStates) { + var value = initialStates[stateKey]; + + this.setState(stateKey, value); + } + } }, { key: "getElement", value: function getElement() { @@ -258,9 +270,14 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons return _isDefined(this.props[propName]); } }, { - key: "setState", + key: "getInitialState", // State related methods. + value: function getInitialState() { + return {}; + } + }, { + key: "setState", value: function setState(stateName, stateVal) { this.states[stateName] = stateVal; } diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index e2ed659..bb92422 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,r){for(var t=0;t-1,o=function(e){return"function"==typeof e},n=function(e){return!isNaN(e)},i=function(e){return"object"==typeof e},s=function(e){return null!==e&&void 0!==e},a={isRequired:function(e,r,t){var o=s(e);return o||u.error('NodeProto Error: The prop "'+r+'" is required and wasn‘t found on: ',t),{result:o,value:e}},isOptional:function(e,r,t){var o=s(e);return o||u.info('NodeProto Info: The prop "'+r+'" is optional and wasn‘t found on: ',t),{result:!0,value:e}},isNumber:{isRequired:function(e,r,t){var o=n(e),i=!0;return a.isRequired.apply(this,arguments),o?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+r+'" is not a number. ',t),i=!1),{result:i,value:e}},isOptional:function(e,r,t){var o=n(e),i=!0;return e&&!o&&(u.error('NodeProto Error: The prop "'+r+'" is not a number. ',t),i=!1),e=Math.abs(e),{result:i,value:n(e)?e:void 0}}},isObject:{isRequired:function(e,r,t){var o=void 0,n=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return o=i(e),o||(u.error('NodeProto Error: The prop "'+r+'" is not an valid JSON object. ',t),n=!1),{result:n,value:e}},isOptional:function(e,r,t){var o=void 0,n=!0,a=s(e);try{e=JSON.parse(e)}catch(c){}return o=i(e),a&&!o&&(u.error('NodeProto Error: The prop "'+r+'" is not an valid JSON object. ',t),n=!1),{result:n,value:e}}}},u={log:function(e){if(!t)try{console.log(e)}catch(r){}},info:function(e){if(!t)try{console.info(e)}catch(r){}},warn:function(e){if(!t)try{console.warn(e)}catch(r){}},error:function(e){if(!t)try{console.error(e)}catch(r){}}},c=function(){function e(t,o){_classCallCheck(this,e),o=i(o)?o:{},t||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=o.props||{},this.props={},this.states={},this.observers={},this.el=t||r.createElement("div"),this._validateAndSetProps(o.propTypes)}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var r=this.el,t=this._passedProps,n=o(this.getDefaultProps)?this.getDefaultProps():{};for(var i in e){var s=t[i]||r.getAttribute("data-"+i.toLowerCase())||n[i],a=e[i],u=a(s,i,r);u.result&&this._setProp(i,u.value)}}},{key:"getElement",value:function(){return this.el}},{key:"_setProp",value:function(e,r){this.props[e]=r}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return s(this.props[e])}},{key:"setState",value:function(e,r){this.states[e]=r}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,r){return(this.observers[e]||(this.observers[e]=[])).push(r)}},{key:"trigger",value:function(e,r){var t=void 0,o=void 0;for(t=this.observers[e],o=0;t&&o-1;)t.splice(o,1);this.observers[e]=r?t:[]}},{key:"extend",value:function(e,r){for(var t in r){var n=r[t];o(n)&&(e.__proto__.hasOwnProperty(t)||(e.__proto__[t]=n))}}}]),e}();return{Component:c,propTypes:a}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,o=function(e){return"function"==typeof e},n=function(e){return!isNaN(e)},i=function(e){return"object"==typeof e},s=function(e){return null!==e&&void 0!==e},a={isRequired:function(e,t,r){var o=s(e);return o||u.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=s(e);return o||u.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,r){var o=n(e),i=!0;return a.isRequired.apply(this,arguments),o?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',r),i=!1),{result:i,value:e}},isOptional:function(e,t,r){var o=n(e),i=!0;return e&&!o&&(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',r),i=!1),e=Math.abs(e),{result:i,value:n(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var o=void 0,n=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return o=i(e),o||(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),n=!1),{result:n,value:e}},isOptional:function(e,t,r){var o=void 0,n=!0,a=s(e);try{e=JSON.parse(e)}catch(l){}return o=i(e),a&&!o&&(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),n=!1),{result:n,value:e}}}},u={log:function(e){if(!r)try{console.log(e)}catch(t){}},info:function(e){if(!r)try{console.info(e)}catch(t){}},warn:function(e){if(!r)try{console.warn(e)}catch(t){}},error:function(e){if(!r)try{console.error(e)}catch(t){}}},l=function(){function e(r,o){_classCallCheck(this,e),o=i(o)?o:{},r||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=o.props||{},this.props={},this.states={},this.observers={},this.el=r||t.createElement("div"),this._validateAndSetProps(o.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=o(this.getDefaultProps)?this.getDefaultProps():{};for(var i in e){var s=r[i]||t.getAttribute("data-"+i.toLowerCase())||n[i],a=e[i],u=a(s,i,t);u.result&&this._setProp(i,u.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState();for(var t in e){var r=e[t];this.setState(t,r)}}},{key:"getElement",value:function(){return this.el}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return s(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,t){for(var r in t){var n=t[r];o(n)&&(e.__proto__.hasOwnProperty(r)||(e.__proto__[r]=n))}}}]),e}();return{Component:l,propTypes:a}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index 567f563..d136e82 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -208,6 +208,7 @@ this.el = element || doc.createElement('div'); this._validateAndSetProps(opts.propTypes); + this._setInitialState(); } _validateAndSetProps(propTypes) { @@ -226,6 +227,16 @@ } } + _setInitialState() { + const initialStates = this.getInitialState(); + + for (let stateKey in initialStates) { + const value = initialStates[stateKey] + + this.setState(stateKey, value); + } + } + getElement() { return this.el; } @@ -244,6 +255,10 @@ } // State related methods. + getInitialState() { + return {}; + } + setState(stateName, stateVal) { this.states[stateName] = stateVal; } diff --git a/Tests/API/State.js b/Tests/API/State.js index 16f5b75..1c9ca41 100644 --- a/Tests/API/State.js +++ b/Tests/API/State.js @@ -17,6 +17,10 @@ describe('NodeProto: State API', () => { expect(instance.getState('myState')).toBe(1); }); + it('should return the initial state if present.', () => { + expect(instance.getState('anotherState')).toBe(true); + }); + afterEach(() => { instance = null; }); diff --git a/Tests/ExampleComponent.js b/Tests/ExampleComponent.js index 1c1ead0..fcb7f8e 100644 --- a/Tests/ExampleComponent.js +++ b/Tests/ExampleComponent.js @@ -11,6 +11,12 @@ class ExampleComponent extends nodeProto.Component { 'anotherProp': 2 } } + + getInitialState() { + return { + 'anotherState': true + } + } } module.exports = ExampleComponent; \ No newline at end of file From 610141c5ebf86ef623b999864d80782a73be7bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 10:42:26 +0200 Subject: [PATCH 03/14] [TASK] Move the ExampleComponent into it's own folder --- Tests/API/Element.js | 4 ++-- Tests/API/Events.js | 4 ++-- Tests/API/Extend.js | 4 ++-- Tests/API/Prop.js | 14 +++++++------- Tests/API/State.js | 4 ++-- .../Default.js} | 2 +- package.json | 2 +- 7 files changed, 17 insertions(+), 17 deletions(-) rename Tests/{ExampleComponent.js => ExampleComponents/Default.js} (79%) diff --git a/Tests/API/Element.js b/Tests/API/Element.js index 2564730..839ab71 100644 --- a/Tests/API/Element.js +++ b/Tests/API/Element.js @@ -1,8 +1,8 @@ -var ExampleComponent = require('./../ExampleComponent.js'); +var Component = require('./../ExampleComponents/Default.js'); describe('NodeProto: Element API', () => { it('should return a DOM element even if none was passed directly to the Constructor.', () => { - var componentInstance = new ExampleComponent(); + var componentInstance = new Component(); expect(componentInstance.getElement()).toBeDefined(); }); diff --git a/Tests/API/Events.js b/Tests/API/Events.js index 94c50ca..1852d43 100644 --- a/Tests/API/Events.js +++ b/Tests/API/Events.js @@ -1,11 +1,11 @@ -var ExampleComponent = require('./../ExampleComponent.js'); +var Component = require('./../ExampleComponents/Default.js'); describe('NodeProto: Events API', () => { let instance; let eventCallback; beforeEach(() => { - instance = new ExampleComponent(); + instance = new Component(); eventCallback = jest.genMockFunction(); }); diff --git a/Tests/API/Extend.js b/Tests/API/Extend.js index 434e743..701bf98 100644 --- a/Tests/API/Extend.js +++ b/Tests/API/Extend.js @@ -1,8 +1,8 @@ -var ExampleComponent = require('./../ExampleComponent.js'); +var Component = require('./../ExampleComponents/Default.js'); describe('NodeProto: Extend API', () => { it('should extend the instances prototype when given a mixin.', () => { - let instance = new ExampleComponent(); + let instance = new Component(); const myFunction = jest.genMockFunction(); instance.extend(instance, { diff --git a/Tests/API/Prop.js b/Tests/API/Prop.js index f4fb9f5..dd04464 100644 --- a/Tests/API/Prop.js +++ b/Tests/API/Prop.js @@ -1,21 +1,21 @@ var nodeProto = require('./../../Src/NodeProto.js'); -var ExampleComponent = require('./../ExampleComponent.js'); +var Component = require('./../ExampleComponents/Default.js'); describe('NodeProto: Prop API', () => { it('should return undefined if no prop was set.', () => { - let instance = new ExampleComponent(); + let instance = new Component(); expect(instance.getProp('myProp')).toBeUndefined(); }); it('should check if a prop is present.', () => { - let instance = new ExampleComponent(); + let instance = new Component(); expect(instance.hasProp('myProp')).toBeFalsy(); }); it('should return the value of a state which was previously set.', () => { - let instance = new ExampleComponent(); + let instance = new Component(); instance.setState('myState', 1); @@ -23,7 +23,7 @@ describe('NodeProto: Prop API', () => { }); it('should validate and set the passed props when propTypes are given.', () => { - let instance = new ExampleComponent(null, { + let instance = new Component(null, { 'props': { 'myProp': 2 }, @@ -41,7 +41,7 @@ describe('NodeProto: Prop API', () => { element.setAttribute('data-myprop', 'value'); - instance = new ExampleComponent(element, { + instance = new Component(element, { 'propTypes': { 'myProp': nodeProto.propTypes.isRequired } @@ -53,7 +53,7 @@ describe('NodeProto: Prop API', () => { it('should fall back to the getDefaultProps method when propTypes are given but the prop wasn‘t found in either the passed props or the dataset.', () => { let instance; - instance = new ExampleComponent(null, { + instance = new Component(null, { 'propTypes': { 'anotherProp': nodeProto.propTypes.isRequired } diff --git a/Tests/API/State.js b/Tests/API/State.js index 1c9ca41..37fcd76 100644 --- a/Tests/API/State.js +++ b/Tests/API/State.js @@ -1,10 +1,10 @@ -var ExampleComponent = require('./../ExampleComponent.js'); +var Component = require('./../ExampleComponents/Default.js'); describe('NodeProto: State API', () => { let instance; beforeEach(() => { - instance = new ExampleComponent(); + instance = new Component(); }); it('should return undefined if no state was set.', () => { diff --git a/Tests/ExampleComponent.js b/Tests/ExampleComponents/Default.js similarity index 79% rename from Tests/ExampleComponent.js rename to Tests/ExampleComponents/Default.js index fcb7f8e..f2b9a87 100644 --- a/Tests/ExampleComponent.js +++ b/Tests/ExampleComponents/Default.js @@ -1,4 +1,4 @@ -var nodeProto = require('./../Src/NodeProto.js'); +var nodeProto = require('./../../Src/NodeProto.js'); // An ExampleClass for testing purposes. class ExampleComponent extends nodeProto.Component { diff --git a/package.json b/package.json index b62c978..b869934 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "unmockedModulePathPatterns": [ "./Src", "./Dist", - "./Tests/ExampleComponent.js" + "./Tests/ExampleComponents" ] } } From 8619b6bd02b4ad636963d00187f1c39268d27046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 10:54:30 +0200 Subject: [PATCH 04/14] [TASK] Simplify the fallback checks for the getDefaultProps() and getInitialState() methods #2 --- Dist/NodeProto.js | 13 ++++++++++--- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 10 ++++++++-- Tests/API/Prop.js | 9 ++++++++- Tests/API/State.js | 7 +++++++ Tests/ExampleComponents/WithoutDefault.js | 10 ++++++++++ 6 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 Tests/ExampleComponents/WithoutDefault.js diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index da85cf4..db5f874 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -224,7 +224,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons value: function _validateAndSetProps(propTypes) { var el = this.el; var _passedProps = this._passedProps; - var defaultProps = _isFunction(this.getDefaultProps) ? this.getDefaultProps() : {}; + var _defaultProps = this.getDefaultProps(); + var defaultProps = _isObject(_defaultProps) ? _defaultProps : {}; for (var propName in propTypes) { var propValue = _passedProps[propName] || el.getAttribute("data-" + propName.toLowerCase()) || defaultProps[propName]; @@ -239,7 +240,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons }, { key: "_setInitialState", value: function _setInitialState() { - var initialStates = this.getInitialState(); + var _initialStates = this.getInitialState(); + var initialStates = _isObject(_initialStates) ? _initialStates : {}; for (var stateKey in initialStates) { var value = initialStates[stateKey]; @@ -253,9 +255,14 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons return this.el; } }, { - key: "_setProp", + key: "getDefaultProps", // Prop related methods. + value: function getDefaultProps() { + return {}; + } + }, { + key: "_setProp", value: function _setProp(propName, propVal) { this.props[propName] = propVal; } diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index bb92422..08bebc0 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,o=function(e){return"function"==typeof e},n=function(e){return!isNaN(e)},i=function(e){return"object"==typeof e},s=function(e){return null!==e&&void 0!==e},a={isRequired:function(e,t,r){var o=s(e);return o||u.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=s(e);return o||u.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,r){var o=n(e),i=!0;return a.isRequired.apply(this,arguments),o?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',r),i=!1),{result:i,value:e}},isOptional:function(e,t,r){var o=n(e),i=!0;return e&&!o&&(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',r),i=!1),e=Math.abs(e),{result:i,value:n(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var o=void 0,n=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return o=i(e),o||(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),n=!1),{result:n,value:e}},isOptional:function(e,t,r){var o=void 0,n=!0,a=s(e);try{e=JSON.parse(e)}catch(l){}return o=i(e),a&&!o&&(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),n=!1),{result:n,value:e}}}},u={log:function(e){if(!r)try{console.log(e)}catch(t){}},info:function(e){if(!r)try{console.info(e)}catch(t){}},warn:function(e){if(!r)try{console.warn(e)}catch(t){}},error:function(e){if(!r)try{console.error(e)}catch(t){}}},l=function(){function e(r,o){_classCallCheck(this,e),o=i(o)?o:{},r||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=o.props||{},this.props={},this.states={},this.observers={},this.el=r||t.createElement("div"),this._validateAndSetProps(o.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=o(this.getDefaultProps)?this.getDefaultProps():{};for(var i in e){var s=r[i]||t.getAttribute("data-"+i.toLowerCase())||n[i],a=e[i],u=a(s,i,t);u.result&&this._setProp(i,u.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState();for(var t in e){var r=e[t];this.setState(t,r)}}},{key:"getElement",value:function(){return this.el}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return s(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,t){for(var r in t){var n=t[r];o(n)&&(e.__proto__.hasOwnProperty(r)||(e.__proto__[r]=n))}}}]),e}();return{Component:l,propTypes:a}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,o=function(e){return"function"==typeof e},n=function(e){return!isNaN(e)},i=function(e){return"object"==typeof e},s=function(e){return null!==e&&void 0!==e},a={isRequired:function(e,t,r){var o=s(e);return o||u.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=s(e);return o||u.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,r){var o=n(e),i=!0;return a.isRequired.apply(this,arguments),o?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',r),i=!1),{result:i,value:e}},isOptional:function(e,t,r){var o=n(e),i=!0;return e&&!o&&(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',r),i=!1),e=Math.abs(e),{result:i,value:n(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var o=void 0,n=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return o=i(e),o||(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),n=!1),{result:n,value:e}},isOptional:function(e,t,r){var o=void 0,n=!0,a=s(e);try{e=JSON.parse(e)}catch(l){}return o=i(e),a&&!o&&(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),n=!1),{result:n,value:e}}}},u={log:function(e){if(!r)try{console.log(e)}catch(t){}},info:function(e){if(!r)try{console.info(e)}catch(t){}},warn:function(e){if(!r)try{console.warn(e)}catch(t){}},error:function(e){if(!r)try{console.error(e)}catch(t){}}},l=function(){function e(r,o){_classCallCheck(this,e),o=i(o)?o:{},r||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=o.props||{},this.props={},this.states={},this.observers={},this.el=r||t.createElement("div"),this._validateAndSetProps(o.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,o=this.getDefaultProps(),n=i(o)?o:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||n[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState(),t=i(e)?e:{};for(var r in t){var o=t[r];this.setState(r,o)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return s(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,t){for(var r in t){var n=t[r];o(n)&&(e.__proto__.hasOwnProperty(r)||(e.__proto__[r]=n))}}}]),e}();return{Component:l,propTypes:a}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index d136e82..6409e92 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -214,7 +214,8 @@ _validateAndSetProps(propTypes) { const el = this.el; const _passedProps = this._passedProps; - const defaultProps = _isFunction(this.getDefaultProps) ? this.getDefaultProps() : {}; + const _defaultProps = this.getDefaultProps(); + const defaultProps = _isObject(_defaultProps) ? _defaultProps : {}; for (let propName in propTypes) { const propValue = _passedProps[propName] || el.getAttribute('data-' + propName.toLowerCase()) || defaultProps[propName]; @@ -228,7 +229,8 @@ } _setInitialState() { - const initialStates = this.getInitialState(); + const _initialStates = this.getInitialState(); + const initialStates = _isObject(_initialStates) ? _initialStates : {}; for (let stateKey in initialStates) { const value = initialStates[stateKey] @@ -242,6 +244,10 @@ } // Prop related methods. + getDefaultProps() { + return {}; + } + _setProp(propName, propVal) { this.props[propName] = propVal; } diff --git a/Tests/API/Prop.js b/Tests/API/Prop.js index dd04464..eea09d3 100644 --- a/Tests/API/Prop.js +++ b/Tests/API/Prop.js @@ -1,5 +1,6 @@ var nodeProto = require('./../../Src/NodeProto.js'); var Component = require('./../ExampleComponents/Default.js'); +var ComponentWithoutDefaults = require('./../ExampleComponents/WithoutDefault.js'); describe('NodeProto: Prop API', () => { it('should return undefined if no prop was set.', () => { @@ -50,7 +51,7 @@ describe('NodeProto: Prop API', () => { expect(instance.getProp('myProp')).toBe('value'); }); - it('should fall back to the getDefaultProps method when propTypes are given but the prop wasn‘t found in either the passed props or the dataset.', () => { + it('should fall back to the getDefaultProps() method when propTypes are given but the prop wasn‘t found in either the passed props or the dataset.', () => { let instance; instance = new Component(null, { @@ -61,4 +62,10 @@ describe('NodeProto: Prop API', () => { expect(instance.getProp('anotherProp')).toBe(2); }); + + it('should return an empty object if no getDefaultProps() method was present.', () => { + let instanceWithoutDefaults = new ComponentWithoutDefaults(); + + expect(instanceWithoutDefaults.getDefaultProps()).toEqual({}); + }); }); \ No newline at end of file diff --git a/Tests/API/State.js b/Tests/API/State.js index 37fcd76..0ab7ac2 100644 --- a/Tests/API/State.js +++ b/Tests/API/State.js @@ -1,4 +1,5 @@ var Component = require('./../ExampleComponents/Default.js'); +var ComponentWithoutDefaults = require('./../ExampleComponents/WithoutDefault.js'); describe('NodeProto: State API', () => { let instance; @@ -21,6 +22,12 @@ describe('NodeProto: State API', () => { expect(instance.getState('anotherState')).toBe(true); }); + it('should return an empty object if no getInitialState() method was present.', () => { + let instanceWithoutDefaults = new ComponentWithoutDefaults(); + + expect(instanceWithoutDefaults.getInitialState()).toEqual({}); + }); + afterEach(() => { instance = null; }); diff --git a/Tests/ExampleComponents/WithoutDefault.js b/Tests/ExampleComponents/WithoutDefault.js new file mode 100644 index 0000000..ecb6c06 --- /dev/null +++ b/Tests/ExampleComponents/WithoutDefault.js @@ -0,0 +1,10 @@ +var nodeProto = require('./../../Src/NodeProto.js'); + +// An ExampleClass without defaults +class ComponentWithoutDefaults extends nodeProto.Component { + constructor(el, opts) { + super(el, opts); + } +} + +module.exports = ComponentWithoutDefaults; \ No newline at end of file From 31178a6e1a680632b6d9f511b584a6a437832d19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 10:59:43 +0200 Subject: [PATCH 05/14] [TASK] Simplify the util helper functions and add a function name to each for debugging purposes --- Dist/NodeProto.js | 16 ++++++++-------- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 16 ++++++++-------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index db5f874..815146e 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -29,21 +29,21 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var doc = global.document; var isScriptExecutedByNode = process && process.title && process.title.indexOf("node") > -1; - var _isFunction = function _isFunction(func) { + function _isFunction(func) { return typeof func === "function"; - }; + } - var _isNumeric = function _isNumeric(num) { + function _isNumeric(num) { return !isNaN(num); - }; + } - var _isObject = function _isObject(obj) { + function _isObject(obj) { return typeof obj === "object"; - }; + } - var _isDefined = function _isDefined(val) { + function _isDefined(val) { return val !== null && val !== undefined; - }; + } var propTypes = { isRequired: function isRequired(propValue, propName, el) { diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 08bebc0..6498a03 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,o=function(e){return"function"==typeof e},n=function(e){return!isNaN(e)},i=function(e){return"object"==typeof e},s=function(e){return null!==e&&void 0!==e},a={isRequired:function(e,t,r){var o=s(e);return o||u.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=s(e);return o||u.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,r){var o=n(e),i=!0;return a.isRequired.apply(this,arguments),o?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',r),i=!1),{result:i,value:e}},isOptional:function(e,t,r){var o=n(e),i=!0;return e&&!o&&(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',r),i=!1),e=Math.abs(e),{result:i,value:n(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var o=void 0,n=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return o=i(e),o||(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),n=!1),{result:n,value:e}},isOptional:function(e,t,r){var o=void 0,n=!0,a=s(e);try{e=JSON.parse(e)}catch(l){}return o=i(e),a&&!o&&(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),n=!1),{result:n,value:e}}}},u={log:function(e){if(!r)try{console.log(e)}catch(t){}},info:function(e){if(!r)try{console.info(e)}catch(t){}},warn:function(e){if(!r)try{console.warn(e)}catch(t){}},error:function(e){if(!r)try{console.error(e)}catch(t){}}},l=function(){function e(r,o){_classCallCheck(this,e),o=i(o)?o:{},r||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=o.props||{},this.props={},this.states={},this.observers={},this.el=r||t.createElement("div"),this._validateAndSetProps(o.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,o=this.getDefaultProps(),n=i(o)?o:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||n[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState(),t=i(e)?e:{};for(var r in t){var o=t[r];this.setState(r,o)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return s(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,t){for(var r in t){var n=t[r];o(n)&&(e.__proto__.hasOwnProperty(r)||(e.__proto__[r]=n))}}}]),e}();return{Component:l,propTypes:a}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,a={isRequired:function(e,t,r){var o=n(e);return o||u.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||u.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),i=!0;return a.isRequired.apply(this,arguments),n?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),{result:i,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,i=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return n=o(e),n||(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),i=!1),{result:i,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(l){}return i=o(e),a&&!i&&(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},u={log:function(e){if(!s)try{console.log(e)}catch(t){}},info:function(e){if(!s)try{console.info(e)}catch(t){}},warn:function(e){if(!s)try{console.warn(e)}catch(t){}},error:function(e){if(!s)try{console.error(e)}catch(t){}}},l=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||i.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:l,propTypes:a}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index 6409e92..09855bd 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -23,21 +23,21 @@ const doc = global.document; const isScriptExecutedByNode = process && process.title && process.title.indexOf('node') > -1; - const _isFunction = function(func) { + function _isFunction(func) { return typeof func === 'function'; - }; + } - const _isNumeric = function(num){ + function _isNumeric(num){ return !isNaN(num); - }; + } - const _isObject = function(obj){ + function _isObject(obj){ return typeof obj === 'object'; - }; + } - const _isDefined = function(val) { + function _isDefined(val) { return val !== null && val !== undefined; - }; + } const propTypes = { isRequired: function(propValue, propName, el) { From c5b6e3472e255c94195871ee6f19dbca891e0776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 11:17:21 +0200 Subject: [PATCH 06/14] [TASK] Improve the internal logger with a setLogLevel method for precise controlling over the logs --- Dist/NodeProto.js | 20 ++++++++++++++++---- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 20 ++++++++++++++++---- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index 815146e..b01f4ee 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -160,8 +160,16 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons }; var logger = { + // 2: Every message is displayed + // 1: Only severe messages are displayed + // 0: No messages are displayed + _logLevel: 0, + setLogLevel: function setLogLevel(int) { + logger._logLevel = _isNumeric(int) ? int : 2; + }, + log: function log(message) { - if (isScriptExecutedByNode) { + if (logger._logLevel <= 2) { return; } @@ -170,7 +178,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } catch (e) {} }, info: function info(message) { - if (isScriptExecutedByNode) { + if (logger._logLevel <= 2) { return; } @@ -179,7 +187,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } catch (e) {} }, warn: function warn(message) { - if (isScriptExecutedByNode) { + if (logger._logLevel <= 1) { return; } @@ -188,7 +196,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } catch (e) {} }, error: function error(message) { - if (isScriptExecutedByNode) { + if (logger._logLevel <= 0) { return; } @@ -198,6 +206,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } }; + if (isScriptExecutedByNode) { + logger.setLogLevel(0); + } + var Component = (function () { function Component(element, opts) { _classCallCheck(this, Component); diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 6498a03..4989cf1 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,a={isRequired:function(e,t,r){var o=n(e);return o||u.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||u.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),i=!0;return a.isRequired.apply(this,arguments),n?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),{result:i,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,i=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return n=o(e),n||(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),i=!1),{result:i,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(l){}return i=o(e),a&&!i&&(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},u={log:function(e){if(!s)try{console.log(e)}catch(t){}},info:function(e){if(!s)try{console.info(e)}catch(t){}},warn:function(e){if(!s)try{console.warn(e)}catch(t){}},error:function(e){if(!s)try{console.error(e)}catch(t){}}},l=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||i.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:l,propTypes:a}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,a={isRequired:function(e,t,r){var o=n(e);return o||u.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||u.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),i=!0;return a.isRequired.apply(this,arguments),n?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),{result:i,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,i=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return n=o(e),n||(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),i=!1),{result:i,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(l){}return i=o(e),a&&!i&&(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},u={_logLevel:0,setLogLevel:function(e){u._logLevel=r(e)?e:2},log:function(e){if(!(u._logLevel<=2))try{console.log(e)}catch(t){}},info:function(e){if(!(u._logLevel<=2))try{console.info(e)}catch(t){}},warn:function(e){if(!(u._logLevel<=1))try{console.warn(e)}catch(t){}},error:function(e){if(!(u._logLevel<=0))try{console.error(e)}catch(t){}}};s&&u.setLogLevel(0);var l=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||i.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:l,propTypes:a}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index 09855bd..f386360 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -154,8 +154,16 @@ }; const logger = { + // 2: Every message is displayed + // 1: Only severe messages are displayed + // 0: No messages are displayed + _logLevel: 0, + setLogLevel: function(int) { + logger._logLevel = _isNumeric(int) ? int : 2; + }, + log: (message) => { - if(isScriptExecutedByNode) { + if(logger._logLevel <= 2) { return; } @@ -164,7 +172,7 @@ } catch(e) {} }, info: (message) => { - if(isScriptExecutedByNode) { + if(logger._logLevel <= 2) { return; } @@ -173,7 +181,7 @@ } catch(e) {} }, warn: (message) => { - if(isScriptExecutedByNode) { + if(logger._logLevel <= 1) { return; } @@ -182,7 +190,7 @@ } catch(e) {} }, error: (message) => { - if(isScriptExecutedByNode) { + if(logger._logLevel <= 0) { return; } @@ -192,6 +200,10 @@ } }; + if(isScriptExecutedByNode) { + logger.setLogLevel(0); + } + class Component { constructor(element, opts) { // Fail-Safe mechanism if someone is passing an array or the like as a second argument. From 9b51c02f0225e7c009984cdd3c621f97b2708f65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 11:21:43 +0200 Subject: [PATCH 07/14] [TASK] Transfer all propType and logger functions to ES6 arrow functions for consistency --- Dist/NodeProto.js | 7 +++++-- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 14 +++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index b01f4ee..9fd631b 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -26,6 +26,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons })(window, function (global) { "use strict"; + var _this = this, + _arguments = arguments; + var doc = global.document; var isScriptExecutedByNode = process && process.title && process.title.indexOf("node") > -1; @@ -76,7 +79,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var result = true; // Since The prop is required, check for it's value beforehand. - propTypes.isRequired.apply(this, arguments); + propTypes.isRequired.apply(_this, _arguments); if (!isNumber) { logger.error("NodeProto Error: The prop \"" + propName + "\" is not a number. ", el); @@ -113,7 +116,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var result = true; // Since The prop is required, check for it's value beforehand. - propTypes.isRequired.apply(this, arguments); + propTypes.isRequired.apply(_this, _arguments); // If the passed Property is a string, convert it to a JSON object beforehand. try { diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 4989cf1..14790ab 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,a={isRequired:function(e,t,r){var o=n(e);return o||u.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||u.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),i=!0;return a.isRequired.apply(this,arguments),n?e=Math.abs(e):(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),{result:i,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(u.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,i=!0;a.isRequired.apply(this,arguments);try{e=JSON.parse(e)}catch(s){}return n=o(e),n||(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),i=!1),{result:i,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(l){}return i=o(e),a&&!i&&(u.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},u={_logLevel:0,setLogLevel:function(e){u._logLevel=r(e)?e:2},log:function(e){if(!(u._logLevel<=2))try{console.log(e)}catch(t){}},info:function(e){if(!(u._logLevel<=2))try{console.info(e)}catch(t){}},warn:function(e){if(!(u._logLevel<=1))try{console.warn(e)}catch(t){}},error:function(e){if(!(u._logLevel<=0))try{console.error(e)}catch(t){}}};s&&u.setLogLevel(0);var l=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||u.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||i.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:l,propTypes:a}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var o=n(e);return o||c.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||c.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),a=!0;return l.isRequired.apply(i,s),n?e=Math.abs(e):(c.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),a=!1),{result:a,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(c.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return n=o(e),n||(c.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(u){}return i=o(e),a&&!i&&(c.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:0,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){if(!(c._logLevel<=2))try{console.log(e)}catch(t){}},info:function(e){if(!(c._logLevel<=2))try{console.info(e)}catch(t){}},warn:function(e){if(!(c._logLevel<=1))try{console.warn(e)}catch(t){}},error:function(e){if(!(c._logLevel<=0))try{console.error(e)}catch(t){}}};u&&c.setLogLevel(0);var f=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||c.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:f,propTypes:l}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index f386360..1da3b8c 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -40,7 +40,7 @@ } const propTypes = { - isRequired: function(propValue, propName, el) { + isRequired: (propValue, propName, el) => { const isPropInProps = _isDefined(propValue); if(!isPropInProps) { @@ -52,7 +52,7 @@ value: propValue }; }, - isOptional: function(propValue, propName, el) { + isOptional: (propValue, propName, el) => { const isPropInProps = _isDefined(propValue); if(!isPropInProps) { @@ -65,7 +65,7 @@ }; }, isNumber: { - isRequired: function(propValue, propName, el) { + isRequired: (propValue, propName, el) => { const isNumber = _isNumeric(propValue); let result = true; @@ -84,7 +84,7 @@ value: propValue }; }, - isOptional: function(propValue, propName, el) { + isOptional: (propValue, propName, el) => { const isNumber = _isNumeric(propValue); let result = true; @@ -102,7 +102,7 @@ } }, isObject: { - isRequired: function(propValue, propName, el) { + isRequired: (propValue, propName, el) => { let isObject; let result = true; @@ -127,7 +127,7 @@ value: propValue }; }, - isOptional: function(propValue, propName, el) { + isOptional: (propValue, propName, el) => { let isObject; let result = true; let isPropValueDefined = _isDefined(propValue); @@ -158,7 +158,7 @@ // 1: Only severe messages are displayed // 0: No messages are displayed _logLevel: 0, - setLogLevel: function(int) { + setLogLevel: (int) => { logger._logLevel = _isNumeric(int) ? int : 2; }, From 10c7d6d5e36cb7500f7b60d6a3ac4e53cf40bb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 11:23:43 +0200 Subject: [PATCH 08/14] [DOCS] Add a note regarding the new getInitialState method --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db9aca5..9169ec5 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ Will return a boolean regarding the existence of the prop for the given key. #### instance.getDefaultProps(); Type: `Function` -Should return an object with all defaultProps you want to specify. +Should return an object with all default props you want to set. #### instance.setState(key, val); Type: `Function` @@ -134,6 +134,11 @@ Argument `key`: `String` Will return the given state of the component. +#### instance.getInitialState(); +Type: `Function` + +Should return an object with all initial states you want to set. + #### instance.on(eventName, listener); Type: `Function` Argument `eventName`: `String` From 95438d3f8d757b7ec770835d745945e7263766a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 14:03:20 +0200 Subject: [PATCH 09/14] [TASK] Add a trailing 's' for the _setInitialStates() method --- Dist/NodeProto.js | 6 +++--- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index 9fd631b..d06a092 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -231,7 +231,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons this.el = element || doc.createElement("div"); this._validateAndSetProps(opts.propTypes); - this._setInitialState(); + this._setInitialStates(); } _createClass(Component, [{ @@ -253,8 +253,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } } }, { - key: "_setInitialState", - value: function _setInitialState() { + key: "_setInitialStates", + value: function _setInitialStates() { var _initialStates = this.getInitialState(); var initialStates = _isObject(_initialStates) ? _initialStates : {}; diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 14790ab..274358e 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var o=n(e);return o||c.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||c.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),a=!0;return l.isRequired.apply(i,s),n?e=Math.abs(e):(c.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),a=!1),{result:a,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(c.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return n=o(e),n||(c.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(u){}return i=o(e),a&&!i&&(c.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:0,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){if(!(c._logLevel<=2))try{console.log(e)}catch(t){}},info:function(e){if(!(c._logLevel<=2))try{console.info(e)}catch(t){}},warn:function(e){if(!(c._logLevel<=1))try{console.warn(e)}catch(t){}},error:function(e){if(!(c._logLevel<=0))try{console.error(e)}catch(t){}}};u&&c.setLogLevel(0);var f=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||c.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialState()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialState",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:f,propTypes:l}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var o=n(e);return o||c.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||c.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),a=!0;return l.isRequired.apply(i,s),n?e=Math.abs(e):(c.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),a=!1),{result:a,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(c.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return n=o(e),n||(c.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(u){}return i=o(e),a&&!i&&(c.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:0,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){if(!(c._logLevel<=2))try{console.log(e)}catch(t){}},info:function(e){if(!(c._logLevel<=2))try{console.info(e)}catch(t){}},warn:function(e){if(!(c._logLevel<=1))try{console.warn(e)}catch(t){}},error:function(e){if(!(c._logLevel<=0))try{console.error(e)}catch(t){}}};u&&c.setLogLevel(0);var f=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||c.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:f,propTypes:l}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index 1da3b8c..4caf872 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -220,7 +220,7 @@ this.el = element || doc.createElement('div'); this._validateAndSetProps(opts.propTypes); - this._setInitialState(); + this._setInitialStates(); } _validateAndSetProps(propTypes) { @@ -240,7 +240,7 @@ } } - _setInitialState() { + _setInitialStates() { const _initialStates = this.getInitialState(); const initialStates = _isObject(_initialStates) ? _initialStates : {}; From e9d5e1f46bc85e50820d419842d237eca2deb463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 14:05:34 +0200 Subject: [PATCH 10/14] [TASK] Reduce the string repition in the logger #9 --- Dist/NodeProto.js | 22 +++++++++++----------- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 22 +++++++++++----------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index d06a092..e05b96c 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -53,7 +53,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var isPropInProps = _isDefined(propValue); if (!isPropInProps) { - logger.error("NodeProto Error: The prop \"" + propName + "\" is required and wasn‘t found on: ", el); + logger.error("The prop \"" + propName + "\" is required and wasn‘t found on: ", el); } return { @@ -65,7 +65,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var isPropInProps = _isDefined(propValue); if (!isPropInProps) { - logger.info("NodeProto Info: The prop \"" + propName + "\" is optional and wasn‘t found on: ", el); + logger.info("The prop \"" + propName + "\" is optional and wasn‘t found on: ", el); } return { @@ -82,7 +82,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons propTypes.isRequired.apply(_this, _arguments); if (!isNumber) { - logger.error("NodeProto Error: The prop \"" + propName + "\" is not a number. ", el); + logger.error("The prop \"" + propName + "\" is not a number. ", el); result = false; } else { propValue = Math.abs(propValue); @@ -98,7 +98,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons var result = true; if (propValue && !isNumber) { - logger.error("NodeProto Error: The prop \"" + propName + "\" is not a number. ", el); + logger.error("The prop \"" + propName + "\" is not a number. ", el); result = false; } @@ -127,7 +127,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons isObject = _isObject(propValue); if (!isObject) { - logger.error("NodeProto Error: The prop \"" + propName + "\" is not an valid JSON object. ", el); + logger.error("The prop \"" + propName + "\" is not an valid JSON object. ", el); result = false; } @@ -150,7 +150,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons isObject = _isObject(propValue); if (isPropValueDefined && !isObject) { - logger.error("NodeProto Error: The prop \"" + propName + "\" is not an valid JSON object. ", el); + logger.error("The prop \"" + propName + "\" is not an valid JSON object. ", el); result = false; } @@ -177,7 +177,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } try { - console.log(message); + console.log("NodeProto: " + message); } catch (e) {} }, info: function info(message) { @@ -186,7 +186,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } try { - console.info(message); + console.info("NodeProto Info: " + message); } catch (e) {} }, warn: function warn(message) { @@ -195,7 +195,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } try { - console.warn(message); + console.warn("NodeProto: " + message); } catch (e) {} }, error: function error(message) { @@ -204,7 +204,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } try { - console.error(message); + console.error("NodeProto Error: " + message); } catch (e) {} } }; @@ -221,7 +221,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons opts = _isObject(opts) ? opts : {}; if (!element) { - logger.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."); + logger.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."); } this._passedProps = opts.props || {}; diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 274358e..8f2ace3 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var o=n(e);return o||c.error('NodeProto Error: The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||c.info('NodeProto Info: The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),a=!0;return l.isRequired.apply(i,s),n?e=Math.abs(e):(c.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),a=!1),{result:a,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(c.error('NodeProto Error: The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return n=o(e),n||(c.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(u){}return i=o(e),a&&!i&&(c.error('NodeProto Error: The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:0,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){if(!(c._logLevel<=2))try{console.log(e)}catch(t){}},info:function(e){if(!(c._logLevel<=2))try{console.info(e)}catch(t){}},warn:function(e){if(!(c._logLevel<=1))try{console.warn(e)}catch(t){}},error:function(e){if(!(c._logLevel<=0))try{console.error(e)}catch(t){}}};u&&c.setLogLevel(0);var f=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||c.warn("NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:f,propTypes:l}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var o=n(e);return o||c.error('The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||c.info('The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),a=!0;return l.isRequired.apply(i,s),n?e=Math.abs(e):(c.error('The prop "'+t+'" is not a number. ',o),a=!1),{result:a,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(c.error('The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return n=o(e),n||(c.error('The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(u){}return i=o(e),a&&!i&&(c.error('The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:0,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){if(!(c._logLevel<=2))try{console.log("NodeProto: "+e)}catch(t){}},info:function(e){if(!(c._logLevel<=2))try{console.info("NodeProto Info: "+e)}catch(t){}},warn:function(e){if(!(c._logLevel<=1))try{console.warn("NodeProto: "+e)}catch(t){}},error:function(e){if(!(c._logLevel<=0))try{console.error("NodeProto Error: "+e)}catch(t){}}};u&&c.setLogLevel(0);var f=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||c.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:f,propTypes:l}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index 4caf872..ecb0e04 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -44,7 +44,7 @@ const isPropInProps = _isDefined(propValue); if(!isPropInProps) { - logger.error('NodeProto Error: The prop "' + propName + '" is required and wasn‘t found on: ', el); + logger.error('The prop "' + propName + '" is required and wasn‘t found on: ', el); } return { @@ -56,7 +56,7 @@ const isPropInProps = _isDefined(propValue); if(!isPropInProps) { - logger.info('NodeProto Info: The prop "' + propName + '" is optional and wasn‘t found on: ', el); + logger.info('The prop "' + propName + '" is optional and wasn‘t found on: ', el); } return { @@ -73,7 +73,7 @@ propTypes.isRequired.apply(this, arguments); if(!isNumber) { - logger.error('NodeProto Error: The prop "' + propName + '" is not a number. ', el); + logger.error('The prop "' + propName + '" is not a number. ', el); result = false; } else { propValue = Math.abs(propValue); @@ -89,7 +89,7 @@ let result = true; if(propValue && !isNumber) { - logger.error('NodeProto Error: The prop "' + propName + '" is not a number. ', el); + logger.error('The prop "' + propName + '" is not a number. ', el); result = false; } @@ -118,7 +118,7 @@ isObject = _isObject(propValue); if(!isObject) { - logger.error('NodeProto Error: The prop "' + propName + '" is not an valid JSON object. ', el); + logger.error('The prop "' + propName + '" is not an valid JSON object. ', el); result = false; } @@ -141,7 +141,7 @@ isObject = _isObject(propValue); if(isPropValueDefined && !isObject) { - logger.error('NodeProto Error: The prop "' + propName + '" is not an valid JSON object. ', el); + logger.error('The prop "' + propName + '" is not an valid JSON object. ', el); result = false; } @@ -168,7 +168,7 @@ } try { - console.log(message); + console.log('NodeProto: ' + message); } catch(e) {} }, info: (message) => { @@ -177,7 +177,7 @@ } try { - console.info(message); + console.info('NodeProto Info: ' + message); } catch(e) {} }, warn: (message) => { @@ -186,7 +186,7 @@ } try { - console.warn(message); + console.warn('NodeProto: ' + message); } catch(e) {} }, error: (message) => { @@ -195,7 +195,7 @@ } try { - console.error(message); + console.error('NodeProto Error: ' + message); } catch(e) {} } }; @@ -210,7 +210,7 @@ opts = _isObject(opts) ? opts : {}; if(!element) { - logger.warn('NodeProto: No element was specified while creating a new Class. Creating a virtual DOM Element instead.'); + logger.warn('No element was specified while creating a new Class. Creating a virtual DOM Element instead.'); } this._passedProps = opts.props || {}; From 7faab3c381f283896f3fb20458177adf609bbf41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 14:10:37 +0200 Subject: [PATCH 11/14] [TASK] Fix the warning and log messages in the logger module #9 --- Dist/NodeProto.js | 20 ++++++++++---------- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 20 ++++++++++---------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index e05b96c..3ce2a4f 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -166,45 +166,45 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons // 2: Every message is displayed // 1: Only severe messages are displayed // 0: No messages are displayed - _logLevel: 0, + _logLevel: 2, setLogLevel: function setLogLevel(int) { logger._logLevel = _isNumeric(int) ? int : 2; }, - log: function log(message) { + log: function log(message, targetElement) { if (logger._logLevel <= 2) { return; } try { - console.log("NodeProto: " + message); + console.log("NodeProto: " + message, targetElement); } catch (e) {} }, - info: function info(message) { + info: function info(message, targetElement) { if (logger._logLevel <= 2) { return; } try { - console.info("NodeProto Info: " + message); + console.info("NodeProto Info: " + message, targetElement); } catch (e) {} }, - warn: function warn(message) { + warn: function warn(message, targetElement) { if (logger._logLevel <= 1) { return; } try { - console.warn("NodeProto: " + message); + console.warn("NodeProto Warning: " + message, targetElement); } catch (e) {} }, - error: function error(message) { + error: function error(message, targetElement) { if (logger._logLevel <= 0) { return; } try { - console.error("NodeProto Error: " + message); + console.error("NodeProto Error: " + message, targetElement); } catch (e) {} } }; @@ -220,7 +220,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons // Fail-Safe mechanism if someone is passing an array or the like as a second argument. opts = _isObject(opts) ? opts : {}; - if (!element) { + if (!_isDefined(element)) { logger.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."); } diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 8f2ace3..8424faa 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var o=n(e);return o||c.error('The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||c.info('The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),a=!0;return l.isRequired.apply(i,s),n?e=Math.abs(e):(c.error('The prop "'+t+'" is not a number. ',o),a=!1),{result:a,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(c.error('The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return n=o(e),n||(c.error('The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(u){}return i=o(e),a&&!i&&(c.error('The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:0,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){if(!(c._logLevel<=2))try{console.log("NodeProto: "+e)}catch(t){}},info:function(e){if(!(c._logLevel<=2))try{console.info("NodeProto Info: "+e)}catch(t){}},warn:function(e){if(!(c._logLevel<=1))try{console.warn("NodeProto: "+e)}catch(t){}},error:function(e){if(!(c._logLevel<=0))try{console.error("NodeProto Error: "+e)}catch(t){}}};u&&c.setLogLevel(0);var f=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},t||c.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:f,propTypes:l}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var o=n(e);return o||c.error('The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||c.info('The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),a=!0;return l.isRequired.apply(i,s),n?e=Math.abs(e):(c.error('The prop "'+t+'" is not a number. ',o),a=!1),{result:a,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(c.error('The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return n=o(e),n||(c.error('The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(u){}return i=o(e),a&&!i&&(c.error('The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:2,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e,t){if(!(c._logLevel<=2))try{console.log("NodeProto: "+e,t)}catch(r){}},info:function(e,t){if(!(c._logLevel<=2))try{console.info("NodeProto Info: "+e,t)}catch(r){}},warn:function(e,t){if(!(c._logLevel<=1))try{console.warn("NodeProto Warning: "+e,t)}catch(r){}},error:function(e,t){if(!(c._logLevel<=0))try{console.error("NodeProto Error: "+e,t)}catch(r){}}};u&&c.setLogLevel(0);var f=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},n(t)||c.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:f,propTypes:l}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index ecb0e04..5c23111 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -157,45 +157,45 @@ // 2: Every message is displayed // 1: Only severe messages are displayed // 0: No messages are displayed - _logLevel: 0, + _logLevel: 2, setLogLevel: (int) => { logger._logLevel = _isNumeric(int) ? int : 2; }, - log: (message) => { + log: (message, targetElement) => { if(logger._logLevel <= 2) { return; } try { - console.log('NodeProto: ' + message); + console.log('NodeProto: ' + message, targetElement); } catch(e) {} }, - info: (message) => { + info: (message, targetElement) => { if(logger._logLevel <= 2) { return; } try { - console.info('NodeProto Info: ' + message); + console.info('NodeProto Info: ' + message, targetElement); } catch(e) {} }, - warn: (message) => { + warn: (message, targetElement) => { if(logger._logLevel <= 1) { return; } try { - console.warn('NodeProto: ' + message); + console.warn('NodeProto Warning: ' + message, targetElement); } catch(e) {} }, - error: (message) => { + error: (message, targetElement) => { if(logger._logLevel <= 0) { return; } try { - console.error('NodeProto Error: ' + message); + console.error('NodeProto Error: ' + message, targetElement); } catch(e) {} } }; @@ -209,7 +209,7 @@ // Fail-Safe mechanism if someone is passing an array or the like as a second argument. opts = _isObject(opts) ? opts : {}; - if(!element) { + if(!_isDefined(element)) { logger.warn('No element was specified while creating a new Class. Creating a virtual DOM Element instead.'); } From 41ed4d146ecd135fc88398d19467b555792102f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 14:15:08 +0200 Subject: [PATCH 12/14] [TASK] Enhance the logging of errors/infos when no element was passed. #9 --- Dist/NodeProto.js | 16 ++++++++++++---- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 8 ++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index 3ce2a4f..8326ecb 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -171,7 +171,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons logger._logLevel = _isNumeric(int) ? int : 2; }, - log: function log(message, targetElement) { + log: function log(message) { + var targetElement = arguments[1] === undefined ? "" : arguments[1]; + if (logger._logLevel <= 2) { return; } @@ -180,7 +182,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons console.log("NodeProto: " + message, targetElement); } catch (e) {} }, - info: function info(message, targetElement) { + info: function info(message) { + var targetElement = arguments[1] === undefined ? "" : arguments[1]; + if (logger._logLevel <= 2) { return; } @@ -189,7 +193,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons console.info("NodeProto Info: " + message, targetElement); } catch (e) {} }, - warn: function warn(message, targetElement) { + warn: function warn(message) { + var targetElement = arguments[1] === undefined ? "" : arguments[1]; + if (logger._logLevel <= 1) { return; } @@ -198,7 +204,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons console.warn("NodeProto Warning: " + message, targetElement); } catch (e) {} }, - error: function error(message, targetElement) { + error: function error(message) { + var targetElement = arguments[1] === undefined ? "" : arguments[1]; + if (logger._logLevel <= 0) { return; } diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 8424faa..cf70e21 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var o=n(e);return o||c.error('The prop "'+t+'" is required and wasn‘t found on: ',r),{result:o,value:e}},isOptional:function(e,t,r){var o=n(e);return o||c.info('The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,o){var n=r(e),a=!0;return l.isRequired.apply(i,s),n?e=Math.abs(e):(c.error('The prop "'+t+'" is not a number. ',o),a=!1),{result:a,value:e}},isOptional:function(e,t,o){var n=r(e),i=!0;return e&&!n&&(c.error('The prop "'+t+'" is not a number. ',o),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var n=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return n=o(e),n||(c.error('The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=n(e);try{e=JSON.parse(e)}catch(u){}return i=o(e),a&&!i&&(c.error('The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:2,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e,t){if(!(c._logLevel<=2))try{console.log("NodeProto: "+e,t)}catch(r){}},info:function(e,t){if(!(c._logLevel<=2))try{console.info("NodeProto Info: "+e,t)}catch(r){}},warn:function(e,t){if(!(c._logLevel<=1))try{console.warn("NodeProto Warning: "+e,t)}catch(r){}},error:function(e,t){if(!(c._logLevel<=0))try{console.error("NodeProto Error: "+e,t)}catch(r){}}};u&&c.setLogLevel(0);var f=function(){function e(t,r){_classCallCheck(this,e),r=o(r)?r:{},n(t)||c.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,n=this.getDefaultProps(),i=o(n)?n:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=o(e)?e:{};for(var r in t){var n=t[r];this.setState(r,n)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return n(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,o=void 0;for(r=this.observers[e],o=0;r&&o-1;)r.splice(o,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var o in r){var n=r[o];t(n)&&(e.__proto__.hasOwnProperty(o)||(e.__proto__[o]=n))}}}]),e}();return{Component:f,propTypes:l}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var n=o(e);return n||c.error('The prop "'+t+'" is required and wasn‘t found on: ',r),{result:n,value:e}},isOptional:function(e,t,r){var n=o(e);return n||c.info('The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,n){var o=r(e),a=!0;return l.isRequired.apply(i,s),o?e=Math.abs(e):(c.error('The prop "'+t+'" is not a number. ',n),a=!1),{result:a,value:e}},isOptional:function(e,t,n){var o=r(e),i=!0;return e&&!o&&(c.error('The prop "'+t+'" is not a number. ',n),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var o=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return o=n(e),o||(c.error('The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=o(e);try{e=JSON.parse(e)}catch(u){}return i=n(e),a&&!i&&(c.error('The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:2,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=2))try{console.log("NodeProto: "+e,t)}catch(r){}},info:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=2))try{console.info("NodeProto Info: "+e,t)}catch(r){}},warn:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=1))try{console.warn("NodeProto Warning: "+e,t)}catch(r){}},error:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=0))try{console.error("NodeProto Error: "+e,t)}catch(r){}}};u&&c.setLogLevel(0);var v=function(){function e(t,r){_classCallCheck(this,e),r=n(r)?r:{},o(t)||c.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,o=this.getDefaultProps(),i=n(o)?o:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=n(e)?e:{};for(var r in t){var o=t[r];this.setState(r,o)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return o(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,n=void 0;for(r=this.observers[e],n=0;r&&n-1;)r.splice(n,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var n in r){var o=r[n];t(o)&&(e.__proto__.hasOwnProperty(n)||(e.__proto__[n]=o))}}}]),e}();return{Component:v,propTypes:l}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index 5c23111..d44fa0a 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -162,7 +162,7 @@ logger._logLevel = _isNumeric(int) ? int : 2; }, - log: (message, targetElement) => { + log: (message, targetElement = '') => { if(logger._logLevel <= 2) { return; } @@ -171,7 +171,7 @@ console.log('NodeProto: ' + message, targetElement); } catch(e) {} }, - info: (message, targetElement) => { + info: (message, targetElement = '') => { if(logger._logLevel <= 2) { return; } @@ -180,7 +180,7 @@ console.info('NodeProto Info: ' + message, targetElement); } catch(e) {} }, - warn: (message, targetElement) => { + warn: (message, targetElement = '') => { if(logger._logLevel <= 1) { return; } @@ -189,7 +189,7 @@ console.warn('NodeProto Warning: ' + message, targetElement); } catch(e) {} }, - error: (message, targetElement) => { + error: (message, targetElement = '') => { if(logger._logLevel <= 0) { return; } From 8d79f243eba702cf834c9a485674ce60dfe15e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 14:32:49 +0200 Subject: [PATCH 13/14] [TASK] Adjust the fallback element warning message --- Dist/NodeProto.js | 2 +- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index 8326ecb..dc38941 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -229,7 +229,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons opts = _isObject(opts) ? opts : {}; if (!_isDefined(element)) { - logger.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."); + logger.warn("No element was specified while creating a new instance of a Class. Creating a virtual DOM Element instead."); } this._passedProps = opts.props || {}; diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index cf70e21..3d25d9a 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ /* NodeProto 1.0.2 | @license MIT */ -"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var n=o(e);return n||c.error('The prop "'+t+'" is required and wasn‘t found on: ',r),{result:n,value:e}},isOptional:function(e,t,r){var n=o(e);return n||c.info('The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,n){var o=r(e),a=!0;return l.isRequired.apply(i,s),o?e=Math.abs(e):(c.error('The prop "'+t+'" is not a number. ',n),a=!1),{result:a,value:e}},isOptional:function(e,t,n){var o=r(e),i=!0;return e&&!o&&(c.error('The prop "'+t+'" is not a number. ',n),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var o=void 0,a=!0;l.isRequired.apply(i,s);try{e=JSON.parse(e)}catch(u){}return o=n(e),o||(c.error('The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}},isOptional:function(e,t,r){var i=void 0,s=!0,a=o(e);try{e=JSON.parse(e)}catch(u){}return i=n(e),a&&!i&&(c.error('The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}}}},c={_logLevel:2,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=2))try{console.log("NodeProto: "+e,t)}catch(r){}},info:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=2))try{console.info("NodeProto Info: "+e,t)}catch(r){}},warn:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=1))try{console.warn("NodeProto Warning: "+e,t)}catch(r){}},error:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=0))try{console.error("NodeProto Error: "+e,t)}catch(r){}}};u&&c.setLogLevel(0);var v=function(){function e(t,r){_classCallCheck(this,e),r=n(r)?r:{},o(t)||c.warn("No element was specified while creating a new Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||a.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,o=this.getDefaultProps(),i=n(o)?o:{};for(var s in e){var a=r[s]||t.getAttribute("data-"+s.toLowerCase())||i[s],u=e[s],l=u(a,s,t);l.result&&this._setProp(s,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=n(e)?e:{};for(var r in t){var o=t[r];this.setState(r,o)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return o(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,n=void 0;for(r=this.observers[e],n=0;r&&n-1;)r.splice(n,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var n in r){var o=r[n];t(o)&&(e.__proto__.hasOwnProperty(n)||(e.__proto__[n]=o))}}}]),e}();return{Component:v,propTypes:l}}); \ No newline at end of file +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var n=o(e);return n||c.error('The prop "'+t+'" is required and wasn‘t found on: ',r),{result:n,value:e}},isOptional:function(e,t,r){var n=o(e);return n||c.info('The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,n){var o=r(e),s=!0;return l.isRequired.apply(i,a),o?e=Math.abs(e):(c.error('The prop "'+t+'" is not a number. ',n),s=!1),{result:s,value:e}},isOptional:function(e,t,n){var o=r(e),i=!0;return e&&!o&&(c.error('The prop "'+t+'" is not a number. ',n),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var o=void 0,s=!0;l.isRequired.apply(i,a);try{e=JSON.parse(e)}catch(u){}return o=n(e),o||(c.error('The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}},isOptional:function(e,t,r){var i=void 0,a=!0,s=o(e);try{e=JSON.parse(e)}catch(u){}return i=n(e),s&&!i&&(c.error('The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}}}},c={_logLevel:2,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=2))try{console.log("NodeProto: "+e,t)}catch(r){}},info:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=2))try{console.info("NodeProto Info: "+e,t)}catch(r){}},warn:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=1))try{console.warn("NodeProto Warning: "+e,t)}catch(r){}},error:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=0))try{console.error("NodeProto Error: "+e,t)}catch(r){}}};u&&c.setLogLevel(0);var v=function(){function e(t,r){_classCallCheck(this,e),r=n(r)?r:{},o(t)||c.warn("No element was specified while creating a new instance of a Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||s.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,o=this.getDefaultProps(),i=n(o)?o:{};for(var a in e){var s=r[a]||t.getAttribute("data-"+a.toLowerCase())||i[a],u=e[a],l=u(s,a,t);l.result&&this._setProp(a,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=n(e)?e:{};for(var r in t){var o=t[r];this.setState(r,o)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return o(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,n=void 0;for(r=this.observers[e],n=0;r&&n-1;)r.splice(n,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var n in r){var o=r[n];t(o)&&(e.__proto__.hasOwnProperty(n)||(e.__proto__[n]=o))}}}]),e}();return{Component:v,propTypes:l}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index d44fa0a..506c3d3 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -210,7 +210,7 @@ opts = _isObject(opts) ? opts : {}; if(!_isDefined(element)) { - logger.warn('No element was specified while creating a new Class. Creating a virtual DOM Element instead.'); + logger.warn('No element was specified while creating a new instance of a Class. Creating a virtual DOM Element instead.'); } this._passedProps = opts.props || {}; From 0537a0d5e95dacc9fb5d54b4adf3dbea3b0b7478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyll=20Wei=C3=9F?= Date: Sun, 28 Jun 2015 14:36:30 +0200 Subject: [PATCH 14/14] [TASK] Release v1.0.3 --- CHANGELOG.md | 9 +++++++-- Dist/NodeProto.js | 2 +- Dist/NodeProto.min.js | 2 +- Src/NodeProto.js | 2 +- package.json | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b83bcf0..1c60a40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,14 @@ # Change Log +## 1.0.3 +**Implemented enhancements:** +- Reduced the overall number of arguments for the Constructor. +- Added support for the getInitialState method. + ## 1.0.2 **Fixed issues:** -- Fixed the name reference for the logger warn function in the constructor -- Fixed the `_isDefined` util function and the corresponding `isRequired` validator test +- Fixed the name reference for the logger warn function in the constructor. +- Fixed the `_isDefined` util function and the corresponding `isRequired` validator test. **Implemented enhancements:** - Added unit test specs for quality ensurance. diff --git a/Dist/NodeProto.js b/Dist/NodeProto.js index dc38941..5d56cbb 100644 --- a/Dist/NodeProto.js +++ b/Dist/NodeProto.js @@ -1,4 +1,4 @@ -/* NodeProto 1.0.2 | @license MIT */ +/* NodeProto 1.0.3 | @license MIT */ "use strict"; diff --git a/Dist/NodeProto.min.js b/Dist/NodeProto.min.js index 3d25d9a..008388f 100644 --- a/Dist/NodeProto.min.js +++ b/Dist/NodeProto.min.js @@ -1,2 +1,2 @@ -/* NodeProto 1.0.2 | @license MIT */ +/* NodeProto 1.0.3 | @license MIT */ "use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var r=0;r-1,l={isRequired:function(e,t,r){var n=o(e);return n||c.error('The prop "'+t+'" is required and wasn‘t found on: ',r),{result:n,value:e}},isOptional:function(e,t,r){var n=o(e);return n||c.info('The prop "'+t+'" is optional and wasn‘t found on: ',r),{result:!0,value:e}},isNumber:{isRequired:function(e,t,n){var o=r(e),s=!0;return l.isRequired.apply(i,a),o?e=Math.abs(e):(c.error('The prop "'+t+'" is not a number. ',n),s=!1),{result:s,value:e}},isOptional:function(e,t,n){var o=r(e),i=!0;return e&&!o&&(c.error('The prop "'+t+'" is not a number. ',n),i=!1),e=Math.abs(e),{result:i,value:r(e)?e:void 0}}},isObject:{isRequired:function(e,t,r){var o=void 0,s=!0;l.isRequired.apply(i,a);try{e=JSON.parse(e)}catch(u){}return o=n(e),o||(c.error('The prop "'+t+'" is not an valid JSON object. ',r),s=!1),{result:s,value:e}},isOptional:function(e,t,r){var i=void 0,a=!0,s=o(e);try{e=JSON.parse(e)}catch(u){}return i=n(e),s&&!i&&(c.error('The prop "'+t+'" is not an valid JSON object. ',r),a=!1),{result:a,value:e}}}},c={_logLevel:2,setLogLevel:function(e){c._logLevel=r(e)?e:2},log:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=2))try{console.log("NodeProto: "+e,t)}catch(r){}},info:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=2))try{console.info("NodeProto Info: "+e,t)}catch(r){}},warn:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=1))try{console.warn("NodeProto Warning: "+e,t)}catch(r){}},error:function(e){var t=void 0===arguments[1]?"":arguments[1];if(!(c._logLevel<=0))try{console.error("NodeProto Error: "+e,t)}catch(r){}}};u&&c.setLogLevel(0);var v=function(){function e(t,r){_classCallCheck(this,e),r=n(r)?r:{},o(t)||c.warn("No element was specified while creating a new instance of a Class. Creating a virtual DOM Element instead."),this._passedProps=r.props||{},this.props={},this.states={},this.observers={},this.el=t||s.createElement("div"),this._validateAndSetProps(r.propTypes),this._setInitialStates()}return _createClass(e,[{key:"_validateAndSetProps",value:function(e){var t=this.el,r=this._passedProps,o=this.getDefaultProps(),i=n(o)?o:{};for(var a in e){var s=r[a]||t.getAttribute("data-"+a.toLowerCase())||i[a],u=e[a],l=u(s,a,t);l.result&&this._setProp(a,l.value)}}},{key:"_setInitialStates",value:function(){var e=this.getInitialState(),t=n(e)?e:{};for(var r in t){var o=t[r];this.setState(r,o)}}},{key:"getElement",value:function(){return this.el}},{key:"getDefaultProps",value:function(){return{}}},{key:"_setProp",value:function(e,t){this.props[e]=t}},{key:"getProp",value:function(e){return this.props[e]}},{key:"hasProp",value:function(e){return o(this.props[e])}},{key:"getInitialState",value:function(){return{}}},{key:"setState",value:function(e,t){this.states[e]=t}},{key:"getState",value:function(e){return this.states[e]}},{key:"on",value:function(e,t){return(this.observers[e]||(this.observers[e]=[])).push(t)}},{key:"trigger",value:function(e,t){var r=void 0,n=void 0;for(r=this.observers[e],n=0;r&&n-1;)r.splice(n,1);this.observers[e]=t?r:[]}},{key:"extend",value:function(e,r){for(var n in r){var o=r[n];t(o)&&(e.__proto__.hasOwnProperty(n)||(e.__proto__[n]=o))}}}]),e}();return{Component:v,propTypes:l}}); \ No newline at end of file diff --git a/Src/NodeProto.js b/Src/NodeProto.js index 506c3d3..9eb4d23 100644 --- a/Src/NodeProto.js +++ b/Src/NodeProto.js @@ -1,4 +1,4 @@ -/* NodeProto 1.0.2 | @license MIT */ +/* NodeProto 1.0.3 | @license MIT */ (function(global, factory) { 'use strict'; diff --git a/package.json b/package.json index b869934..73e40ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nodeproto", - "version": "1.0.2", + "version": "1.0.3", "description": "A prototypical class that makes it easy to create Components with nodes, a powerfull instance configuration system and a react-like API.", "main": "Dist/NodeProto.js", "scripts": {