Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dynamic-number

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dynamic-number - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

readme.md

9

example/app.js

@@ -47,3 +47,3 @@ /******/ (function(modules) { // webpackBootstrap

"use strict";var _index=__webpack_require__(1);var _index2=_interopRequireDefault(_index);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}window.dynamicNumberExample=function(inputElement,outputElement,options){var state={viewValue:0,modelValue:0};var dynamicNumber=new _index2.default();dynamicNumber.separator=options.separator||".";dynamicNumber.integer=options.integer=10;dynamicNumber.fraction=options.fraction=10;dynamicNumber.positive=options.positive=true;dynamicNumber.negative=options.negative=true;dynamicNumber.thousand=options.thousand=null;function calculate(dynamicNumber,inputElement,outputElement,state){dynamicNumber.calculate(inputElement.value,state.modelValue,state.viewValue);state.modelValue=dynamicNumber.modelValue;state.viewValue=dynamicNumber.viewValue;inputElement.removeEventListener("input",listener);outputElement.textContent=state.modelValue;inputElement.value=state.viewValue;inputElement.addEventListener("input",listener);}function listener(ev){calculate(dynamicNumber,inputElement,outputElement,state);}dynamicNumber.calculate(inputElement.value,state.modelValue,state.viewValue);state.modelValue=dynamicNumber.modelValue;state.viewValue=dynamicNumber.viewValue;outputElement.textContent=state.modelValue;inputElement.value=state.viewValue;inputElement.addEventListener("input",listener);};
"use strict";var _index=__webpack_require__(1);var _index2=_interopRequireDefault(_index);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}window.dynamicNumberExample=function(inputElement,outputElement,options){var state={view:"",model:0,cursor:0};var dynamicNumber=new _index2.default();dynamicNumber.separator=options.separator||".";dynamicNumber.integer=options.integer=10;dynamicNumber.fraction=options.fraction=10;dynamicNumber.positive=options.positive=true;dynamicNumber.negative=options.negative=true;dynamicNumber.thousand=options.thousand=null;function setDOMValues(){inputElement.removeEventListener("input",listener);outputElement.textContent=state.model;inputElement.value=state.view;inputElement.addEventListener("input",listener);}function calculate(){var res=dynamicNumber.calculateFromView(inputElement.value);if(res){state.view=res.view;state.model=res.model;state.cursor=res.cursor;}setDOMValues();}function listener(){calculate();}calculate();};

@@ -60,5 +60,4 @@ /***/ },

'use strict';Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var DynamicNumber=function(){function DynamicNumber(){_classCallCheck(this,DynamicNumber);this._separator='.';this._integer=10;this._fraction=10;this._positive=true;this._negative=true;this._regexp=this._buildRegexp();this._isThousand=false;this._thousand=null;}_createClass(DynamicNumber,[{key:'clone',value:function clone(){var other=new DynamicNumber();other._separator=this._separator;other._integer=this._integer;other._fraction=this._fraction;other._positive=this._positive;other._negative=this._negative;other._regexp=this._regexp;other._isThousand=this._isThousand;other._thousand=this._thousand;other._cursor=this._cursor;return other;}},{key:'calculateViewFromModel',value:function calculateViewFromModel(){var modelValue=arguments.length>0&&arguments[0]!==undefined?arguments[0]:0;return this._createViewValueFromModel(modelValue);}},{key:'calculate',value:function calculate(){var rawViewValue=arguments.length>0&&arguments[0]!==undefined?arguments[0]:0;var oldModelValue=arguments.length>1&&arguments[1]!==undefined?arguments[1]:0;var oldViewValue=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'0';var cursorPosition=arguments.length>3&&arguments[3]!==undefined?arguments[3]:null;this._rawViewValue=rawViewValue;this._oldModelValue=oldModelValue;this._oldViewValue=oldViewValue;this._newModelValue=0;this._newViewValue='';this._cursor=cursorPosition;var value=String(this._rawViewValue);value=this._removeThousandSeparator(value);value=this._removeLeadingZero(value);if(value===''&&String(this._rawViewValue).charAt(0)==='0'){this._newModelValue=0;this._newViewValue='0';return;}if(value===undefined||value===''){this._newModelValue=0;this._newViewValue='';return;}if(value==='-'){this._newModelValue=0;this._newViewValue='-';return;}//test fails, therefore we use old values
if(this._regexp.test(value)===false){this._newModelValue=this._oldModelValue;this._newViewValue=this._oldViewValue;return;}// view value success 'correct view format' test
else{this._newModelValue=this._createModelValueFromView(value);this._newViewValue=this._createViewValueFromView(value);this._cursor=this._calculateNewCursorPosition();return;}}},{key:'_calculateThousandSeparator',/**
'use strict';Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var DynamicNumber=function(){function DynamicNumber(){_classCallCheck(this,DynamicNumber);this._separator='.';this._integer=10;this._fraction=10;this._positive=true;this._negative=true;this._regexp=this._buildRegexp();this._isThousand=false;this._thousand=null;}_createClass(DynamicNumber,[{key:'clone',value:function clone(){var other=new DynamicNumber();other._separator=this._separator;other._integer=this._integer;other._fraction=this._fraction;other._positive=this._positive;other._negative=this._negative;other._regexp=this._regexp;other._isThousand=this._isThousand;other._thousand=this._thousand;other._cursor=this._cursor;return other;}},{key:'calculateFromModel',value:function calculateFromModel(){var modelValue=arguments.length>0&&arguments[0]!==undefined?arguments[0]:0;return this._createViewValueFromModel(modelValue);}},{key:'calculateFromView',value:function calculateFromView(){var rawViewValue=arguments.length>0&&arguments[0]!==undefined?arguments[0]:0;var cursorPosition=arguments.length>1&&arguments[1]!==undefined?arguments[1]:null;this._rawViewValue=rawViewValue;this._newModelValue=0;this._newViewValue='';this._cursor=cursorPosition;var value=String(this._rawViewValue);value=this._removeThousandSeparator(value);value=this._removeLeadingZero(value);if(value===""&&String(this._rawViewValue).charAt(0)==="0"){return this._createCorrectResponse("0",0,1);}if(value===undefined||value===""){return this._createCorrectResponse("",0,0);}if(value==="-"){return this._createCorrectResponse("-",0,1);}//test fails, therefore we use old values
if(this._regexp.test(value)===false){return false;}else{return this._createCorrectResponse(this._createViewValueFromView(value),this._createModelValueFromView(value),this._calculateNewCursorPosition());}}},{key:'_createCorrectResponse',value:function _createCorrectResponse(view,model,cursor){return{view:view,model:model,cursor:cursor};}},{key:'_calculateThousandSeparator',/**
* private function which calculate thousand separator.

@@ -71,3 +70,3 @@ */value:function _calculateThousandSeparator(){if(this._thousand!==' '){if(this._separator==='.'){this._thousand=',';}else{this._thousand='.';}}}/**

.replace(/^[\.,]/g,"0$&");//change . to 0.
}},{key:'_removeThousandSeparator',value:function _removeThousandSeparator(value){if(this._isThousand){return value.replace(new RegExp('\\'+this._thousand,'g'),'');}else{return value;}}},{key:'_createModelValueFromView',value:function _createModelValueFromView(value){if(this._separator===','){return parseFloat(value.replace(/\./g,"").replace(",","."));}else{return parseFloat(value.replace(/,/g,""));}}},{key:'_createViewValueFromView',value:function _createViewValueFromView(value){if(this._isThousand){value=value.split(this._separator);value[0]=value[0].replace(/\B(?=(\d{3})+(?!\d))/g,this._thousand);return value.join(this._separator);}else{return value;}}},{key:'_createViewValueFromModel',value:function _createViewValueFromModel(modelValue){var value=String(modelValue);if(this._isThousand){value=value.split(".");value[0]=value[0].replace(/\B(?=(\d{3})+(?!\d))/g,this._thousand);return value.join(this._separator);}else{return value.replace(/\./g,this._separator);}}},{key:'separator',set:function set(sep){this._separator=sep==='.'||sep===','?sep:this._separator;this._regexp=this._buildRegexp();this._calculateThousandSeparator();}},{key:'integer',set:function set(part){if(part>=0){var _part=parseInt(part,10);if(isNaN(_part)===false&&isFinite(_part)&&_part>=0){this._integer=_part;}}this._regexp=this._buildRegexp();}},{key:'fraction',set:function set(part){if(part>=0){var _part=parseInt(part,10);if(isNaN(_part)===false&&isFinite(_part)&&_part>=0){this._fraction=_part;}}this._regexp=this._buildRegexp();}},{key:'positive',set:function set(isPositive){if(isPositive===true||isPositive===false){this._positive=isPositive;}this._regexp=this._buildRegexp();}},{key:'negative',set:function set(isNegative){if(isNegative===true||isNegative===false){this._negative=isNegative;}this._regexp=this._buildRegexp();}},{key:'thousand',set:function set(value){this._isThousand=value||value===' ';if(value===' '){this._thousand=' ';}this._calculateThousandSeparator();}},{key:'modelValue',get:function get(){return this._newModelValue;}},{key:'viewValue',get:function get(){return this._newViewValue;}},{key:'cursorPosition',get:function get(){return this._cursor;}}]);return DynamicNumber;}();exports.default=DynamicNumber;
}},{key:'_removeThousandSeparator',value:function _removeThousandSeparator(value){if(this._isThousand){return value.replace(new RegExp('\\'+this._thousand,'g'),'');}else{return value;}}},{key:'_createModelValueFromView',value:function _createModelValueFromView(value){if(this._separator===','){return parseFloat(value.replace(/\./g,"").replace(",","."));}else{return parseFloat(value.replace(/,/g,""));}}},{key:'_createViewValueFromView',value:function _createViewValueFromView(value){if(this._isThousand){value=value.split(this._separator);value[0]=value[0].replace(/\B(?=(\d{3})+(?!\d))/g,this._thousand);return value.join(this._separator);}else{return value;}}},{key:'_createViewValueFromModel',value:function _createViewValueFromModel(modelValue){var value=String(modelValue);if(this._isThousand){value=value.split(".");value[0]=value[0].replace(/\B(?=(\d{3})+(?!\d))/g,this._thousand);return value.join(this._separator);}else{return value.replace(/\./g,this._separator);}}},{key:'separator',set:function set(sep){this._separator=sep==='.'||sep===','?sep:this._separator;this._regexp=this._buildRegexp();this._calculateThousandSeparator();}},{key:'integer',set:function set(part){if(part>=0){var _part=parseInt(part,10);if(isNaN(_part)===false&&isFinite(_part)&&_part>=0){this._integer=_part;}}this._regexp=this._buildRegexp();}},{key:'fraction',set:function set(part){if(part>=0){var _part=parseInt(part,10);if(isNaN(_part)===false&&isFinite(_part)&&_part>=0){this._fraction=_part;}}this._regexp=this._buildRegexp();}},{key:'positive',set:function set(isPositive){if(isPositive===true||isPositive===false){this._positive=isPositive;}this._regexp=this._buildRegexp();}},{key:'negative',set:function set(isNegative){if(isNegative===true||isNegative===false){this._negative=isNegative;}this._regexp=this._buildRegexp();}},{key:'thousand',set:function set(value){this._isThousand=value||value===' ';if(value===' '){this._thousand=' ';}this._calculateThousandSeparator();}},{key:'cursorPosition',get:function get(){return this._cursor;}}]);return DynamicNumber;}();exports.default=DynamicNumber;

@@ -74,0 +73,0 @@ /***/ }

@@ -5,5 +5,7 @@ import DynamicNumber from "../src/index";

var state = {
viewValue: 0,
modelValue: 0
}
view: "",
model: 0,
cursor: 0
};
var dynamicNumber = new DynamicNumber();

@@ -17,25 +19,25 @@ dynamicNumber.separator = options.separator || ".";

function calculate (dynamicNumber, inputElement, outputElement, state) {
dynamicNumber.calculate(inputElement.value, state.modelValue, state.viewValue);
state.modelValue = dynamicNumber.modelValue;
state.viewValue = dynamicNumber.viewValue;
function setDOMValues () {
inputElement.removeEventListener("input", listener);
outputElement.textContent = state.modelValue;
inputElement.value = state.viewValue;
outputElement.textContent = state.model;
inputElement.value = state.view;
inputElement.addEventListener("input", listener);
}
function listener (ev) {
calculate(dynamicNumber, inputElement, outputElement, state);
function calculate () {
var res = dynamicNumber.calculateFromView(inputElement.value);
if(res) {
state.view = res.view;
state.model = res.model;
state.cursor = res.cursor;
}
setDOMValues();
}
dynamicNumber.calculate(inputElement.value, state.modelValue, state.viewValue);
state.modelValue = dynamicNumber.modelValue;
state.viewValue = dynamicNumber.viewValue;
outputElement.textContent = state.modelValue;
inputElement.value = state.viewValue;
inputElement.addEventListener("input", listener);
function listener () {
calculate();
}
calculate();
};
{
"name": "dynamic-number",
"version": "0.1.0",
"version": "0.2.0",
"description": "Highly customizable module for numbers",

@@ -5,0 +5,0 @@ "keywords": [

@@ -1,1 +0,1 @@

!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var i=t();for(var r in i)("object"==typeof exports?exports:e)[r]=i[r]}}(this,function(){return function(e){function t(r){if(i[r])return i[r].exports;var a=i[r]={exports:{},id:r,loaded:!1};return e[r].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}var i={};return t.m=e,t.c=i,t.p="",t(0)}([function(e,t,i){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0});var a=i(1),s=r(a);t.default=s.default},function(e,t){"use strict";function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,i,r){return i&&e(t.prototype,i),r&&e(t,r),t}}(),a=function(){function e(){i(this,e),this._separator=".",this._integer=10,this._fraction=10,this._positive=!0,this._negative=!0,this._regexp=this._buildRegexp(),this._isThousand=!1,this._thousand=null}return r(e,[{key:"clone",value:function(){var t=new e;return t._separator=this._separator,t._integer=this._integer,t._fraction=this._fraction,t._positive=this._positive,t._negative=this._negative,t._regexp=this._regexp,t._isThousand=this._isThousand,t._thousand=this._thousand,t._cursor=this._cursor,t}},{key:"calculateViewFromModel",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return this._createViewValueFromModel(e)}},{key:"calculate",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"0",r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null;this._rawViewValue=e,this._oldModelValue=t,this._oldViewValue=i,this._newModelValue=0,this._newViewValue="",this._cursor=r;var a=String(this._rawViewValue);return a=this._removeThousandSeparator(a),a=this._removeLeadingZero(a),""===a&&"0"===String(this._rawViewValue).charAt(0)?(this._newModelValue=0,void(this._newViewValue="0")):void 0===a||""===a?(this._newModelValue=0,void(this._newViewValue="")):"-"===a?(this._newModelValue=0,void(this._newViewValue="-")):this._regexp.test(a)===!1?(this._newModelValue=this._oldModelValue,void(this._newViewValue=this._oldViewValue)):(this._newModelValue=this._createModelValueFromView(a),this._newViewValue=this._createViewValueFromView(a),void(this._cursor=this._calculateNewCursorPosition()))}},{key:"_calculateThousandSeparator",value:function(){" "!==this._thousand&&("."===this._separator?this._thousand=",":this._thousand=".")}},{key:"_calculateNewCursorPosition",value:function(){var e=String(this._rawViewValue).slice(0,this._cursor);e=this._removeThousandSeparator(e),e=this._removeLeadingZero(e);var t=e.length;if(this._isThousand){var i=0,r=0,a=void 0,s=this._newViewValue.length;for(a=0;a<s;a++)if(this._newViewValue[a]!==this._thousand){if(i++,i>=t)break}else r++;t+=r}return t}},{key:"_buildRegexp",value:function(){var e="-?";this._positive===!1&&this._negative===!0?e="-":this._positive===!0&&this._negative===!1&&(e="");var t="[0-9]{0,"+this._integer+"}";0===this._integer&&(t="0");var i="(\\"+this._separator+"([0-9]){0,"+this._fraction+"})";return 0===this._fraction&&(i=""),new RegExp("^"+e+t+i+"?$")}},{key:"_removeLeadingZero",value:function(e){return e.replace(/^0+/g,"").replace(/^-0(\d+)/g,"-$1").replace(/^-([\.,])/,"-0$1").replace(/^[\.,]/g,"0$&")}},{key:"_removeThousandSeparator",value:function(e){return this._isThousand?e.replace(new RegExp("\\"+this._thousand,"g"),""):e}},{key:"_createModelValueFromView",value:function(e){return","===this._separator?parseFloat(e.replace(/\./g,"").replace(",",".")):parseFloat(e.replace(/,/g,""))}},{key:"_createViewValueFromView",value:function(e){return this._isThousand?(e=e.split(this._separator),e[0]=e[0].replace(/\B(?=(\d{3})+(?!\d))/g,this._thousand),e.join(this._separator)):e}},{key:"_createViewValueFromModel",value:function(e){var t=String(e);return this._isThousand?(t=t.split("."),t[0]=t[0].replace(/\B(?=(\d{3})+(?!\d))/g,this._thousand),t.join(this._separator)):t.replace(/\./g,this._separator)}},{key:"separator",set:function(e){this._separator="."===e||","===e?e:this._separator,this._regexp=this._buildRegexp(),this._calculateThousandSeparator()}},{key:"integer",set:function(e){if(e>=0){var t=parseInt(e,10);isNaN(t)===!1&&isFinite(t)&&t>=0&&(this._integer=t)}this._regexp=this._buildRegexp()}},{key:"fraction",set:function(e){if(e>=0){var t=parseInt(e,10);isNaN(t)===!1&&isFinite(t)&&t>=0&&(this._fraction=t)}this._regexp=this._buildRegexp()}},{key:"positive",set:function(e){e!==!0&&e!==!1||(this._positive=e),this._regexp=this._buildRegexp()}},{key:"negative",set:function(e){e!==!0&&e!==!1||(this._negative=e),this._regexp=this._buildRegexp()}},{key:"thousand",set:function(e){this._isThousand=e||" "===e," "===e&&(this._thousand=" "),this._calculateThousandSeparator()}},{key:"modelValue",get:function(){return this._newModelValue}},{key:"viewValue",get:function(){return this._newViewValue}},{key:"cursorPosition",get:function(){return this._cursor}}]),e}();t.default=a}])});
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var i=t();for(var r in i)("object"==typeof exports?exports:e)[r]=i[r]}}(this,function(){return function(e){function t(r){if(i[r])return i[r].exports;var s=i[r]={exports:{},id:r,loaded:!1};return e[r].call(s.exports,s,s.exports,t),s.loaded=!0,s.exports}var i={};return t.m=e,t.c=i,t.p="",t(0)}([function(e,t,i){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0});var s=i(1),a=r(s);t.default=a.default},function(e,t){"use strict";function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,i,r){return i&&e(t.prototype,i),r&&e(t,r),t}}(),s=function(){function e(){i(this,e),this._separator=".",this._integer=10,this._fraction=10,this._positive=!0,this._negative=!0,this._regexp=this._buildRegexp(),this._isThousand=!1,this._thousand=null}return r(e,[{key:"clone",value:function(){var t=new e;return t._separator=this._separator,t._integer=this._integer,t._fraction=this._fraction,t._positive=this._positive,t._negative=this._negative,t._regexp=this._regexp,t._isThousand=this._isThousand,t._thousand=this._thousand,t._cursor=this._cursor,t}},{key:"calculateFromModel",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return this._createViewValueFromModel(e)}},{key:"calculateFromView",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;this._rawViewValue=e,this._newModelValue=0,this._newViewValue="",this._cursor=t;var i=String(this._rawViewValue);return i=this._removeThousandSeparator(i),i=this._removeLeadingZero(i),""===i&&"0"===String(this._rawViewValue).charAt(0)?this._createCorrectResponse("0",0,1):void 0===i||""===i?this._createCorrectResponse("",0,0):"-"===i?this._createCorrectResponse("-",0,1):this._regexp.test(i)!==!1&&this._createCorrectResponse(this._createViewValueFromView(i),this._createModelValueFromView(i),this._calculateNewCursorPosition())}},{key:"_createCorrectResponse",value:function(e,t,i){return{view:e,model:t,cursor:i}}},{key:"_calculateThousandSeparator",value:function(){" "!==this._thousand&&("."===this._separator?this._thousand=",":this._thousand=".")}},{key:"_calculateNewCursorPosition",value:function(){var e=String(this._rawViewValue).slice(0,this._cursor);e=this._removeThousandSeparator(e),e=this._removeLeadingZero(e);var t=e.length;if(this._isThousand){var i=0,r=0,s=void 0,a=this._newViewValue.length;for(s=0;s<a;s++)if(this._newViewValue[s]!==this._thousand){if(i++,i>=t)break}else r++;t+=r}return t}},{key:"_buildRegexp",value:function(){var e="-?";this._positive===!1&&this._negative===!0?e="-":this._positive===!0&&this._negative===!1&&(e="");var t="[0-9]{0,"+this._integer+"}";0===this._integer&&(t="0");var i="(\\"+this._separator+"([0-9]){0,"+this._fraction+"})";return 0===this._fraction&&(i=""),new RegExp("^"+e+t+i+"?$")}},{key:"_removeLeadingZero",value:function(e){return e.replace(/^0+/g,"").replace(/^-0(\d+)/g,"-$1").replace(/^-([\.,])/,"-0$1").replace(/^[\.,]/g,"0$&")}},{key:"_removeThousandSeparator",value:function(e){return this._isThousand?e.replace(new RegExp("\\"+this._thousand,"g"),""):e}},{key:"_createModelValueFromView",value:function(e){return","===this._separator?parseFloat(e.replace(/\./g,"").replace(",",".")):parseFloat(e.replace(/,/g,""))}},{key:"_createViewValueFromView",value:function(e){return this._isThousand?(e=e.split(this._separator),e[0]=e[0].replace(/\B(?=(\d{3})+(?!\d))/g,this._thousand),e.join(this._separator)):e}},{key:"_createViewValueFromModel",value:function(e){var t=String(e);return this._isThousand?(t=t.split("."),t[0]=t[0].replace(/\B(?=(\d{3})+(?!\d))/g,this._thousand),t.join(this._separator)):t.replace(/\./g,this._separator)}},{key:"separator",set:function(e){this._separator="."===e||","===e?e:this._separator,this._regexp=this._buildRegexp(),this._calculateThousandSeparator()}},{key:"integer",set:function(e){if(e>=0){var t=parseInt(e,10);isNaN(t)===!1&&isFinite(t)&&t>=0&&(this._integer=t)}this._regexp=this._buildRegexp()}},{key:"fraction",set:function(e){if(e>=0){var t=parseInt(e,10);isNaN(t)===!1&&isFinite(t)&&t>=0&&(this._fraction=t)}this._regexp=this._buildRegexp()}},{key:"positive",set:function(e){e!==!0&&e!==!1||(this._positive=e),this._regexp=this._buildRegexp()}},{key:"negative",set:function(e){e!==!0&&e!==!1||(this._negative=e),this._regexp=this._buildRegexp()}},{key:"thousand",set:function(e){this._isThousand=e||" "===e," "===e&&(this._thousand=" "),this._calculateThousandSeparator()}},{key:"cursorPosition",get:function(){return this._cursor}}]),e}();t.default=s}])});

@@ -77,10 +77,8 @@ class DynamicNumber {

calculateViewFromModel(modelValue = 0) {
calculateFromModel(modelValue = 0) {
return this._createViewValueFromModel(modelValue);
}
calculate(rawViewValue = 0, oldModelValue = 0, oldViewValue = '0', cursorPosition = null) {
calculateFromView(rawViewValue = 0, cursorPosition = null) {
this._rawViewValue = rawViewValue;
this._oldModelValue = oldModelValue;
this._oldViewValue = oldViewValue;
this._newModelValue = 0;

@@ -93,40 +91,32 @@ this._newViewValue = '';

value = this._removeLeadingZero(value);
if(value === '' && String(this._rawViewValue).charAt(0)=== '0'){
this._newModelValue = 0;
this._newViewValue = '0';
return;
if(value === "" && String(this._rawViewValue).charAt(0)=== "0"){
return this._createCorrectResponse("0", 0, 1);
}
if(value === undefined || value === ''){
this._newModelValue = 0;
this._newViewValue = '';
return;
if(value === undefined || value === ""){
return this._createCorrectResponse("", 0, 0);
}
if(value === '-'){
this._newModelValue = 0;
this._newViewValue = '-';
return;
if(value === "-"){
return this._createCorrectResponse("-", 0, 1);
}
//test fails, therefore we use old values
if(this._regexp.test(value) === false){
this._newModelValue = this._oldModelValue;
this._newViewValue = this._oldViewValue;
return;
return false;
}
// view value success 'correct view format' test
else {
this._newModelValue = this._createModelValueFromView(value);
this._newViewValue = this._createViewValueFromView(value);
this._cursor = this._calculateNewCursorPosition();
return;
return this._createCorrectResponse(
this._createViewValueFromView(value),
this._createModelValueFromView(value),
this._calculateNewCursorPosition()
);
}
}
get modelValue() {
return this._newModelValue;
_createCorrectResponse(view, model, cursor) {
return {
view,
model,
cursor
};
}
get viewValue() {
return this._newViewValue;
}
get cursorPosition() {

@@ -133,0 +123,0 @@ return this._cursor;

@@ -20,10 +20,11 @@

var value = '1234567';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 1234567', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(1234567);
expect(resp.model).to.be.equal(1234567);
});
it('should viewValue equal \'1234567\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('1234567');
expect(resp.view).to.be.equal('1234567');
});

@@ -33,10 +34,11 @@ });

var value = '-1234567';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal -1234567', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(-1234567);
expect(resp.model).to.be.equal(-1234567);
});
it('should viewValue equal \'-1234567\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('-1234567');
expect(resp.view).to.be.equal('-1234567');
});

@@ -46,22 +48,21 @@ });

var value = '123,4567';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(0);
it('should fail', ()=>{
expect(resp).to.be.equal(false);
});
it('should viewValue equal \'0\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('0');
});
});
describe('value=\'123.4567\'', ()=>{
var value = '123.4567';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 123.4567', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(123.4567);
expect(resp.model).to.be.equal(123.4567);
});
it('should viewValue equal \'123.4567\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('123.4567');
expect(resp.view).to.be.equal('123.4567');
});

@@ -71,10 +72,11 @@ });

var value = '.';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(0);
expect(resp.model).to.be.equal(0);
});
it('should viewValue equal \'0.\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('0.');
expect(resp.view).to.be.equal('0.');
});

@@ -84,10 +86,11 @@ });

var value = '-';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(0);
expect(resp.model).to.be.equal(0);
});
it('should viewValue equal \'-\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('-');
expect(resp.view).to.be.equal('-');
});

@@ -97,10 +100,11 @@ });

var value = '-.';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(0);
expect(resp.model).to.be.equal(0);
});
it('should viewValue equal \'-0.\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('-0.');
expect(resp.view).to.be.equal('-0.');
});

@@ -110,23 +114,19 @@ });

var value = '12345678901234567';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(0);
it('should fail', ()=>{
expect(resp).to.be.equal(false);
});
it('should viewValue equal \'0\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('0');
});
});
describe('value=\'1.23456789012345678\'', ()=>{
var value = '1.23456789012345678';
var resp;
before(()=>{
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=>{
expect(dynamicNumber.modelValue).to.be.equal(0);
it('should fail', ()=>{
expect(resp).to.be.equal(false);
});
it('should viewValue equal \'0\'', ()=>{
expect(dynamicNumber.viewValue).to.be.equal('0');
});
});

@@ -144,10 +144,11 @@ });

var value = '123,4567';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 123.4567', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(123.4567);
expect(resp.model).to.be.equal(123.4567);
});
it('should viewValue equal \'123,4567\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('123,4567');
expect(resp.view).to.be.equal('123,4567');
});

@@ -157,11 +158,9 @@ });

var value = '123.4567';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(0);
it('should fail', ()=>{
expect(resp).to.be.equal(false);
});
it('should viewValue equal \'0\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('0');
});
});

@@ -181,10 +180,11 @@ });

var value = '123,4567';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 123.4567', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(123.4567);
expect(resp.model).to.be.equal(123.4567);
});
it('should viewValue equal \'123,4567\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('123,4567');
expect(resp.view).to.be.equal('123,4567');
});

@@ -194,11 +194,9 @@ });

var value = '-123,4567';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(0);
it('should fail', ()=>{
expect(resp).to.be.equal(false);
});
it('should viewValue equal \'0\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('0');
});
});

@@ -218,10 +216,11 @@ });

var value = '-123,4567';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal -123.4567', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(-123.4567);
expect(resp.model).to.be.equal(-123.4567);
});
it('should viewValue equal \'-123,4567\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('-123,4567');
expect(resp.view).to.be.equal('-123,4567');
});

@@ -231,11 +230,9 @@ });

var value = '123,4567';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 0', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(0);
it('should fail', ()=>{
expect(resp).to.be.equal(false);
});
it('should viewValue equal \'0\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('0');
});
});

@@ -256,10 +253,11 @@ });

var value = '-123456789,123';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal -123456789.123', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(-123456789.123);
expect(resp.model).to.be.equal(-123456789.123);
});
it('should viewValue equal \'-123.456.789,123\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('-123.456.789,123');
expect(resp.view).to.be.equal('-123.456.789,123');
});

@@ -269,10 +267,11 @@ });

var value = '123456789,123';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 123456789.123', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(123456789.123);
expect(resp.model).to.be.equal(123456789.123);
});
it('should viewValue equal \'123.456.789,123\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('123.456.789,123');
expect(resp.view).to.be.equal('123.456.789,123');
});

@@ -294,10 +293,11 @@ });

var value = '-123456789.123';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal -123456789.123', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(-123456789.123);
expect(resp.model).to.be.equal(-123456789.123);
});
it('should viewValue equal \'-123 456 789.123\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('-123 456 789.123');
expect(resp.view).to.be.equal('-123 456 789.123');
});

@@ -307,10 +307,11 @@ });

var value = '123456789.123';
var resp;
before(()=> {
dynamicNumber.calculate(value);
resp = dynamicNumber.calculateFromView(value);
});
it('should modelValue equal 123456789.123', ()=> {
expect(dynamicNumber.modelValue).to.be.equal(123456789.123);
expect(resp.model).to.be.equal(123456789.123);
});
it('should viewValue equal \'123 456 789.123\'', ()=> {
expect(dynamicNumber.viewValue).to.be.equal('123 456 789.123');
expect(resp.view).to.be.equal('123 456 789.123');
});

@@ -317,0 +318,0 @@ });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc