diff --git a/README.md b/README.md index 4e0db00..e71fda0 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ On [Packagist](https://packagist.org/packages/data-values/javascript): ## Release notes ### 0.9.0 (dev) +* Removed `valueFormatters.ValueFormatterStore`. * Removed `dataValues.TimeValue.getYear`, `getMonth`, `getDay`, `getHour`, `getMinute`, and `getSecond`. * Declared `globeCoordinate.GlobeCoordinate.getDecimal` private. diff --git a/config.js b/config.js index 65342c3..87ea98c 100644 --- a/config.js +++ b/config.js @@ -107,10 +107,6 @@ this.config = ( function() { 'valueFormatters/valueFormatters': { exports: 'valueFormatters' }, - 'valueFormatters/ValueFormatterStore': { - exports: 'valueFormatters.ValueFormatterStore', - deps: [ 'valueFormatters/valueFormatters', 'jquery' ] - }, 'formatters/NullFormatter': [ 'valueFormatters/valueFormatters', @@ -219,7 +215,6 @@ this.config = ( function() { 'tests/src/values/UnDeserializableValue.tests', 'tests/src/valueFormatters/valueFormatters.tests', - 'tests/src/valueFormatters/ValueFormatterStore.tests', 'tests/src/valueFormatters/formatters/NullFormatter.tests', 'tests/src/valueFormatters/formatters/StringFormatter.tests', diff --git a/src/valueFormatters/ValueFormatterStore.js b/src/valueFormatters/ValueFormatterStore.js deleted file mode 100644 index 09af5fb..0000000 --- a/src/valueFormatters/ValueFormatterStore.js +++ /dev/null @@ -1,132 +0,0 @@ -( function( $, vf ) { - 'use strict'; - -/** - * @ignore - * - * @param {Function} Formatter - * - * @throws {Error} if the provided argument is not a valueFormatters.ValueFormatter constructor. - */ -function assertIsValueFormatterConstructor( Formatter ) { - if( !( $.isFunction( Formatter ) && Formatter.prototype instanceof vf.ValueFormatter ) ) { - throw new Error( 'Invalid ValueFormatter constructor' ); - } -} - -/** - * Store managing ValueFormatter instances. - * @class valueFormatters.ValueFormatterStore - * @since 0.1 - * @license GPL-2.0+ - * @author H. Snater < mediawiki@snater.com > - * - * @constructor - * - * @param {Function|null} [DefaultFormatter=null] Constructor of a default formatter that shall be - * returned when no formatter is registered for a specific purpose. - */ -var SELF = vf.ValueFormatterStore = function VpValueFormatterStore( DefaultFormatter ) { - this._DefaultFormatter = DefaultFormatter || null; - this._formattersForDataTypes = {}; - this._formattersForDataValueTypes = {}; -}; - -$.extend( SELF.prototype, { - /** - * Default formatter constructor to be returned when no formatter is registered for a specific - * purpose. - * @property {Function|null} - * @private - */ - _DefaultFormatter: null, - - /** - * @property {Object} - * @private - */ - _formattersForDataTypes: null, - - /** - * @property {Object} - * @private - */ - _formattersForDataValueTypes: null, - - /** - * Registers a formatter for a certain data type. - * - * @param {Function} Formatter - * @param {string} dataTypeId - * - * @throws {Error} if the data type id is omitted. - * @throws {Error} if a formatter for the specified data type id is registered already. - */ - registerDataTypeFormatter: function( Formatter, dataTypeId ) { - assertIsValueFormatterConstructor( Formatter ); - - if( dataTypeId === undefined ) { - throw new Error( 'No proper data type id provided to register the formatter for' ); - } - - if( this._formattersForDataTypes[dataTypeId] ) { - throw new Error( 'Formatter for data type "' + dataTypeId + '" is registered ' - + 'already' ); - } - - this._formattersForDataTypes[dataTypeId] = Formatter; - }, - - /** - * Registers a formatter for a certain data value type. - * - * @param {Function} Formatter - * @param {string} dataValueType - * - * @throws {Error} if no data type id is specified. - * @throws {Error} if a formatter for the specified data value type is registered already. - */ - registerDataValueFormatter: function( Formatter, dataValueType ) { - assertIsValueFormatterConstructor( Formatter ); - - if( dataValueType === undefined ) { - throw new Error( 'No proper data value type provided to register the formatter ' - + 'for' ); - } - - if( this._formattersForDataValueTypes[dataValueType] ) { - throw new Error( 'Formatter for DataValue type "' + dataValueType + '" is ' - + 'registered already' ); - } - - this._formattersForDataValueTypes[dataValueType] = Formatter; - }, - - /** - * Returns the ValueFormatter constructor registered for the specified purpose or the default - * formatter if no ValueFormatter is registered for that purpose. - * - * @param {string} dataValueType - * @param {string} [dataTypeId] - * @return {Function|null} - * - * @throws {Error} if no proper purpose is provided to retrieve a formatter. - */ - getFormatter: function( dataValueType, dataTypeId ) { - var formatter; - - if( typeof dataTypeId === 'string' ) { - formatter = this._formattersForDataTypes[dataTypeId]; - } - - if( !formatter && typeof dataValueType === 'string' ) { - formatter = this._formattersForDataValueTypes[dataValueType]; - } else if( !formatter ) { - throw new Error( 'No sufficient purpose provided for choosing a formatter' ); - } - - return formatter || this._DefaultFormatter; - } -} ); - -}( jQuery, valueFormatters ) ); diff --git a/src/valueFormatters/resources.php b/src/valueFormatters/resources.php index 633eb2e..1074620 100644 --- a/src/valueFormatters/resources.php +++ b/src/valueFormatters/resources.php @@ -31,15 +31,6 @@ ], ], - 'valueFormatters.ValueFormatterStore' => $moduleTemplate + [ - 'scripts' => [ - 'ValueFormatterStore.js', - ], - 'dependencies' => [ - 'valueFormatters', - ], - ], - 'valueFormatters.formatters' => $moduleTemplate + [ 'scripts' => [ 'formatters/NullFormatter.js', diff --git a/tests/src/valueFormatters/ValueFormatterStore.tests.js b/tests/src/valueFormatters/ValueFormatterStore.tests.js deleted file mode 100644 index 564ca08..0000000 --- a/tests/src/valueFormatters/ValueFormatterStore.tests.js +++ /dev/null @@ -1,275 +0,0 @@ -/** - * @license GPL-2.0+ - * @author H. Snater < mediawiki@snater.com > - */ -define( [ - 'valueFormatters/valueFormatters', - 'dataValues/dataValues', - 'jquery', - 'qunit', - 'formatters/NullFormatter', - 'formatters/StringFormatter', - 'valueFormatters/ValueFormatterStore', - 'values/NumberValue', - 'values/StringValue', - 'values/UnknownValue', - 'qunit.parameterize' -], function( vf, dv, $, QUnit ) { - 'use strict'; - - var DataTypeMock = function( dataTypeId, DataValue ) { - this._dataTypeId = dataTypeId; - this._dataValueType = DataValue.TYPE; - }; - $.extend( DataTypeMock.prototype, { - getId: function() { - return this._dataTypeId; - }, - getDataValueType: function() { - return this._dataValueType; - } - } ); - - /** - * Returns a descriptive string to be used as id when registering a ValueFormatter in a - * ValueFormatterStore. - * - * @param {DataTypeMock|Function} purpose - * @return {string} - */ - function getTypeInfo( purpose ) { - if( purpose instanceof DataTypeMock ) { - return 'DataType with data value type "' + purpose.getDataValueType() + '"'; - } - return 'constructor for DataValue of type "' + purpose.TYPE + '"'; - } - - var StringValue = dv.StringValue, - UnknownValue = dv.UnknownValue, - stringType = new DataTypeMock( 'somestringtype', StringValue ), - numberType = new DataTypeMock( 'somenumbertype', dv.NumberValue ), - StringFormatter = vf.StringFormatter, - NullFormatter = vf.NullFormatter; - - QUnit.module( 'valueFormatters.ValueFormatterStore' ); - - QUnit.test( 'Constructor', function( assert ) { - var formatterStore = new vf.ValueFormatterStore(); - - assert.ok( - formatterStore instanceof vf.ValueFormatterStore, - 'Instantiated ValueFormatterStore.' - ); - } ); - - QUnit.test( 'registerDataTypeFormatter(): Error handling', function( assert ) { - var formatterStore = new vf.ValueFormatterStore(); - - assert['throws']( - function() { - formatterStore.registerDataTypeFormatter( 'invalid', stringType.getId() ); - }, - 'Failed trying to register an invalid formatter constructor.' - ); - - formatterStore.registerDataTypeFormatter( StringFormatter, stringType.getId() ); - - assert['throws']( - function() { - formatterStore.getFormatter( stringType ); - }, - 'Failed trying to get a formatter with an invalid purpose.' - ); - } ); - - QUnit.test( 'registerDataValueFormatter(): Error handling', function( assert ) { - var formatterStore = new vf.ValueFormatterStore(); - - assert['throws']( - function() { - formatterStore.registerDataValueFormatter( 'invalid', StringValue.TYPE ); - }, - 'Failed trying to register an invalid formatter constructor.' - ); - - formatterStore.registerDataValueFormatter( StringFormatter, StringValue.TYPE ); - - assert['throws']( - function() { - formatterStore.getFormatter( StringValue ); - }, - 'Failed trying to get a formatter with an invalid purpose.' - ); - } ); - - QUnit.test( 'Return default formatter on getFormatter()', function( assert ) { - var formatterStore = new vf.ValueFormatterStore( NullFormatter ); - - assert.equal( - formatterStore.getFormatter( StringValue.TYPE ), - NullFormatter, - 'Returning default formatter if no formatter is registered for a specific data value.' - ); - - assert.equal( - formatterStore.getFormatter( stringType.getDataValueType(), stringType.getId() ), - NullFormatter, - 'Returning default formatter if no formatter is registered for a specific data type.' - ); - - formatterStore.registerDataValueFormatter( StringFormatter, StringValue.TYPE ); - - assert.equal( - formatterStore.getFormatter( StringValue.TYPE ), - StringFormatter, - 'Returning specific formatter if a formatter is registered for a specific data value.' - ); - - assert.equal( - formatterStore.getFormatter( UnknownValue.TYPE ), - NullFormatter, - 'Still returning default formatter if no formatter is registered for a specific data ' - + 'value.' - ); - - assert.equal( - formatterStore.getFormatter( numberType.getDataValueType(), numberType.getId() ), - NullFormatter, - 'Still returning default formatter if no formatter is registered for a specific data ' - + 'type.' - ); - } ); - - // Tests regarding registration of formatters: - - /** - * Array of test definitions used as provider for "valueFormatterStoreRegistrationTest". - * @property {Object[]} - */ - var valueFormatterStoreRegistrationTestCases = [ - { - title: 'Empty ValueFormatterStore', - register: [], - expect: [ - [ StringValue, null ], - [ stringType, null ] - ] - }, - { - title: 'Store with formatter for string DataValue which is also suitable for string ' - + 'DataType', - register: [ - [ StringValue, StringFormatter ] - ], - expect: [ - [ StringValue, StringFormatter ], - [ stringType, StringFormatter ], // data type uses value type - [ UnknownValue, null ], - [ numberType, null ] - ] - }, - { - title: 'Store for string DataType. String DataValue can\'t use this potentially more ' - + 'specialized formatter', - register: [ - [ stringType, StringFormatter ] - ], - expect: [ - [ StringValue, null ], - [ stringType, StringFormatter ] - ] - }, - { - title: 'Store with two formatters: For DataValue and for DataType using that ' - + 'DataValue type', - register: [ - [ StringValue, StringFormatter ], - [ stringType, StringFormatter ] - ], - expect: [ - [ StringValue, StringFormatter ], - [ stringType, StringFormatter ], - [ UnknownValue, null ] - ] - }, - { - title: 'Store with two formatters for two different DataValue types', - register: [ - [ StringValue, StringFormatter ], - [ UnknownValue, NullFormatter ] - ], - expect: [ - [ StringValue, StringFormatter ], - [ UnknownValue, NullFormatter ], - [ numberType, null ] - ] - } - ]; - - /** - * Test for registration of ValueFormatters to ValueFormatterStore and expected conditions - * afterwards. - * - * @param {QUnit.assert} assert - * @param {Array[]} toRegister Array containing arrays where each one tells a - * ValueFormatterStore what formatters to register. The inner array has to consist out - * of two objects, a formatter constructor and a DataValue constructor or a DataTypeMock - * object. - * @param {Array[]} toExpect Array containing arrays where each one states one expected - * condition of the ValueFormatterStore after registration of what is given in the first - * parameter. Each inner array should contain a data type, data value or data value - * constructor and a ValueFormatter which is expected to be registered for it. - */ - function valueFormatterStoreRegistrationTest( assert, toRegister, toExpect ) { - var formatterStore = new vf.ValueFormatterStore(); - - // Register ValueFormatters as per definition: - $.each( toRegister, function( i, registerPair ) { - var purpose = registerPair[0], - Formatter = registerPair[1]; - - if( purpose instanceof DataTypeMock ) { - formatterStore.registerDataTypeFormatter( Formatter, purpose.getId() ); - } else { - formatterStore.registerDataValueFormatter( Formatter, purpose.TYPE ); - } - - assert.ok( - true, - 'Registered formatter for ' + getTypeInfo( purpose ) - ); - } ); - - // Check for expected conditions: - $.each( toExpect, function( i, expectPair ) { - var purpose = expectPair[0], - Formatter = expectPair[1], - RetrievedFormatter; - - if( purpose instanceof DataTypeMock ) { - RetrievedFormatter = formatterStore.getFormatter( - purpose.getDataValueType(), purpose.getId() - ); - } else { - RetrievedFormatter = formatterStore.getFormatter( purpose.TYPE ); - } - - assert.strictEqual( - RetrievedFormatter, - Formatter, - 'Requesting formatter for ' + getTypeInfo( purpose ) + - ( Formatter !== null ? ' returns expected formatter' : ' returns null' ) - ); - } ); - } - - QUnit - .cases( valueFormatterStoreRegistrationTestCases ) - .test( - 'registerDataTypeFormatter() / registerDataValueFormatter() & getFormatter() ', - function( params, assert ) { - valueFormatterStoreRegistrationTest( assert, params.register, params.expect ); - } - ); - -} );