Add Limit formatter
* src/validate/formatter/insurance/Limit.js: Added * test/validate/formatter/insurance/LimitTest.js: Addedmaster
parent
c0df3ef02e
commit
31f3ab010e
|
@ -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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} );
|
|
@ -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" ],
|
||||||
|
} );
|
||||||
|
} );
|
Loading…
Reference in New Issue