angular-dynamic-number
Advanced tools
Comparing version 1.6.0 to 1.7.0
{ | ||
"name": "angular-dynamic-number", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"homepage": "https://github.com/uhlryk/angular-dynamic-number", | ||
@@ -5,0 +5,0 @@ "authors": [ |
{ | ||
"name": "angular-dynamic-number", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Highly customizable angular directive for numbers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -37,2 +37,3 @@ # Angular Dynamic Number | ||
- custom strategies | ||
- allow add currency (single character) | ||
@@ -107,2 +108,12 @@ ## Limitations: | ||
**num-prepend** | ||
Allow to set single character prepend currency e.g. $1234.12. Html could have problem with show some characters. In those situations you should set currency as html entit. | ||
€ = `€` | ||
**num-append** | ||
Allow to set single character append currency e.g. 1234.12€. Html could have problem with show some characters. In those situations you should set currency as html entit. | ||
€ = `€` | ||
## Custom strategies | ||
@@ -145,3 +156,3 @@ | ||
{{ expression | awnum:numFrac:numSep:numRound:numFixed:numThousand:numThousandSep}} | ||
{{ expression | awnum:numFrac:numSep:numRound:numFixed:numThousand:numThousandSep:numPrepend:numAppend}} | ||
@@ -172,2 +183,12 @@ **numFrac** | ||
**numPrepend** | ||
Allow to set single character prepend currency e.g. $1234.12. Html could have problem with show some characters. In those situations you should set currency as html entit. | ||
€ = `€` | ||
**numAppend** | ||
Allow to set single character append currency e.g. 1234.12€. Html could have problem with show some characters. In those situations you should set currency as html entit. | ||
€ = `€` | ||
## ngTrim and spaces | ||
@@ -174,0 +195,0 @@ |
/*jslint node: true */ | ||
(function(window, angular, undefined) {"use strict"; | ||
function convModelToView(modelValue, viewSeparator){ | ||
function convModelToView(modelValue, viewSeparator, prepend, append){ | ||
if(modelValue === undefined) { | ||
return ''; | ||
} | ||
var newViewValue = ''; | ||
if(viewSeparator === ',') { | ||
return String(modelValue).replace(".",","); | ||
newViewValue = String(modelValue).replace(".",","); | ||
} else { | ||
return String(modelValue); | ||
newViewValue = String(modelValue); | ||
} | ||
return addPrependAppend(newViewValue, prepend, append); | ||
} | ||
@@ -20,2 +22,18 @@ function convViewToModel(viewValue, viewSeparator, thousandSeparator) { | ||
} | ||
function addPrependAppend(value, prepend, append) { | ||
var newViewValue = value; | ||
if(append) { | ||
newViewValue = newViewValue + append; | ||
} | ||
if(prepend) { | ||
if(/^\-.+/.test(newViewValue)) { | ||
newViewValue = newViewValue.replace('-', '-' + prepend); | ||
} else if(/^\-/.test(newViewValue)) { | ||
newViewValue = newViewValue; | ||
} else { | ||
newViewValue = prepend + newViewValue; | ||
} | ||
} | ||
return newViewValue; | ||
} | ||
function initIntegerPart(attrs_num_int, def_num_int){ | ||
@@ -105,2 +123,12 @@ if(attrs_num_int >= 0){ | ||
} | ||
function initNumAppendPrepend(attrs_num_char){ | ||
var regexp = new RegExp('[^\\d,\\.\\s\\-]{1}'); | ||
if(regexp.test(attrs_num_char)) { | ||
return attrs_num_char; | ||
} | ||
return null; | ||
} | ||
function buildRegexp(integerPart, fractionPart, fractionSeparator, isPositiveNumber, isNegativeNumber){ | ||
@@ -131,2 +159,12 @@ var negativeRegex = '-?'; | ||
} | ||
function removePrependAppendChars(value, prepend, append) { | ||
var newValue = value; | ||
if(prepend) { | ||
newValue = newValue.replace(new RegExp('[\\'+prepend+']','g'),''); | ||
} | ||
if(append) { | ||
newValue = newValue.replace(new RegExp('[\\'+append+']','g'),''); | ||
} | ||
return newValue; | ||
} | ||
function removeThousandSeparators(value, thousandSeparator){ | ||
@@ -146,9 +184,9 @@ if(thousandSeparator === '.') { | ||
} | ||
function changeViewValue(ngModelController, value){ | ||
function changeViewValue(ngModelController, value, prepend, append){ | ||
// https://github.com/angular/angular.js/issues/13068 | ||
// ngModelController.$viewValue = value; | ||
ngModelController.$setViewValue(value); | ||
ngModelController.$setViewValue(addPrependAppend(value, prepend, append)); | ||
ngModelController.$render(); | ||
} | ||
function filterModelValue(value, fractionPart, fractionSeparator, roundFunction, numFixed, isThousandSeparator, thousandSeparator){ | ||
function filterModelValue(value, fractionPart, fractionSeparator, roundFunction, numFixed, isThousandSeparator, thousandSeparator, prepend, append){ | ||
value = Number(value); | ||
@@ -158,5 +196,5 @@ if(!isNaN(value) && isFinite(value)) { | ||
if(numFixed) { | ||
value = convModelToView((roundFunction(value*powerOfTen)/powerOfTen).toFixed(fractionPart), fractionSeparator); | ||
value = convModelToView((roundFunction(value*powerOfTen)/powerOfTen).toFixed(fractionPart), fractionSeparator, prepend, append); | ||
} else { | ||
value = convModelToView(String(roundFunction(value*powerOfTen)/powerOfTen), fractionSeparator); | ||
value = convModelToView(String(roundFunction(value*powerOfTen)/powerOfTen), fractionSeparator, prepend, append); | ||
} | ||
@@ -235,3 +273,5 @@ if(isThousandSeparator){ | ||
numThousand: "@", | ||
numThousandSep: "@" | ||
numThousandSep: "@", | ||
numPrepend: "@", | ||
numAppend: "@" | ||
}, | ||
@@ -260,3 +300,4 @@ link: function(scope, element, attrs, ngModelController) { | ||
var thousandSeparator = initThousandSeparator(scope.numThousandSep !== undefined ? scope.numThousandSep : strategy.numThousandSep, fractionSeparator, fractionSeparator==='.'?',':'.'); | ||
var prepend = initNumAppendPrepend(scope.numPrepend !== undefined ? scope.numPrepend : strategy.numPrepend); | ||
var append = initNumAppendPrepend(scope.numAppend !== undefined ? scope.numAppend : strategy.numAppend); | ||
if(isPositiveNumber === false && isNegativeNumber === false) { | ||
@@ -268,4 +309,5 @@ 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 parsedValue = String(value); | ||
parsedValue = removePrependAppendChars(parsedValue, prepend, append); | ||
if(new RegExp('^[\.,'+thousandSeparator+']{2,}').test(parsedValue)) { | ||
changeViewValue(ngModelController, 0); | ||
changeViewValue(ngModelController, 0, prepend, append); | ||
return 0; | ||
@@ -283,3 +325,3 @@ } | ||
if(parsedValue === '' && String(value).charAt(0)=== '0'){ | ||
changeViewValue(ngModelController, 0); | ||
changeViewValue(ngModelController, 0, prepend, append); | ||
return 0; | ||
@@ -291,3 +333,3 @@ } | ||
if(parsedValue === '-'){ | ||
changeViewValue(ngModelController, '-'); | ||
changeViewValue(ngModelController, '-', prepend, append); | ||
return 0; | ||
@@ -300,7 +342,7 @@ } | ||
if(viewRegexTest.test(parsedValue) === false){ | ||
var modelValue = convModelToView(ngModelController.$modelValue, fractionSeparator); | ||
var modelValue = convModelToView(ngModelController.$modelValue, fractionSeparator, prepend, append); | ||
if(isThousandSeparator){ | ||
modelValue = addThousandSeparator(modelValue, thousandSeparator); | ||
} | ||
changeViewValue(ngModelController, modelValue); | ||
changeViewValue(ngModelController, modelValue, prepend, append); | ||
setCaretPosition(element[0],cursorPosition-1); | ||
@@ -320,3 +362,3 @@ return ngModelController.$modelValue; | ||
} | ||
changeViewValue(ngModelController, parsedValue); | ||
changeViewValue(ngModelController, parsedValue, prepend, append); | ||
setCaretPosition(element[0],currentPosition + dots); | ||
@@ -330,3 +372,3 @@ return convViewToModel(parsedValue, fractionSeparator, thousandSeparator); | ||
ngModelController.$formatters.push(function(value){ | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, false, isThousandSeparator, thousandSeparator); | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, false, isThousandSeparator, thousandSeparator, prepend, append); | ||
}); | ||
@@ -355,3 +397,3 @@ } | ||
.filter('awnum', function() { | ||
return function(value, numFract, numSep, numRound, numFixed, numThousand, numThousandSep) { | ||
return function(value, numFract, numSep, numRound, numFixed, numThousand, numThousandSep, numPrepend, numAppend) { | ||
var fractionPart = initFractionPart(numFract, 2); | ||
@@ -363,3 +405,5 @@ var fractionSeparator = initSeparator(numSep, '.'); | ||
var thousandSeparator = initThousandSeparator(numThousandSep, fractionSeparator, fractionSeparator==='.'?',':'.'); | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, isFixed, isThousandSeparator, thousandSeparator); | ||
var prepend = initNumAppendPrepend(numPrepend); | ||
var append = initNumAppendPrepend(numAppend); | ||
return filterModelValue(value, fractionPart, fractionSeparator, roundFunction, isFixed, isThousandSeparator, thousandSeparator, prepend, append); | ||
}; | ||
@@ -366,0 +410,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,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); | ||
!function(n,e,t){"use strict";function r(n,e,r,u){if(n===t)return"";var a="";return a=","===e?String(n).replace(".",","):String(n),i(a,r,u)}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,t){var r=n;return t&&(r+=t),e&&(r=/^\-.+/.test(r)?r.replace("-","-"+e):/^\-/.test(r)?r:e+r),r}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){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function c(n,e){return","===n?",":"."===n?".":e}function s(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 m(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 l(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function p(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){var e=new RegExp("[^\\d,\\.\\s\\-]{1}");return e.test(n)?n:null}function v(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 S(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 h(n,e,t){var r=n;return e&&(r=r.replace(new RegExp("[\\"+e+"]","g"),"")),t&&(r=r.replace(new RegExp("[\\"+t+"]","g"),"")),r}function w(n,e){return"."===e?String(n).replace(/\./g,""):","===e?String(n).replace(/,/g,""):String(n).replace(new RegExp("\\s","g"),"")}function R(n,e){return n=String(n).split("."),n[0]=n[0].replace(/\B(?=(\d{3})+(?!\d))/g,e),n.join(".")}function x(n,e,t,r){n.$setViewValue(i(e,t,r)),n.$render()}function N(n,e,t,u,i,a,o,c,s){if(n=Number(n),!isNaN(n)&&isFinite(n)){var f=Math.pow(10,e);return n=i?r((u(n*f)/f).toFixed(e),t,c,s):r(String(u(n*f)/f),t,c,s),a&&(n=R(n,o)),n}return i?0..toFixed(e):"0"}function y(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,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 b(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 E(n){return{restrict:"A",require:"?ngModel",scope:{awnum:"@",numInt:"@",numFract:"@",numSep:"@",numPos:"@",numNeg:"@",numRound:"@",numThousand:"@",numThousandSep:"@",numPrepend:"@",numAppend:"@"},link:function(e,i,g,E){if(!i[0]||"INPUT"!==i[0].tagName||"text"!==i[0].type)return void console.warn("Directive angular-dynamic-number works only for 'input' tag with type = 'text'");if(!E)return void console.warn("Directive angular-dynamic-number need ngModel attribute");var T={};e.awnum&&(T=n.getStrategy(e.awnum));var F=a(e.numInt!==t?e.numInt:T.numInt,6),P=o(e.numFract!==t?e.numFract:T.numFract,2),M=c(e.numSep!==t?e.numSep:T.numSep,"."),I=s(e.numPos!==t?e.numPos:T.numPos,!0),A=f(e.numNeg!==t?e.numNeg:T.numNeg,!0),V=m(e.numRound!==t?e.numRound:T.numRound,Math.round),k=l(e.numThousand!==t?e.numThousand:T.numThousand,!1),D=p(e.numThousandSep!==t?e.numThousandSep:T.numThousandSep,M,"."===M?",":"."),_=d(e.numPrepend!==t?e.numPrepend:T.numPrepend),j=d(e.numAppend!==t?e.numAppend:T.numAppend);if(I===!1&&A===!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 q=v(F,P,M,I,A);E.$parsers.unshift(function(n){var e=String(n);if(e=h(e,_,j),new RegExp("^[.,"+D+"]{2,}").test(e))return x(E,0,_,j),0;var a=y(i[0]),o=e.slice(0,a),c=o.length;if(o=w(o,D),e=w(e,D),o=S(o),e=S(e),""===e&&"0"===String(n).charAt(0))return x(E,0,_,j),0;if(e===t||""===e)return 0;if("-"===e)return x(E,"-",_,j),0;if(q.test(e)===!1){var s=r(E.$modelValue,M,_,j);return k&&(s=R(s,D)),x(E,s,_,j),$(i[0],a-1),E.$modelValue}var f=0,m=a-c+o.length;return k&&(e=R(e,D),f=b(e,D,m)),x(E,e,_,j),$(i[0],m+f),u(e,M,D)}),E.$formatters.push(function(n){return N(n,P,M,V,!1,k,D,_,j)})}}}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,a,s,f){var v=o(e,2),S=c(t,"."),h=m(r,Math.round),w=g(u,!1),R=l(i,!1),x=p(a,S,"."===S?",":"."),y=d(s),$=d(f);return N(n,v,S,h,w,R,x,y,$)}}).directive("awnum",["dynamicNumberStrategy",E])}(window,window.angular); |
Sorry, the diff of this file is not supported yet
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
57841
430
210