sjs-base-model
Advanced tools
Comparing version 1.5.1 to 1.5.2
@@ -6,2 +6,3 @@ export declare enum ConversionTypeEnum { | ||
String = "string", | ||
JSON = "json", | ||
} |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.SjsBaseModel={})}(this,function(t){"use strict";var e,n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};(e=t.ConversionTypeEnum||(t.ConversionTypeEnum={})).Number="number",e.Float="float",e.Boolean="boolean",e.String="string";var r=function(){function e(){}return e.uniqueId=function(t){void 0===t&&(t=null);var n=++e._idCounter;return null!=t?String(t+n):n},e.deletePropertyFromObject=function(t,n){var r=n instanceof Array?n:[n];return Object.keys(t).forEach(function(n){var o=t[n];!0===r.includes(n)?delete t[n]:o instanceof Array?o.forEach(function(t){return e.deletePropertyFromObject(t,r)}):o instanceof Object&&e.deletePropertyFromObject(o,r)}),t},e.clone=function(t,n){if(void 0===n&&(n=null),null===t||void 0===t||"object"!=typeof t)return t;if(t instanceof Date)return new Date(t.getTime());if(t instanceof RegExp)return new RegExp(t);if(t instanceof Array)return t.map(function(t){return e.clone(t,n)});if(t instanceof Object){var r={};return Object.keys(t).forEach(function(o){var i=null!==n?n(o):o;r[i]=e.clone(t[o],n)}),r}throw new Error("Unable to copy. "+t+" isn't supported.")},e.toBoolean=function(t){var e="string"==typeof t?t.toLowerCase():t;return!(null==e||e<=0||"false"===e||"off"===e)},e.convertDataUsingOptions=function(t,n){Object.keys(n).forEach(function(r){if(!t.hasOwnProperty(r))throw new SyntaxError('Conversion property name "'+r+'" does not match a property name on the model.');var o=t[r],i=n[r];t[r]=e.convertDataToType(o,i)})},e.convertDataToType=function(n,r){switch(r){case t.ConversionTypeEnum.Boolean:return e.toBoolean(n);case t.ConversionTypeEnum.Float:return null===n?n:parseFloat(n);case t.ConversionTypeEnum.Number:return null===n?n:parseInt(n,10);case t.ConversionTypeEnum.String:return null===n?n:String(n);default:return n}},e._idCounter=0,e}(),o=function(t){function e(e){void 0===e&&(e={});var n=t.call(this)||this;return n.sjsOptions={expand:!1},n.sjsOptions.expand=!0===e.expand,n}return function(t,e){function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(e,t),e.prototype.update=function(t,e){var n=this;void 0===t&&(t={}),void 0===e&&(e={});var o=this._isObject(t,!0)?t:{};return Object.keys(this).forEach(function(t){if("sjsId"!==t&&"sjsOptions"!==t){var e=n[t],r=o[t];n[t]=n._getPropertyData(e,r)}}),r.convertDataUsingOptions(this,e),this},e.prototype.toJSON=function(){var t=r.clone(this);return r.deletePropertyFromObject(t,["sjsId","sjsOptions"])},e.prototype.toJSONString=function(){return JSON.stringify(this.toJSON())},e.prototype.fromJSON=function(t){var e=JSON.parse(t);return this.update(e),this},e.prototype.clone=function(){return new this.constructor(this)},e.prototype._getPropertyData=function(t,e){var n=this;if(!0===Array.isArray(t)){var r=t[0],o=this._isBaseModelObject(r),i=this._isBaseModelClass(r);if(null==e&&!0===i)return[];if(null==e)return t;var s=!1===Array.isArray(e)?[e]:e;return!0===i?s.map(function(t){return new r(t,n.sjsOptions)}):!0===o?s.map(function(t){return new r.constructor(t,n.sjsOptions)}):s}return null==e?this._updateData(t,null):this._updateData(t,e)},e.prototype._updateData=function(t,e){var n=this._isBaseModelObject(t),r=this._isBaseModelClass(t),o=this._isObjectWithProperties(e);if(!0===n&&!0===o){var i=t;return i.update(e),i}return!0!==r||!0!==o&&!0!==this.sjsOptions.expand?!0===r?null:null!=e?e:t:new t(o?e:{},this.sjsOptions)},e.prototype._isBaseModelClass=function(t){return"function"==typeof t&&!0===t.IS_BASE_MODEL},e.prototype._isBaseModelObject=function(t){return t instanceof e},e.prototype._isObjectWithProperties=function(t){return this._isObject(t)&&Object.keys(t).length>0},e.prototype._isObject=function(t,e){void 0===e&&(e=!1);var n=Boolean(t)&&!1===Array.isArray(t)&&"object"==typeof t;return!1===n&&!0===e&&console.error("Something is wrong! "+this.getClassName()+' only allows Objects but "'+t+'" was passed in.'),n},e.IS_BASE_MODEL=!0,e}(function(){function t(){this.sjsId=null,this.sjsId=r.uniqueId()}return t.prototype.destroy=function(){var t=this;Object.keys(this).filter(function(t){return"sjsId"!==t}).forEach(function(e){return t[e]=null})},t.prototype.getClassName=function(){return this.constructor.name},t}());t.BaseModel=o,t.Util=r,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(t.SjsBaseModel={})}(this,function(t){"use strict";var n,e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)n.hasOwnProperty(e)&&(t[e]=n[e])};(n=t.ConversionTypeEnum||(t.ConversionTypeEnum={})).Number="number",n.Float="float",n.Boolean="boolean",n.String="string",n.JSON="json";var o=function(){function n(){}return n.uniqueId=function(t){void 0===t&&(t=null);var e=++n._idCounter;return null!=t?String(t+e):e},n.deletePropertyFromObject=function(t,e){var o=e instanceof Array?e:[e];return Object.keys(t).forEach(function(e){var r=t[e];!0===o.includes(e)?delete t[e]:r instanceof Array?r.forEach(function(t){return n.deletePropertyFromObject(t,o)}):r instanceof Object&&n.deletePropertyFromObject(r,o)}),t},n.clone=function(t,e){if(void 0===e&&(e=null),null===t||void 0===t||"object"!=typeof t)return t;if(t instanceof Date)return new Date(t.getTime());if(t instanceof RegExp)return new RegExp(t);if(t instanceof Array)return t.map(function(t){return n.clone(t,e)});if(t instanceof Object){var o={};return Object.keys(t).forEach(function(r){var i=null!==e?e(r):r;o[i]=n.clone(t[r],e)}),o}throw new Error("Unable to copy. "+t+" isn't supported.")},n.toBoolean=function(t){var n="string"==typeof t?t.toLowerCase():t;return!(null==n||n<=0||"false"===n||"off"===n)},n.convertDataUsingConversionOptions=function(t,e){Object.keys(e).forEach(function(o){if(!t.hasOwnProperty(o))throw new SyntaxError('Conversion property name "'+o+'" does not match a property name on the model.');var r=t[o],i=e[o];t[o]=n.convertDataToConversionType(r,i)})},n.convertDataToConversionType=function(e,o){switch(o){case t.ConversionTypeEnum.Boolean:return n.toBoolean(e);case t.ConversionTypeEnum.Float:return null===e?null:parseFloat(e);case t.ConversionTypeEnum.Number:return null===e?null:parseInt(e,10);case t.ConversionTypeEnum.String:return null===e?null:String(e);case t.ConversionTypeEnum.JSON:return null==e?null:JSON.parse(e);default:return e}},n._idCounter=0,n}(),r=function(t){function n(n){void 0===n&&(n={});var e=t.call(this)||this;return e.sjsOptions={expand:!1},e.sjsOptions.expand=!0===n.expand,e}return function(t,n){function o(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(o.prototype=n.prototype,new o)}(n,t),n.prototype.update=function(t,n){var e=this;void 0===t&&(t={}),void 0===n&&(n={});var r=this._isObject(t,!0)?t:{};return Object.keys(this).forEach(function(t){if("sjsId"!==t&&"sjsOptions"!==t){var n=e[t],o=r[t];e[t]=e._getPropertyData(n,o)}}),o.convertDataUsingConversionOptions(this,n),this},n.prototype.toJSON=function(){var t=o.clone(this);return o.deletePropertyFromObject(t,["sjsId","sjsOptions"])},n.prototype.toJSONString=function(){return JSON.stringify(this.toJSON())},n.prototype.fromJSON=function(t){var n=JSON.parse(t);return this.update(n),this},n.prototype.clone=function(){return new this.constructor(this)},n.prototype._getPropertyData=function(t,n){var e=this;if(!0===Array.isArray(t)){var o=t[0],r=this._isBaseModelObject(o),i=this._isBaseModelClass(o);if(null==n&&!0===i)return[];if(null==n)return t;var s=!1===Array.isArray(n)?[n]:n;return!0===i?s.map(function(t){return new o(t,e.sjsOptions)}):!0===r?s.map(function(t){return new o.constructor(t,e.sjsOptions)}):s}return null==n?this._updateData(t,null):this._updateData(t,n)},n.prototype._updateData=function(t,n){var e=this._isBaseModelObject(t),o=this._isBaseModelClass(t),r=this._isObjectWithProperties(n);if(!0===e&&!0===r){var i=t;return i.update(n),i}return!0!==o||!0!==r&&!0!==this.sjsOptions.expand?!0===o?null:null!=n?n:t:new t(r?n:{},this.sjsOptions)},n.prototype._isBaseModelClass=function(t){return"function"==typeof t&&!0===t.IS_BASE_MODEL},n.prototype._isBaseModelObject=function(t){return t instanceof n},n.prototype._isObjectWithProperties=function(t){return this._isObject(t)&&Object.keys(t).length>0},n.prototype._isObject=function(t,n){void 0===n&&(n=!1);var e=Boolean(t)&&!1===Array.isArray(t)&&"object"==typeof t;return!1===e&&!0===n&&console.error("Something is wrong! "+this.getClassName()+' only allows Objects but "'+t+'" was passed in.'),e},n.IS_BASE_MODEL=!0,n}(function(){function t(){this.sjsId=null,this.sjsId=o.uniqueId()}return t.prototype.destroy=function(){var t=this;Object.keys(this).filter(function(t){return"sjsId"!==t}).forEach(function(n){return t[n]=null})},t.prototype.getClassName=function(){return this.constructor.name},t}());t.BaseModel=r,t.Util=o,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=index.js.map |
@@ -9,4 +9,4 @@ import { ConversionTypeEnum } from './ConversionTypeEnum'; | ||
static toBoolean(value: string | number | boolean): boolean; | ||
static convertDataUsingOptions(data: object, conversionOptions: IConversionOption): void; | ||
static convertDataToType(propertyData: string | number | boolean, conversionType: ConversionTypeEnum): string | number | boolean; | ||
static convertDataUsingConversionOptions(data: object, conversionOptions: IConversionOption): void; | ||
static convertDataToConversionType(propertyData: string | number | boolean, conversionType: ConversionTypeEnum): string | number | boolean | object; | ||
} |
{ | ||
"name": "sjs-base-model", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./lib/index.d.ts", |
@@ -0,1 +1,7 @@ | ||
<p align="center"> | ||
<a href="https://npmcharts.com/compare/sjs-base-model?minimal=true"><img src="https://img.shields.io/npm/dm/sjs-base-model.svg" alt="Downloads"></a> | ||
<a href="https://www.npmjs.com/package/sjs-base-model"><img src="https://img.shields.io/npm/v/sjs-base-model.svg" alt="Version"></a> | ||
<a href="https://www.npmjs.com/package/sjs-base-model"><img src="https://img.shields.io/npm/l/sjs-base-model.svg" alt="License"></a> | ||
</p> | ||
# sjs-base-model | ||
@@ -100,3 +106,3 @@ | ||
`BaseModel` has the ability to convert data when passed to the `update` method. For example if a string number was passed in `"2"` and you wanted to be an actual number `2` then you can give it the property name and associate it with the correct `ConversionTypeEnum`. Currently it only supports `number`, `float` and `boolean`. See below for an example: | ||
`BaseModel` has the ability to convert data when passed to the `update` method. For example if a string number was passed in `"2"` and you wanted to be an actual number `2` then you can give it the property name and associate it with the correct `ConversionTypeEnum`. Currently it only supports `number`, `float`, `string` and `boolean`. See below for an example: | ||
@@ -305,2 +311,4 @@ ```javascript | ||
* 2018-12-07 v1.5.2 Add ConversionTypeEnum.JSON to IConvertOption. | ||
* 2018-10-24 v1.5.1 Throw error if conversion property name doesn't match any properties on the model. Add ConversionTypeEnum.String IConvertOption. | ||
@@ -307,0 +315,0 @@ |
@@ -131,3 +131,3 @@ import {BaseObject} from './BaseObject'; | ||
Util.convertDataUsingOptions(this, conversionOptions); | ||
Util.convertDataUsingConversionOptions(this, conversionOptions); | ||
@@ -134,0 +134,0 @@ return this; |
@@ -6,2 +6,3 @@ export enum ConversionTypeEnum { | ||
String = 'string', | ||
JSON = 'json', | ||
} |
@@ -153,3 +153,3 @@ import {ConversionTypeEnum} from './ConversionTypeEnum'; | ||
public static convertDataUsingOptions(data: object, conversionOptions: IConversionOption): void { | ||
public static convertDataUsingConversionOptions(data: object, conversionOptions: IConversionOption): void { | ||
Object | ||
@@ -159,6 +159,6 @@ .keys(conversionOptions) | ||
if (data.hasOwnProperty(conversionPropertyName)) { | ||
const propertyData: number | string = data[conversionPropertyName]; | ||
const propertyData: number | string | boolean = data[conversionPropertyName]; | ||
const conversionType: ConversionTypeEnum = conversionOptions[conversionPropertyName]; | ||
data[conversionPropertyName] = Util.convertDataToType(propertyData, conversionType); | ||
data[conversionPropertyName] = Util.convertDataToConversionType(propertyData, conversionType); | ||
} else { | ||
@@ -170,3 +170,3 @@ throw new SyntaxError(`Conversion property name "${conversionPropertyName}" does not match a property name on the model.`); | ||
public static convertDataToType(propertyData: string | number | boolean, conversionType: ConversionTypeEnum): string | number | boolean { | ||
public static convertDataToConversionType(propertyData: string | number | boolean, conversionType: ConversionTypeEnum): string | number | boolean | object { | ||
switch (conversionType) { | ||
@@ -177,12 +177,16 @@ case ConversionTypeEnum.Boolean: | ||
return (propertyData === null) | ||
? propertyData | ||
? null | ||
: parseFloat(propertyData as string); | ||
case ConversionTypeEnum.Number: | ||
return (propertyData === null) | ||
? propertyData | ||
? null | ||
: parseInt(propertyData as string, 10); | ||
case ConversionTypeEnum.String: | ||
return (propertyData === null) | ||
? propertyData | ||
? null | ||
: String(propertyData); | ||
case ConversionTypeEnum.JSON: | ||
return (propertyData == null) | ||
? null | ||
: JSON.parse(propertyData as string); | ||
default: | ||
@@ -189,0 +193,0 @@ return propertyData; |
Sorry, the diff of this file is not supported yet
67114
663
329