Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <gerwitm@lovullo.com>2016-06-24 15:55:51 -0400
committerMike Gerwitz <gerwitm@lovullo.com>2016-06-27 16:51:25 -0400
commit31f3ab010edc4bdbb22b5d47d386b45d882a6fa5 (patch)
treec8330a25cff858fef689e36e7438f5bbf3ed5b3f
parentc0df3ef02e8f2d55b4ada57b447b25ff41b472b5 (diff)
downloadliza-31f3ab010edc4bdbb22b5d47d386b45d882a6fa5.tar.gz
liza-31f3ab010edc4bdbb22b5d47d386b45d882a6fa5.tar.bz2
liza-31f3ab010edc4bdbb22b5d47d386b45d882a6fa5.zip
Add Limit formatter
* src/validate/formatter/insurance/Limit.js: Added * test/validate/formatter/insurance/LimitTest.js: Added
-rw-r--r--src/validate/formatter/insurance/Limit.js70
-rw-r--r--test/validate/formatter/insurance/LimitTest.js68
2 files changed, 138 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <http://www.gnu.org/licenses/>.
+ */
+
+
+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" ],
+ } );
+} );