From c0df3ef02e8f2d55b4ada57b447b25ff41b472b5 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 24 Jun 2016 15:19:43 -0400 Subject: [PATCH] Add Number formatter * src/validate/formatter/Number.js: Added * test/validate/formatter/NumberTest.js: Added --- src/validate/formatter/Number.js | 88 +++++++++++++++++++++++++++ test/validate/formatter/NumberTest.js | 50 +++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/validate/formatter/Number.js create mode 100644 test/validate/formatter/NumberTest.js diff --git a/src/validate/formatter/Number.js b/src/validate/formatter/Number.js new file mode 100644 index 0000000..4adf3c8 --- /dev/null +++ b/src/validate/formatter/Number.js @@ -0,0 +1,88 @@ +/** + * Number formatter + * + * Copyright (C) 2016 LoVullo Associates, Inc. + * + * This file is part of liza. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var Trait = require( 'easejs' ).Trait, + ValidatorFormatter = require( '../ValidatorFormatter' ); + + +/** + * Formats insurance limit(s) + */ +module.exports = Trait( 'Number' ) + .implement( ValidatorFormatter ) + .extend( +{ + /** + * Parse item as a number + * + * @param {string} data data to parse + * + * @return {string} data formatted for storage + */ + 'virtual abstract override public parse': function( data ) + { + return this.__super( data ).replace( /[ ,]/g, '' ); + }, + + + /** + * Format number with thousands separators + * + * @param {string} data data to format for display + * + * @return {string} data formatted for display + */ + 'virtual abstract override public retrieve': function( data ) + { + return this.styleNumber( this.__super( data ) ); + }, + + + /** + * Style number with thousands separators + * + * @param {string} number number to style (digits only) + * + * @return {string} formatted number + */ + 'virtual protected styleNumber': function( number ) + { + var i = number.length, + ret = [], + chunk = ''; + + do + { + i -= 3; + + // the second argument will adjust the length of the chunk + // if I is negative (e.g. 3 + -2 = 1) + chunk = number.substr( + ( i < 0 ) ? 0 : i, + Math.min( 3, 3 + i ) + ); + + ret.unshift( chunk ); + } while ( i > 0 ); + + return ret.join( ',' ); + } +} ); \ No newline at end of file diff --git a/test/validate/formatter/NumberTest.js b/test/validate/formatter/NumberTest.js new file mode 100644 index 0000000..74d2595 --- /dev/null +++ b/test/validate/formatter/NumberTest.js @@ -0,0 +1,50 @@ +/** + * Number formatter test + * + * Copyright (C) 2016 LoVullo Associates, Inc. + * + * This file is part of liza. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var liza = require( '../../../' ), + Sut = liza.validate.formatter.Number, + EchoFormatter = liza.validate.formatter.EchoFormatter, + common = require( './common' ); + + +describe( 'validate.formatter.Number', function() +{ + common.testValidate( EchoFormatter.use( Sut )(), { + "1": [ "1", "1" ], + "123": [ "123", "123" ], + "12345": [ "12345", "12,345" ], + "12,345": [ "12345", "12,345" ], + ",12,345": [ "12345", "12,345" ], + "12,345,": [ "12345", "12,345" ], + " 12,345 ,": [ "12345", "12,345" ], + " 1, ,": [ "1", "1" ], + } ); + + + common.testMixin( + EchoFormatter, + Sut, + '123', + '4567', + '1234567', + '1,234,567' + ); +} );