Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <gerwitm@lovullo.com>2016-06-22 15:59:12 -0400
committerMike Gerwitz <gerwitm@lovullo.com>2016-06-22 15:59:12 -0400
commitd6cecf1ff6d086dcdb2773a232faefcdc1195e04 (patch)
tree3f8e03c64f8fc9ccb72135ed9d66410937bac97c
parenta509e53a3e3216acb0e78a1c4abe35c7dade8914 (diff)
downloadliza-d6cecf1ff6d086dcdb2773a232faefcdc1195e04.tar.gz
liza-d6cecf1ff6d086dcdb2773a232faefcdc1195e04.tar.bz2
liza-d6cecf1ff6d086dcdb2773a232faefcdc1195e04.zip
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.
-rw-r--r--src/validate/ValidatorFormatter.js57
-rw-r--r--src/validate/formatter/PatternFormatter.js (renamed from src/validate/VFormat.js)12
-rw-r--r--test/validate/formatter/PatternFormatterTest.js (renamed from test/validate/VFormatTest.js)17
3 files changed, 78 insertions, 8 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+
+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
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
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 <http://www.gnu.org/licenses/>.
*/
-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()