angular-dynamic-number
Advanced tools
Comparing version 1.5.5 to 1.6.0
{ | ||
"name": "angular-dynamic-number", | ||
"version": "1.5.5", | ||
"version": "1.6.0", | ||
"homepage": "https://github.com/uhlryk/angular-dynamic-number", | ||
@@ -5,0 +5,0 @@ "authors": [ |
{ | ||
"name": "angular-dynamic-number", | ||
"version": "1.5.5", | ||
"version": "1.6.0", | ||
"description": "Highly customizable angular directive for numbers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -31,3 +31,4 @@ # Angular Dynamic Number | ||
- model value is correct javascript number, but view value may be correct number for localities | ||
- dynamic thousand separator (if decimal separator is comma then thousand separator is dot) | ||
- dynamic thousand separator (by default if decimal separator is comma then thousand separator is dot) | ||
- thousand separarator: space, dot or comma | ||
- filter with comma/dot separator and congurable number of fraction digits | ||
@@ -101,2 +102,7 @@ - filter with thousand separator | ||
**num-thousand-sep**: | ||
Set thousand separator (dot or comma or space) (enable if num-thousand = true, by default if num-sep equal dot then thousand separator is comma). | ||
If you want to set separator as space remember that angular by default trim spaces. You can as value set "{{' '}}" | ||
## Custom strategies | ||
@@ -139,3 +145,3 @@ | ||
{{ expression | awnum:numFrac:numSep:numRound:numFixed:numThousand}} | ||
{{ expression | awnum:numFrac:numSep:numRound:numFixed:numThousand:numThousandSep}} | ||
@@ -162,7 +168,20 @@ **numFrac** | ||
**numThousandSep** | ||
Set thousand separator (dot or comma or space). | ||
## ngTrim and spaces | ||
This is angular input directive parameter. By default it has value true, which means that it automatically trim spaces and Angular Dynamic Number don't get spaces. | ||
Therefore the best result is when you set for input ng-trim=false | ||
## Example: | ||
Negative number with max value 9999.99 and comma as separator | ||
<input type='text' ng-model='value4' awnum num-sep=',' num-int=4 num-fract=2 num-pos=false> | ||
<input type='text' ng-trim=false ng-model='value4' awnum num-sep=',' num-int=4 num-fract=2 num-pos=false> | ||
Negative or positive number with max value 9999.99 and comma as separator and thousand separator space | ||
<input type='text' ng-trim=false ng-model='value4' awnum num-sep=',' num-int=4 num-fract=2 num-pos=false num-thousand=true num-thousand-sep="{{' '}}"> | ||
Filter for number with max 3 fraction number and comma separator | ||
@@ -169,0 +188,0 @@ |
@@ -13,7 +13,7 @@ /*jslint node: true */ | ||
} | ||
function convViewToModel(viewValue, viewSeparator) { | ||
function convViewToModel(viewValue, viewSeparator, thousandSeparator) { | ||
if(viewSeparator === ',') { | ||
return String(viewValue).replace(/\./g,"").replace(",","."); | ||
} else { | ||
return String(viewValue).replace(/,/g,""); | ||
return String(viewValue).replace(/[\.\s]/g,"").replace(",","."); | ||
} else if(viewSeparator === '.') { | ||
return String(viewValue).replace(/[,\s]/g,""); | ||
} | ||
@@ -82,5 +82,5 @@ } | ||
function initIsThousand(attrs_thousand, def_thousand){ | ||
if(attrs_thousand === 'false') { | ||
if(attrs_thousand === 'false' || attrs_thousand === false) { | ||
return false; | ||
} else if(attrs_thousand === 'true') { | ||
} else if(attrs_thousand === 'true' || attrs_thousand === true) { | ||
return true; | ||
@@ -90,2 +90,18 @@ } | ||
} | ||
function initThousandSeparator(attrs_thousand, fractionSeparator, def_thousand) { | ||
if(!attrs_thousand) { | ||
return def_thousand; | ||
} | ||
var regexp; | ||
if(fractionSeparator === '.') { | ||
regexp = new RegExp('^[,\\s]$'); | ||
} else { | ||
regexp = new RegExp('^[\\.\\s]$'); | ||
} | ||
if(regexp.test(attrs_thousand)) { | ||
return attrs_thousand; | ||
} else { | ||
return def_thousand; | ||
} | ||
} | ||
function buildRegexp(integerPart, fractionPart, fractionSeparator, isPositiveNumber, isNegativeNumber){ | ||
@@ -113,4 +129,4 @@ var negativeRegex = '-?'; | ||
.replace(/^-0(\d+)/g, "-$1")//change -013212 to -0 | ||
.replace(/^-([\.,])/g, "-0$1")//change -. to -0. | ||
.replace(/^[\.,]/g, "0$&");//change . to 0. | ||
.replace(new RegExp('^-([\\.,\\s])', 'g'), "-0$1")//change -. to -0. | ||
.replace(new RegExp('^[\\.,\\s]','g'), "0$&");//change . to 0. | ||
} | ||
@@ -120,8 +136,12 @@ function removeThousandSeparators(value, thousandSeparator){ | ||
return String(value).replace(/\./g, ""); | ||
} else if(thousandSeparator === ','){ | ||
return String(value).replace(/,/g, ""); | ||
} else { | ||
return String(value).replace(/,/g, ""); | ||
return String(value).replace(new RegExp('\\s','g'), ""); | ||
} | ||
} | ||
function addThousandSeparator(value, thousandSeparator){ | ||
return String(value).replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator); | ||
value = String(value).split('.'); | ||
value[0] = value[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator); | ||
return value.join('.'); | ||
} | ||
@@ -134,3 +154,3 @@ function changeViewValue(ngModelController, value){ | ||
} | ||
function filterModelValue(value, fractionPart, fractionSeparator, roundFunction, numFixed, isThousandSeparator){ | ||
function filterModelValue(value, fractionPart, fractionSeparator, roundFunction, numFixed, isThousandSeparator, thousandSeparator){ | ||
value = Number(value); | ||
@@ -145,3 +165,3 @@ if(!isNaN(value) && isFinite(value)) { | ||
if(isThousandSeparator){ | ||
value = addThousandSeparator(value, fractionSeparator==='.'?',':'.'); | ||
value = addThousandSeparator(value, thousandSeparator); | ||
} | ||
@@ -216,3 +236,4 @@ return value; | ||
numRound: "@", | ||
numThousand: "@" | ||
numThousand: "@", | ||
numThousandSep: "@" | ||
}, | ||
@@ -240,2 +261,3 @@ link: function(scope, element, attrs, ngModelController) { | ||
var isThousandSeparator = initIsThousand(scope.numThousand !== undefined ? scope.numThousand : strategy.numThousand, false); | ||
var thousandSeparator = initThousandSeparator(scope.numThousandSep !== undefined ? scope.numThousandSep : strategy.numThousandSep, fractionSeparator, fractionSeparator==='.'?',':'.'); | ||
@@ -248,3 +270,3 @@ if(isPositiveNumber === false && isNegativeNumber === false) { | ||
var parsedValue = String(value); | ||
if(/^[\.,]{2,}/.test(parsedValue)) { | ||
if(new RegExp('^[\.,'+thousandSeparator+']{2,}').test(parsedValue)) { | ||
changeViewValue(ngModelController, 0); | ||
@@ -257,4 +279,4 @@ return 0; | ||
valBeforeCursor = removeThousandSeparators(valBeforeCursor, fractionSeparator==='.'?',':'.'); | ||
parsedValue = removeThousandSeparators(parsedValue, fractionSeparator==='.'?',':'.'); | ||
valBeforeCursor = removeThousandSeparators(valBeforeCursor, thousandSeparator); | ||
parsedValue = removeThousandSeparators(parsedValue, thousandSeparator); | ||
valBeforeCursor = removeLeadingZero(valBeforeCursor); | ||
@@ -281,3 +303,3 @@ parsedValue = removeLeadingZero(parsedValue); | ||
if(isThousandSeparator){ | ||
modelValue = addThousandSeparator(modelValue, fractionSeparator==='.'?',':'.'); | ||
modelValue = addThousandSeparator(modelValue, thousandSeparator); | ||
} | ||
@@ -296,8 +318,8 @@ changeViewValue(ngModelController, modelValue); | ||
if(isThousandSeparator){ | ||
parsedValue = addThousandSeparator(parsedValue, fractionSeparator==='.'?',':'.'); | ||
dots = countThousandSeparatorToPosition(parsedValue,fractionSeparator==='.'?',':'.',currentPosition); | ||
parsedValue = addThousandSeparator(parsedValue, thousandSeparator); | ||
dots = countThousandSeparatorToPosition(parsedValue,thousandSeparator,currentPosition); | ||
} | ||
changeViewValue(ngModelController, parsedValue); | ||
setCaretPosition(element[0],currentPosition + dots); | ||
return convViewToModel(parsedValue, fractionSeparator); | ||
return convViewToModel(parsedValue, fractionSeparator, thousandSeparator); | ||
} | ||
@@ -309,3 +331,3 @@ }); | ||
ngModelController.$formatters.push(function(value){ | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, false, isThousandSeparator); | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, false, isThousandSeparator, thousandSeparator); | ||
}); | ||
@@ -315,15 +337,3 @@ } | ||
} | ||
/** | ||
* filter does not validate data only filter fraction part and decimal separator | ||
*/ | ||
function dynamicNumberFilter(){ | ||
return function(value, numFract, numSep, numRound, numFixed, numThousand) { | ||
var fractionPart = initFractionPart(numFract, 2); | ||
var fractionSeparator = initSeparator(numSep, '.'); | ||
var roundFunction = initRound(numRound, Math.round); | ||
var isFixed = initIsFixed(numFixed, false); | ||
var isThousandSeparator = initIsThousand(numThousand, false); | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, isFixed, isThousandSeparator); | ||
}; | ||
} | ||
angular.module('dynamicNumber',[]) | ||
@@ -347,3 +357,3 @@ .provider('dynamicNumberStrategy', function() { | ||
.filter('awnum', function() { | ||
return function(value, numFract, numSep, numRound, numFixed, numThousand) { | ||
return function(value, numFract, numSep, numRound, numFixed, numThousand, numThousandSep) { | ||
var fractionPart = initFractionPart(numFract, 2); | ||
@@ -354,3 +364,4 @@ var fractionSeparator = initSeparator(numSep, '.'); | ||
var isThousandSeparator = initIsThousand(numThousand, false); | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, isFixed, isThousandSeparator); | ||
var thousandSeparator = initThousandSeparator(numThousandSep, fractionSeparator, fractionSeparator==='.'?',':'.'); | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, isFixed, isThousandSeparator, thousandSeparator); | ||
}; | ||
@@ -357,0 +368,0 @@ }) |
@@ -1,1 +0,1 @@ | ||
!function(n,e,t){"use strict";function r(n,e){return n===t?"":","===e?String(n).replace(".",","):String(n)}function u(n,e){return","===e?String(n).replace(/\./g,"").replace(",","."):String(n).replace(/,/g,"")}function i(n,e){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function a(n,e){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function o(n,e){return","===n?",":"."===n?".":e}function c(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function f(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function s(n,e){return"floor"===n?Math.floor:"ceil"===n?Math.ceil:"round"===n?Math.round:e}function m(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function l(n,e){return"false"===n?!1:"true"===n?!0:e}function g(n,e,t,r,u){var i="-?";r===!1&&u===!0?i="-":r===!0&&u===!1&&(i="");var a="[0-9]{0,"+n+"}";0===n&&(a="0");var o="(\\"+t+"([0-9]){0,"+e+"})";return 0===e&&(o=""),new RegExp("^"+i+a+o+"?$")}function d(n){return String(n).replace(/^0+/g,"").replace(/^-0(\d+)/g,"-$1").replace(/^-([\.,])/g,"-0$1").replace(/^[\.,]/g,"0$&")}function v(n,e){return"."===e?String(n).replace(/\./g,""):String(n).replace(/,/g,"")}function p(n,e){return String(n).replace(/\B(?=(\d{3})+(?!\d))/g,e)}function S(n,e){n.$setViewValue(e),n.$render()}function h(n,e,t,u,i,a){if(n=Number(n),!isNaN(n)&&isFinite(n)){var o=Math.pow(10,e);return n=i?r((u(n*o)/o).toFixed(e),t):r(String(u(n*o)/o),t),a&&(n=p(n,"."===t?",":".")),n}return i?0..toFixed(e):"0"}function w(n){var e=0;if(document.selection){n.focus();var t=document.selection.createRange();t.moveStart("character",-n.value.length),e=t.text.length}else(n.selectionStart||"0"==n.selectionStart)&&(e=n.selectionStart);return e}function N(n,e){if(null!==n)if(n.createTextRange){var t=n.createTextRange();t.move("character",e),t.select()}else n.selectionStart?(n.focus(),n.setSelectionRange(e,e)):n.focus()}function y(n,e,t){for(var r=0,u=0,i=0;i<n.length;i++)if(n[i]!==e){if(r++,r>=t)break}else u++;return u}function b(n){return{restrict:"A",require:"?ngModel",scope:{awnum:"@",numInt:"@",numFract:"@",numSep:"@",numPos:"@",numNeg:"@",numRound:"@",numThousand:"@"},link:function(e,m,b,$){if(!m[0]||"INPUT"!==m[0].tagName||"text"!==m[0].type)return void console.warn("Directive angular-dynamic-number works only for 'input' tag with type = 'text'");if(!$)return void console.warn("Directive angular-dynamic-number need ngModel attribute");var F={};e.awnum&&(F=n.getStrategy(e.awnum));var R=i(e.numInt!==t?e.numInt:F.numInt,6),x=a(e.numFract!==t?e.numFract:F.numFract,2),M=o(e.numSep!==t?e.numSep:F.numSep,"."),I=c(e.numPos!==t?e.numPos:F.numPos,!0),T=f(e.numNeg!==t?e.numNeg:F.numNeg,!0),P=s(e.numRound!==t?e.numRound:F.numRound,Math.round),V=l(e.numThousand!==t?e.numThousand:F.numThousand,!1);if(I===!1&&T===!1)throw new Error("Number is set to not be positive and not be negative. Change num_pos attr or/and num_neg attr to true");var k=g(R,x,M,I,T);$.$parsers.unshift(function(n){var e=String(n);if(/^[\.,]{2,}/.test(e))return S($,0),0;var i=w(m[0]),a=e.slice(0,i),o=a.length;if(a=v(a,"."===M?",":"."),e=v(e,"."===M?",":"."),a=d(a),e=d(e),""===e&&"0"===String(n).charAt(0))return S($,0),0;if(e===t||""===e)return 0;if("-"===e)return S($,"-"),0;if(k.test(e)===!1){var c=r($.$modelValue,M);return V&&(c=p(c,"."===M?",":".")),S($,c),N(m[0],i-1),$.$modelValue}var f=0,s=i-o+a.length;return V&&(e=p(e,"."===M?",":"."),f=y(e,"."===M?",":".",s)),S($,e),N(m[0],s+f),u(e,M)}),$.$formatters.push(function(n){return h(n,x,M,P,!1,V)})}}}e.module("dynamicNumber",[]).provider("dynamicNumberStrategy",function(){var n={};this.addStrategy=function(e,t){n[e]=t},this.getStrategy=function(e){return n[e]},this.$get=function(){return{getStrategy:function(e){return n[e]}}}}).filter("awnum",function(){return function(n,e,t,r,u,i){var c=a(e,2),f=o(t,"."),g=s(r,Math.round),d=m(u,!1),v=l(i,!1);return h(n,c,f,g,d,v)}}).directive("awnum",["dynamicNumberStrategy",b])}(window,window.angular); | ||
!function(n,e,t){"use strict";function r(n,e){return n===t?"":","===e?String(n).replace(".",","):String(n)}function u(n,e,t){return","===e?String(n).replace(/[\.\s]/g,"").replace(",","."):"."===e?String(n).replace(/[,\s]/g,""):void 0}function i(n,e){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function a(n,e){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function o(n,e){return","===n?",":"."===n?".":e}function c(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function s(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function f(n,e){return"floor"===n?Math.floor:"ceil"===n?Math.ceil:"round"===n?Math.round:e}function g(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function m(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function l(n,e,t){if(!n)return t;var r;return r="."===e?new RegExp("^[,\\s]$"):new RegExp("^[\\.\\s]$"),r.test(n)?n:t}function d(n,e,t,r,u){var i="-?";r===!1&&u===!0?i="-":r===!0&&u===!1&&(i="");var a="[0-9]{0,"+n+"}";0===n&&(a="0");var o="(\\"+t+"([0-9]){0,"+e+"})";return 0===e&&(o=""),new RegExp("^"+i+a+o+"?$")}function p(n){return String(n).replace(/^0+/g,"").replace(/^-0(\d+)/g,"-$1").replace(new RegExp("^-([\\.,\\s])","g"),"-0$1").replace(new RegExp("^[\\.,\\s]","g"),"0$&")}function v(n,e){return"."===e?String(n).replace(/\./g,""):","===e?String(n).replace(/,/g,""):String(n).replace(new RegExp("\\s","g"),"")}function S(n,e){return n=String(n).split("."),n[0]=n[0].replace(/\B(?=(\d{3})+(?!\d))/g,e),n.join(".")}function h(n,e){n.$setViewValue(e),n.$render()}function w(n,e,t,u,i,a,o){if(n=Number(n),!isNaN(n)&&isFinite(n)){var c=Math.pow(10,e);return n=i?r((u(n*c)/c).toFixed(e),t):r(String(u(n*c)/c),t),a&&(n=S(n,o)),n}return i?0..toFixed(e):"0"}function N(n){var e=0;if(document.selection){n.focus();var t=document.selection.createRange();t.moveStart("character",-n.value.length),e=t.text.length}else(n.selectionStart||"0"==n.selectionStart)&&(e=n.selectionStart);return e}function R(n,e){if(null!==n)if(n.createTextRange){var t=n.createTextRange();t.move("character",e),t.select()}else n.selectionStart?(n.focus(),n.setSelectionRange(e,e)):n.focus()}function x(n,e,t){for(var r=0,u=0,i=0;i<n.length;i++)if(n[i]!==e){if(r++,r>=t)break}else u++;return u}function y(n){return{restrict:"A",require:"?ngModel",scope:{awnum:"@",numInt:"@",numFract:"@",numSep:"@",numPos:"@",numNeg:"@",numRound:"@",numThousand:"@",numThousandSep:"@"},link:function(e,g,y,$){if(!g[0]||"INPUT"!==g[0].tagName||"text"!==g[0].type)return void console.warn("Directive angular-dynamic-number works only for 'input' tag with type = 'text'");if(!$)return void console.warn("Directive angular-dynamic-number need ngModel attribute");var b={};e.awnum&&(b=n.getStrategy(e.awnum));var T=i(e.numInt!==t?e.numInt:b.numInt,6),F=a(e.numFract!==t?e.numFract:b.numFract,2),E=o(e.numSep!==t?e.numSep:b.numSep,"."),M=c(e.numPos!==t?e.numPos:b.numPos,!0),I=s(e.numNeg!==t?e.numNeg:b.numNeg,!0),P=f(e.numRound!==t?e.numRound:b.numRound,Math.round),V=m(e.numThousand!==t?e.numThousand:b.numThousand,!1),k=l(e.numThousandSep!==t?e.numThousandSep:b.numThousandSep,E,"."===E?",":".");if(M===!1&&I===!1)throw new Error("Number is set to not be positive and not be negative. Change num_pos attr or/and num_neg attr to true");var A=d(T,F,E,M,I);$.$parsers.unshift(function(n){var e=String(n);if(new RegExp("^[.,"+k+"]{2,}").test(e))return h($,0),0;var i=N(g[0]),a=e.slice(0,i),o=a.length;if(a=v(a,k),e=v(e,k),a=p(a),e=p(e),""===e&&"0"===String(n).charAt(0))return h($,0),0;if(e===t||""===e)return 0;if("-"===e)return h($,"-"),0;if(A.test(e)===!1){var c=r($.$modelValue,E);return V&&(c=S(c,k)),h($,c),R(g[0],i-1),$.$modelValue}var s=0,f=i-o+a.length;return V&&(e=S(e,k),s=x(e,k,f)),h($,e),R(g[0],f+s),u(e,E,k)}),$.$formatters.push(function(n){return w(n,F,E,P,!1,V,k)})}}}e.module("dynamicNumber",[]).provider("dynamicNumberStrategy",function(){var n={};this.addStrategy=function(e,t){n[e]=t},this.getStrategy=function(e){return n[e]},this.$get=function(){return{getStrategy:function(e){return n[e]}}}}).filter("awnum",function(){return function(n,e,t,r,u,i,c){var s=a(e,2),d=o(t,"."),p=f(r,Math.round),v=g(u,!1),S=m(i,!1),h=l(c,d,"."===d?",":".");return w(n,s,d,p,v,S,h)}}).directive("awnum",["dynamicNumberStrategy",y])}(window,window.angular); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54385
16
388
189