From d6cecf1ff6d086dcdb2773a232faefcdc1195e04 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 22 Jun 2016 15:59:12 -0400 Subject: [PATCH] VFormat => PatternFormatter with added interface * src/validate/ValidatorFormatter.js: Interface added. * src/validate/formatter/PatternFormatter.js: Renamed from ../VFormat.js. * test/validate/formatter/PatternFormatterTest.js: Renamed from ../VFormatTest.js. --- src/validate/ValidatorFormatter.js | 57 +++++++++++++++++++ .../PatternFormatter.js} | 12 ++-- .../PatternFormatterTest.js} | 17 ++++-- 3 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 src/validate/ValidatorFormatter.js rename src/validate/{VFormat.js => formatter/PatternFormatter.js} (92%) rename test/validate/{VFormatTest.js => formatter/PatternFormatterTest.js} (89%) diff --git a/src/validate/ValidatorFormatter.js b/src/validate/ValidatorFormatter.js new file mode 100644 index 0000000..bdc1318 --- /dev/null +++ b/src/validate/ValidatorFormatter.js @@ -0,0 +1,57 @@ +/** + * Validator-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 Interface = require( 'easejs' ).Interface; + + +module.exports = Interface( 'ValidatorFormatter', +{ + /** + * Format the given data or fail if no match is found + * + * If the given data matches a pattern, it will be formatted with + * respect to the first matched pattern. Otherwise, an error will + * be thrown indicating a validation failure. + * + * @param {string} data data to parse + * + * @return {string} formatted string, if a match is found + */ + 'public parse': [ 'data' ], + + + /** + * Retrieve data that may require formatting for display + * + * Return formatting is optional. No formatting will be done if no pattern + * was given when the instance was constructed. + * + * To ensure consistency and correctness, *any data returned by this method + * must be reversible* --- that is, parse( retrieve( data ) ) should not + * throw an exception. + * + * @param {string} data data to format for display + * + * @return {string} data formatted for display + */ + 'public retrieve': [ 'data' ] +} ); diff --git a/src/validate/VFormat.js b/src/validate/formatter/PatternFormatter.js similarity index 92% rename from src/validate/VFormat.js rename to src/validate/formatter/PatternFormatter.js index 1b8842f..43fccea 100644 --- a/src/validate/VFormat.js +++ b/src/validate/formatter/PatternFormatter.js @@ -1,5 +1,5 @@ /** - * Validator-formatter + * Pattern-based validator-formatter * * Copyright (C) 2016 LoVullo Associates, Inc. * @@ -20,13 +20,17 @@ */ -var Class = require( 'easejs' ).Class; +var Class = require( 'easejs' ).Class, + ValidatorFormatter = require( '../ValidatorFormatter' ); /** - * Data validation and formatting + * Data validation and formatting based on patterns and their + * replacements */ -module.exports = Class( 'VFormat', +module.exports = Class( 'VFormat' ) + .implement( ValidatorFormatter ) + .extend( { /** * Pattern definition diff --git a/test/validate/VFormatTest.js b/test/validate/formatter/PatternFormatterTest.js similarity index 89% rename from test/validate/VFormatTest.js rename to test/validate/formatter/PatternFormatterTest.js index 160822e..749d428 100644 --- a/test/validate/VFormatTest.js +++ b/test/validate/formatter/PatternFormatterTest.js @@ -1,5 +1,5 @@ /** - * Test validator-formatter + * Test pattern-based validator-formatter * * Copyright (C) 2016 LoVullo Associates, Inc. * @@ -19,8 +19,10 @@ * along with this program. If not, see . */ -var liza = require( '../../' ), - Sut = liza.validate.VFormat, +var liza = require( '../../../' ), + Sut = liza.validate.formatter.PatternFormatter, + VFmt = liza.validate.ValidatorFormatter, + Class = require( 'easejs' ).Class, expect = require( 'chai' ).expect, assert = require( 'assert' ), dfn = [ @@ -30,8 +32,15 @@ var liza = require( '../../' ), ]; -describe( 'VFormat', function() +describe( 'PatternFormatter', function() { + it( 'is a ValidatorFormatter', function() + { + expect( Class.isA( VFmt, createSut( [] ) ) ) + .to.be.true; + } ); + + describe( '#parse', function() { it( 'formats string according to given definition', function()