From 640aa7fcaa0285d0b7831b0114a074a68cd987c5 Mon Sep 17 00:00:00 2001 From: Igor Rafael Date: Sun, 24 Sep 2017 11:17:17 -0300 Subject: [PATCH] refactor: reorganize helper modules --- src/br/boleto-bancario/boleto-bancario.js | 2 +- src/br/br-masks.js | 4 +- src/br/car-plate/car-plate.js | 2 +- src/br/cep/cep.js | 2 +- src/br/cnpj/cnpj.js | 3 +- src/br/cpf-cnpj/cpf-cnpj.js | 2 +- src/br/cpf/cpf.js | 3 +- src/br/inscricao-estadual/ie.js | 60 +++++++++++------------ src/br/nfe/nfe.js | 6 +-- src/br/phone/br-phone.js | 3 +- src/ch/ch-masks.js | 4 +- src/ch/phone/ch-phone.js | 3 +- src/fr/fr-masks.js | 4 +- src/fr/phone/fr-phone.js | 2 +- src/global/credit-card/credit-card.js | 2 +- src/global/global-masks.js | 4 +- src/global/money/money.js | 7 +-- src/global/number/number.js | 8 +-- src/global/percentage/percentage.js | 8 +-- src/global/time/time.js | 1 + src/helpers.js | 59 ---------------------- src/{libs => helpers}/mask-factory.js | 0 src/helpers/number-mask-builder.js | 38 ++++++++++++++ src/helpers/pre-formatters.js | 19 +++++++ src/{libs => helpers}/validators.js | 0 src/us/phone/us-phone.js | 2 +- src/us/us-masks.js | 4 +- 27 files changed, 125 insertions(+), 127 deletions(-) delete mode 100644 src/helpers.js rename src/{libs => helpers}/mask-factory.js (100%) create mode 100644 src/helpers/number-mask-builder.js create mode 100644 src/helpers/pre-formatters.js rename src/{libs => helpers}/validators.js (100%) diff --git a/src/br/boleto-bancario/boleto-bancario.js b/src/br/boleto-bancario/boleto-bancario.js index c88238d6..50a61e3a 100644 --- a/src/br/boleto-bancario/boleto-bancario.js +++ b/src/br/boleto-bancario/boleto-bancario.js @@ -1,7 +1,7 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); +var maskFactory = require('../../helpers/mask-factory'); var boletoBancarioMask = new StringMask('00000.00000 00000.000000 00000.000000 0 00000000000000'); diff --git a/src/br/br-masks.js b/src/br/br-masks.js index 1e1a00d3..e44d42b2 100644 --- a/src/br/br-masks.js +++ b/src/br/br-masks.js @@ -1,8 +1,6 @@ 'use strict'; -var m = angular.module('ui.utils.masks.br', [ - require('../helpers'), -]) +var m = angular.module('ui.utils.masks.br', []) .directive('uiBrBoletoBancarioMask', require('./boleto-bancario/boleto-bancario')) .directive('uiBrCarPlateMask', require('./car-plate/car-plate')) .directive('uiBrCepMask', require('./cep/cep')) diff --git a/src/br/car-plate/car-plate.js b/src/br/car-plate/car-plate.js index 8c1e88a7..512313d6 100644 --- a/src/br/car-plate/car-plate.js +++ b/src/br/car-plate/car-plate.js @@ -1,7 +1,7 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); +var maskFactory = require('../../helpers/mask-factory'); var carPlateMask = new StringMask('UUU-0000'); diff --git a/src/br/cep/cep.js b/src/br/cep/cep.js index 8b66ed8c..b572099b 100644 --- a/src/br/cep/cep.js +++ b/src/br/cep/cep.js @@ -1,7 +1,7 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); +var maskFactory = require('../../helpers/mask-factory'); var cepMask = new StringMask('00000-000'); diff --git a/src/br/cnpj/cnpj.js b/src/br/cnpj/cnpj.js index c9762560..ff9199f8 100644 --- a/src/br/cnpj/cnpj.js +++ b/src/br/cnpj/cnpj.js @@ -2,7 +2,8 @@ var StringMask = require('string-mask'); var BrV = require('br-validations'); -var maskFactory = require('../../libs/mask-factory'); + +var maskFactory = require('../../helpers/mask-factory'); var cnpjPattern = new StringMask('00.000.000\/0000-00'); diff --git a/src/br/cpf-cnpj/cpf-cnpj.js b/src/br/cpf-cnpj/cpf-cnpj.js index 0a733dc4..43bc34bc 100644 --- a/src/br/cpf-cnpj/cpf-cnpj.js +++ b/src/br/cpf-cnpj/cpf-cnpj.js @@ -2,7 +2,7 @@ var StringMask = require('string-mask'); var BrV = require('br-validations'); -var maskFactory = require('../../libs/mask-factory'); +var maskFactory = require('../../helpers/mask-factory'); var cnpjPattern = new StringMask('00.000.000\/0000-00'); var cpfPattern = new StringMask('000.000.000-00'); diff --git a/src/br/cpf/cpf.js b/src/br/cpf/cpf.js index 2df59a44..d04adae7 100644 --- a/src/br/cpf/cpf.js +++ b/src/br/cpf/cpf.js @@ -2,7 +2,8 @@ var StringMask = require('string-mask'); var BrV = require('br-validations'); -var maskFactory = require('../../libs/mask-factory'); + +var maskFactory = require('../../helpers/mask-factory'); var cpfPattern = new StringMask('000.000.000-00'); diff --git a/src/br/inscricao-estadual/ie.js b/src/br/inscricao-estadual/ie.js index fa0f3f7a..84c86b05 100644 --- a/src/br/inscricao-estadual/ie.js +++ b/src/br/inscricao-estadual/ie.js @@ -3,37 +3,37 @@ var StringMask = require('string-mask'); var BrV = require('br-validations'); -function BrIeMaskDirective($parse) { - var ieMasks = { - 'AC': [{mask: new StringMask('00.000.000/000-00')}], - 'AL': [{mask: new StringMask('000000000')}], - 'AM': [{mask: new StringMask('00.000.000-0')}], - 'AP': [{mask: new StringMask('000000000')}], - 'BA': [{chars: 8, mask: new StringMask('000000-00')}, {mask: new StringMask('0000000-00')}], - 'CE': [{mask: new StringMask('00000000-0')}], - 'DF': [{mask: new StringMask('00000000000-00')}], - 'ES': [{mask: new StringMask('00000000-0')}], - 'GO': [{mask: new StringMask('00.000.000-0')}], - 'MA': [{mask: new StringMask('000000000')}], - 'MG': [{mask: new StringMask('000.000.000/0000')}], - 'MS': [{mask: new StringMask('000000000')}], - 'MT': [{mask: new StringMask('0000000000-0')}], - 'PA': [{mask: new StringMask('00-000000-0')}], - 'PB': [{mask: new StringMask('00000000-0')}], - 'PE': [{chars: 9, mask: new StringMask('0000000-00')}, {mask: new StringMask('00.0.000.0000000-0')}], - 'PI': [{mask: new StringMask('000000000')}], - 'PR': [{mask: new StringMask('000.00000-00')}], - 'RJ': [{mask: new StringMask('00.000.00-0')}], - 'RN': [{chars: 9, mask: new StringMask('00.000.000-0')}, {mask: new StringMask('00.0.000.000-0')}], - 'RO': [{mask: new StringMask('0000000000000-0')}], - 'RR': [{mask: new StringMask('00000000-0')}], - 'RS': [{mask: new StringMask('000/0000000')}], - 'SC': [{mask: new StringMask('000.000.000')}], - 'SE': [{mask: new StringMask('00000000-0')}], - 'SP': [{mask: new StringMask('000.000.000.000')}, {mask: new StringMask('-00000000.0/000')}], - 'TO': [{mask: new StringMask('00000000000')}] - }; +var ieMasks = { + 'AC': [{mask: new StringMask('00.000.000/000-00')}], + 'AL': [{mask: new StringMask('000000000')}], + 'AM': [{mask: new StringMask('00.000.000-0')}], + 'AP': [{mask: new StringMask('000000000')}], + 'BA': [{chars: 8, mask: new StringMask('000000-00')}, {mask: new StringMask('0000000-00')}], + 'CE': [{mask: new StringMask('00000000-0')}], + 'DF': [{mask: new StringMask('00000000000-00')}], + 'ES': [{mask: new StringMask('00000000-0')}], + 'GO': [{mask: new StringMask('00.000.000-0')}], + 'MA': [{mask: new StringMask('000000000')}], + 'MG': [{mask: new StringMask('000.000.000/0000')}], + 'MS': [{mask: new StringMask('000000000')}], + 'MT': [{mask: new StringMask('0000000000-0')}], + 'PA': [{mask: new StringMask('00-000000-0')}], + 'PB': [{mask: new StringMask('00000000-0')}], + 'PE': [{chars: 9, mask: new StringMask('0000000-00')}, {mask: new StringMask('00.0.000.0000000-0')}], + 'PI': [{mask: new StringMask('000000000')}], + 'PR': [{mask: new StringMask('000.00000-00')}], + 'RJ': [{mask: new StringMask('00.000.00-0')}], + 'RN': [{chars: 9, mask: new StringMask('00.000.000-0')}, {mask: new StringMask('00.0.000.000-0')}], + 'RO': [{mask: new StringMask('0000000000000-0')}], + 'RR': [{mask: new StringMask('00000000-0')}], + 'RS': [{mask: new StringMask('000/0000000')}], + 'SC': [{mask: new StringMask('000.000.000')}], + 'SE': [{mask: new StringMask('00000000-0')}], + 'SP': [{mask: new StringMask('000.000.000.000')}, {mask: new StringMask('-00000000.0/000')}], + 'TO': [{mask: new StringMask('00000000000')}] +}; +function BrIeMaskDirective($parse) { function clearValue(value) { if (!value) { return value; diff --git a/src/br/nfe/nfe.js b/src/br/nfe/nfe.js index 1b007bdc..fce04653 100644 --- a/src/br/nfe/nfe.js +++ b/src/br/nfe/nfe.js @@ -1,10 +1,10 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); -var nfeAccessKeyMask = new StringMask('0000 0000 0000 0000 0000' + - ' 0000 0000 0000 0000 0000 0000'); +var maskFactory = require('../../helpers/mask-factory'); + +var nfeAccessKeyMask = new StringMask('0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000'); module.exports = maskFactory({ clearValue: function(rawValue) { diff --git a/src/br/phone/br-phone.js b/src/br/phone/br-phone.js index 549cc68e..c6477625 100644 --- a/src/br/phone/br-phone.js +++ b/src/br/phone/br-phone.js @@ -1,7 +1,8 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); + +var maskFactory = require('../../helpers/mask-factory'); /** * FIXME: all numbers will have 9 digits after 2016. diff --git a/src/ch/ch-masks.js b/src/ch/ch-masks.js index caf1c965..0ed9b447 100644 --- a/src/ch/ch-masks.js +++ b/src/ch/ch-masks.js @@ -1,8 +1,6 @@ 'use strict'; -var m = angular.module('ui.utils.masks.ch', [ - require('../helpers'), -]) +var m = angular.module('ui.utils.masks.ch', []) .directive('uiChPhoneNumber', require('./phone/ch-phone')); module.exports = m.name; diff --git a/src/ch/phone/ch-phone.js b/src/ch/phone/ch-phone.js index f8a96a83..e390002a 100644 --- a/src/ch/phone/ch-phone.js +++ b/src/ch/phone/ch-phone.js @@ -1,7 +1,8 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); + +var maskFactory = require('../../helpers/mask-factory'); var phoneMask = new StringMask('+00 00 000 00 00'); diff --git a/src/fr/fr-masks.js b/src/fr/fr-masks.js index b9ec3c6b..5025b823 100644 --- a/src/fr/fr-masks.js +++ b/src/fr/fr-masks.js @@ -1,8 +1,6 @@ 'use strict'; -var m = angular.module('ui.utils.masks.fr', [ - require('../helpers') -]) +var m = angular.module('ui.utils.masks.fr', []) .directive('uiFrPhoneNumber', require('./phone/fr-phone')); module.exports = m.name; diff --git a/src/fr/phone/fr-phone.js b/src/fr/phone/fr-phone.js index 2b5c1646..4b5aa34c 100644 --- a/src/fr/phone/fr-phone.js +++ b/src/fr/phone/fr-phone.js @@ -1,7 +1,7 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); +var maskFactory = require('../../helpers/mask-factory'); var phoneMaskFR = new StringMask('00 00 00 00 00'); diff --git a/src/global/credit-card/credit-card.js b/src/global/credit-card/credit-card.js index c27928f9..576beaad 100644 --- a/src/global/credit-card/credit-card.js +++ b/src/global/credit-card/credit-card.js @@ -1,7 +1,7 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); +var maskFactory = require('../../helpers/mask-factory'); var ccSize = 16; diff --git a/src/global/global-masks.js b/src/global/global-masks.js index 9951636c..b3f732a0 100644 --- a/src/global/global-masks.js +++ b/src/global/global-masks.js @@ -1,8 +1,6 @@ 'use strict'; -var m = angular.module('ui.utils.masks.global', [ - require('../helpers'), -]) +var m = angular.module('ui.utils.masks.global', []) .directive('uiCreditCard', require('./credit-card/credit-card')) .directive('uiDateMask', require('./date/date')) .directive('uiMoneyMask', require('./money/money')) diff --git a/src/global/money/money.js b/src/global/money/money.js index 2b1f51f8..78ead721 100644 --- a/src/global/money/money.js +++ b/src/global/money/money.js @@ -1,9 +1,10 @@ 'use strict'; var StringMask = require('string-mask'); -var validators = require('../../libs/validators'); +var validators = require('../../helpers/validators'); +var PreFormatters = require('../../helpers/pre-formatters'); -function MoneyMaskDirective($locale, $parse, PreFormatters) { +function MoneyMaskDirective($locale, $parse) { return { restrict: 'A', require: 'ngModel', @@ -160,6 +161,6 @@ function MoneyMaskDirective($locale, $parse, PreFormatters) { } }; } -MoneyMaskDirective.$inject = ['$locale', '$parse', 'PreFormatters']; +MoneyMaskDirective.$inject = ['$locale', '$parse']; module.exports = MoneyMaskDirective; diff --git a/src/global/number/number.js b/src/global/number/number.js index 9c0f6e70..f4473edb 100644 --- a/src/global/number/number.js +++ b/src/global/number/number.js @@ -1,8 +1,10 @@ 'use strict'; -var validators = require('../../libs/validators'); +var validators = require('../../helpers/validators'); +var NumberMasks = require('../../helpers/number-mask-builder'); +var PreFormatters = require('../../helpers/pre-formatters'); -function NumberMaskDirective($locale, $parse, PreFormatters, NumberMasks) { +function NumberMaskDirective($locale, $parse) { return { restrict: 'A', require: 'ngModel', @@ -111,6 +113,6 @@ function NumberMaskDirective($locale, $parse, PreFormatters, NumberMasks) { } }; } -NumberMaskDirective.$inject = ['$locale', '$parse', 'PreFormatters', 'NumberMasks']; +NumberMaskDirective.$inject = ['$locale', '$parse']; module.exports = NumberMaskDirective; diff --git a/src/global/percentage/percentage.js b/src/global/percentage/percentage.js index adb2b6e2..4d8a7c14 100644 --- a/src/global/percentage/percentage.js +++ b/src/global/percentage/percentage.js @@ -1,8 +1,10 @@ 'use strict'; -var validators = require('../../libs/validators'); +var validators = require('../../helpers/validators'); +var NumberMasks = require('../../helpers/number-mask-builder'); +var PreFormatters = require('../../helpers/pre-formatters'); -function PercentageMaskDirective($locale, $parse, PreFormatters, NumberMasks) { +function PercentageMaskDirective($locale) { function preparePercentageToFormatter(value, decimals, modelMultiplier) { return PreFormatters.clearDelimitersAndLeadingZeros((parseFloat(value)*modelMultiplier).toFixed(decimals)); } @@ -147,6 +149,6 @@ function PercentageMaskDirective($locale, $parse, PreFormatters, NumberMasks) { } }; } -PercentageMaskDirective.$inject = ['$locale', '$parse', 'PreFormatters', 'NumberMasks']; +PercentageMaskDirective.$inject = ['$locale']; module.exports = PercentageMaskDirective; diff --git a/src/global/time/time.js b/src/global/time/time.js index 81fe8db0..70ef306a 100644 --- a/src/global/time/time.js +++ b/src/global/time/time.js @@ -2,6 +2,7 @@ var StringMask = require('string-mask'); +//TODO: refactor to use the mask factory module.exports = function TimeMaskDirective() { return { restrict: 'A', diff --git a/src/helpers.js b/src/helpers.js deleted file mode 100644 index 958ad315..00000000 --- a/src/helpers.js +++ /dev/null @@ -1,59 +0,0 @@ -'use strict'; - -var StringMask = require('string-mask'); - -var m = angular.module('ui.utils.masks.helpers', []); - -module.exports = m.name; - -m.factory('PreFormatters', [function() { - function clearDelimitersAndLeadingZeros(value) { - if (value === '0') { - return '0'; - } - - var cleanValue = value.toString().replace(/^-/,'').replace(/^0*/, ''); - return cleanValue.replace(/[^0-9]/g, ''); - } - - function prepareNumberToFormatter(value, decimals) { - return clearDelimitersAndLeadingZeros((parseFloat(value)).toFixed(decimals)); - } - - return { - clearDelimitersAndLeadingZeros: clearDelimitersAndLeadingZeros, - prepareNumberToFormatter: prepareNumberToFormatter - }; -}]) - .factory('NumberMasks', [function() { - return { - viewMask: function(decimals, decimalDelimiter, thousandsDelimiter) { - var mask = '#' + thousandsDelimiter + '##0'; - - if (decimals > 0) { - mask += decimalDelimiter; - for (var i = 0; i < decimals; i++) { - mask += '0'; - } - } - - return new StringMask(mask, { - reverse: true - }); - }, - modelMask: function(decimals) { - var mask = '###0'; - - if (decimals > 0) { - mask += '.'; - for (var i = 0; i < decimals; i++) { - mask += '0'; - } - } - - return new StringMask(mask, { - reverse: true - }); - } - }; - }]); diff --git a/src/libs/mask-factory.js b/src/helpers/mask-factory.js similarity index 100% rename from src/libs/mask-factory.js rename to src/helpers/mask-factory.js diff --git a/src/helpers/number-mask-builder.js b/src/helpers/number-mask-builder.js new file mode 100644 index 00000000..aa5fac01 --- /dev/null +++ b/src/helpers/number-mask-builder.js @@ -0,0 +1,38 @@ +'use strict'; + +var StringMask = require('string-mask'); + +function viewMask(decimals, decimalDelimiter, thousandsDelimiter) { + var mask = '#' + thousandsDelimiter + '##0'; + + if (decimals > 0) { + mask += decimalDelimiter; + for (var i = 0; i < decimals; i++) { + mask += '0'; + } + } + + return new StringMask(mask, { + reverse: true + }); +} + +function modelMask(decimals) { + var mask = '###0'; + + if (decimals > 0) { + mask += '.'; + for (var i = 0; i < decimals; i++) { + mask += '0'; + } + } + + return new StringMask(mask, { + reverse: true + }); +} + +module.exports = { + viewMask: viewMask, + modelMask: modelMask +}; diff --git a/src/helpers/pre-formatters.js b/src/helpers/pre-formatters.js new file mode 100644 index 00000000..9d52fedb --- /dev/null +++ b/src/helpers/pre-formatters.js @@ -0,0 +1,19 @@ +'use strict'; + +function clearDelimitersAndLeadingZeros(value) { + if (value === '0') { + return '0'; + } + + var cleanValue = value.toString().replace(/^-/,'').replace(/^0*/, ''); + return cleanValue.replace(/[^0-9]/g, ''); +} + +function prepareNumberToFormatter(value, decimals) { + return clearDelimitersAndLeadingZeros((parseFloat(value)).toFixed(decimals)); +} + +module.exports = { + clearDelimitersAndLeadingZeros: clearDelimitersAndLeadingZeros, + prepareNumberToFormatter: prepareNumberToFormatter +}; diff --git a/src/libs/validators.js b/src/helpers/validators.js similarity index 100% rename from src/libs/validators.js rename to src/helpers/validators.js diff --git a/src/us/phone/us-phone.js b/src/us/phone/us-phone.js index 012cc010..786f3b9d 100644 --- a/src/us/phone/us-phone.js +++ b/src/us/phone/us-phone.js @@ -1,7 +1,7 @@ 'use strict'; var StringMask = require('string-mask'); -var maskFactory = require('../../libs/mask-factory'); +var maskFactory = require('../../helpers/mask-factory'); var phoneMaskUS = new StringMask('(000) 000-0000'), phoneMaskINTL = new StringMask('+00-00-000-000000'); diff --git a/src/us/us-masks.js b/src/us/us-masks.js index 52be7a88..a3975b07 100644 --- a/src/us/us-masks.js +++ b/src/us/us-masks.js @@ -1,8 +1,6 @@ 'use strict'; -var m = angular.module('ui.utils.masks.us', [ - require('../helpers') -]) +var m = angular.module('ui.utils.masks.us', []) .directive('uiUsPhoneNumber', require('./phone/us-phone')); module.exports = m.name;