From 31f3ab010edc4bdbb22b5d47d386b45d882a6fa5 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 24 Jun 2016 15:55:51 -0400 Subject: [PATCH] Add Limit formatter * src/validate/formatter/insurance/Limit.js: Added * test/validate/formatter/insurance/LimitTest.js: Added --- src/validate/formatter/insurance/Limit.js | 70 +++++++++++++++++++ .../validate/formatter/insurance/LimitTest.js | 68 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/validate/formatter/insurance/Limit.js create mode 100644 test/validate/formatter/insurance/LimitTest.js diff --git a/src/validate/formatter/insurance/Limit.js b/src/validate/formatter/insurance/Limit.js new file mode 100644 index 0000000..2664b47 --- /dev/null +++ b/src/validate/formatter/insurance/Limit.js @@ -0,0 +1,70 @@ +/** + * Insurance limit 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( 'Limit' ) + .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 ( isNaN( parseInt( data ) ) ) + ? data + : this.__super( data ); + }, + + + /** + * 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 ) + { + var number = parseFloat( data ), + is_numeric = !isNaN( number ); + + if ( !is_numeric ) + { + return data; + } + + return this.__super( + ( number < 1000 ) + ? ''+( number * 1000 ) + : data + ); + } +} ); diff --git a/test/validate/formatter/insurance/LimitTest.js b/test/validate/formatter/insurance/LimitTest.js new file mode 100644 index 0000000..33b7e5e --- /dev/null +++ b/test/validate/formatter/insurance/LimitTest.js @@ -0,0 +1,68 @@ +/** + * Tests insurance limit 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 Class = require( 'easejs' ).Class, + liza = require( '../../../../' ), + Sut = liza.validate.formatter.insurance.Limit, + EchoFormatter = liza.validate.formatter.EchoFormatter, + ValidatorFormatter = liza.validate.ValidatorFormatter, + common = require( '../common' ), + expect = require( 'chai' ).expect; + +var DummyFormatter = Class.implement( ValidatorFormatter ) + .extend( +{ + 'virtual parse': function( data ) + { + return '+' + data; + }, + + 'virtual retrieve': function( data ) + { + return '-' + data; + }, +} ); + + +describe( 'validate.formatter.insurance.Limit', function() +{ + common.testValidate( DummyFormatter.use( Sut )(), { + // plain strings are ignored (and do not invoke supertype) + "": [ "" ], + "abc": [ "abc" ], + "abc_": [ "abc_" ], + "_ -": [ "_ -" ], + + // numbers >3 digits echoed + "1234": [ "+1234", "-+1234" ], + "123456": [ "+123456", "-+123456" ], + } ); + + + // 3-digit abbreviations are converted *on retrieval* (we'll often + // be styling existing data, so we don't want to rely on prior + // conversion) + common.testValidate( EchoFormatter.use( Sut )(), { + "100": [ "100", "100000" ], + "500": [ "500", "500000" ], + } ); +} );