From 8643fdcc91b8ebc07d0e0b74ef832f687e3eb5c7 Mon Sep 17 00:00:00 2001 From: josdejong Date: Thu, 20 Jun 2013 13:56:13 +0200 Subject: [PATCH] Renamed DataSet option fieldTypes to convert, and renamed util.cast to util.convert --- src/DataSet.js | 82 +++++++++---------- src/DataView.js | 2 +- src/graph/dotparser.js | 6 +- src/timeline/Range.js | 4 +- src/timeline/Timeline.js | 2 +- src/timeline/component/GroupSet.js | 2 +- src/timeline/component/TimeAxis.js | 4 +- src/util.js | 24 +++--- test/dataset.html | 2 +- test/dataset.js | 8 +- test/timeline.html | 2 +- vis.js | 126 ++++++++++++++--------------- vis.min.js | 10 +-- 13 files changed, 137 insertions(+), 137 deletions(-) diff --git a/src/DataSet.js b/src/DataSet.js index 6e2b5733..fb274a44 100644 --- a/src/DataSet.js +++ b/src/DataSet.js @@ -4,7 +4,7 @@ * Usage: * var dataSet = new DataSet({ * fieldId: '_id', - * fieldTypes: { + * convert: { * // ... * } * }); @@ -29,7 +29,7 @@ * @param {Object} [options] Available options: * {String} fieldId Field name of the id in the * items, 'id' by default. - * {Object.} [fieldTypes] + * {Object.} [convert] * {String[]} [fields] field names to be returned * {function} [filter] filter items * {String | function} [order] Order the items by @@ -319,14 +319,14 @@ DataSet.prototype.get = function (args) { } // build options - var fieldTypes = options && options.fieldTypes || this.options.fieldTypes; + var convert = options && options.convert || this.options.convert; var filter = options && options.filter; var items = [], item, itemId, i, len; - // cast items + // convert items if (id != undefined) { // return a single item - item = me._getItem(id, fieldTypes); + item = me._getItem(id, convert); if (filter && !filter(item)) { item = null; } @@ -334,7 +334,7 @@ DataSet.prototype.get = function (args) { else if (ids != undefined) { // return a subset of items for (i = 0, len = ids.length; i < len; i++) { - item = me._getItem(ids[i], fieldTypes); + item = me._getItem(ids[i], convert); if (!filter || filter(item)) { items.push(item); } @@ -344,7 +344,7 @@ DataSet.prototype.get = function (args) { // return all items for (itemId in this.data) { if (this.data.hasOwnProperty(itemId)) { - item = me._getItem(itemId, fieldTypes); + item = me._getItem(itemId, convert); if (!filter || filter(item)) { items.push(item); } @@ -420,7 +420,7 @@ DataSet.prototype.getIds = function (options) { var data = this.data, filter = options && options.filter, order = options && options.order, - fieldTypes = options && options.fieldTypes || this.options.fieldTypes, + convert = options && options.convert || this.options.convert, i, len, id, @@ -435,7 +435,7 @@ DataSet.prototype.getIds = function (options) { items = []; for (id in data) { if (data.hasOwnProperty(id)) { - item = this._getItem(id, fieldTypes); + item = this._getItem(id, convert); if (filter(item)) { items.push(item); } @@ -452,7 +452,7 @@ DataSet.prototype.getIds = function (options) { // create unordered list for (id in data) { if (data.hasOwnProperty(id)) { - item = this._getItem(id, fieldTypes); + item = this._getItem(id, convert); if (filter(item)) { ids.push(item[this.fieldId]); } @@ -496,7 +496,7 @@ DataSet.prototype.getIds = function (options) { * The order of the items is not determined. * @param {function} callback * @param {Object} [options] Available options: - * {Object.} [fieldTypes] + * {Object.} [convert] * {String[]} [fields] filter fields * {function} [filter] filter items * {String | function} [order] Order the items by @@ -504,7 +504,7 @@ DataSet.prototype.getIds = function (options) { */ DataSet.prototype.forEach = function (callback, options) { var filter = options && options.filter, - fieldTypes = options && options.fieldTypes || this.options.fieldTypes, + convert = options && options.convert || this.options.convert, data = this.data, item, id; @@ -523,7 +523,7 @@ DataSet.prototype.forEach = function (callback, options) { // unordered for (id in data) { if (data.hasOwnProperty(id)) { - item = this._getItem(id, fieldTypes); + item = this._getItem(id, convert); if (!filter || filter(item)) { callback(item, id); } @@ -536,7 +536,7 @@ DataSet.prototype.forEach = function (callback, options) { * Map every item in the dataset. * @param {function} callback * @param {Object} [options] Available options: - * {Object.} [fieldTypes] + * {Object.} [convert] * {String[]} [fields] filter fields * {function} [filter] filter items * {String | function} [order] Order the items by @@ -545,15 +545,15 @@ DataSet.prototype.forEach = function (callback, options) { */ DataSet.prototype.map = function (callback, options) { var filter = options && options.filter, - fieldTypes = options && options.fieldTypes || this.options.fieldTypes, + convert = options && options.convert || this.options.convert, mappedItems = [], data = this.data, item; - // cast and filter items + // convert and filter items for (var id in data) { if (data.hasOwnProperty(id)) { - item = this._getItem(id, fieldTypes); + item = this._getItem(id, convert); if (!filter || filter(item)) { mappedItems.push(callback(item, id)); } @@ -747,13 +747,13 @@ DataSet.prototype.min = function (field) { DataSet.prototype.distinct = function (field) { var data = this.data, values = [], - fieldType = this.options.fieldTypes[field], + fieldType = this.options.convert[field], count = 0; for (var prop in data) { if (data.hasOwnProperty(prop)) { var item = data[prop]; - var value = util.cast(item[field], fieldType); + var value = util.convert(item[field], fieldType); var exists = false; for (var i = 0; i < count; i++) { if (values[i] == value) { @@ -797,8 +797,8 @@ DataSet.prototype._addItem = function (item) { var d = {}; for (var field in item) { if (item.hasOwnProperty(field)) { - var type = this.fieldTypes[field]; // type may be undefined - d[field] = util.cast(item[field], type); + var fieldType = this.convert[field]; // type may be undefined + d[field] = util.convert(item[field], fieldType); } } this.data[id] = d; @@ -807,13 +807,13 @@ DataSet.prototype._addItem = function (item) { }; /** - * Get an item. Fields can be casted to a specific type + * Get an item. Fields can be converted to a specific type * @param {String} id - * @param {Object.} [fieldTypes] Cast field types + * @param {Object.} [convert] field types to convert * @return {Object | null} item * @private */ -DataSet.prototype._getItem = function (id, fieldTypes) { +DataSet.prototype._getItem = function (id, convert) { var field, value; // get the item from the dataset @@ -822,35 +822,35 @@ DataSet.prototype._getItem = function (id, fieldTypes) { return null; } - // cast the items field types - var casted = {}, + // convert the items field types + var converted = {}, fieldId = this.fieldId, internalIds = this.internalIds; - if (fieldTypes) { + if (convert) { for (field in raw) { if (raw.hasOwnProperty(field)) { value = raw[field]; // output all fields, except internal ids if ((field != fieldId) || !(value in internalIds)) { - casted[field] = util.cast(value, fieldTypes[field]); + converted[field] = util.convert(value, convert[field]); } } } } else { - // no field types specified, no casting needed + // no field types specified, no converting needed for (field in raw) { if (raw.hasOwnProperty(field)) { value = raw[field]; // output all fields, except internal ids if ((field != fieldId) || !(value in internalIds)) { - casted[field] = value; + converted[field] = value; } } } } - return casted; + return converted; }; /** @@ -875,8 +875,8 @@ DataSet.prototype._updateItem = function (item) { // merge with current item for (var field in item) { if (item.hasOwnProperty(field)) { - var type = this.fieldTypes[field]; // type may be undefined - d[field] = util.cast(item[field], type); + var fieldType = this.convert[field]; // type may be undefined + d[field] = util.convert(item[field], fieldType); } } diff --git a/src/DataView.js b/src/DataView.js index ed2c7fa2..540fef17 100644 --- a/src/DataView.js +++ b/src/DataView.js @@ -96,7 +96,7 @@ DataView.prototype.setData = function (data) { * {Object} options An Object with options. Available options: * {String} [type] Type of data to be returned. Can * be 'DataTable' or 'Array' (default) - * {Object.} [fieldTypes] + * {Object.} [convert] * {String[]} [fields] field names to be returned * {function} [filter] filter items * {String | function} [order] Order the items by diff --git a/src/graph/dotparser.js b/src/graph/dotparser.js index 7d0f5ed3..46d73d05 100644 --- a/src/graph/dotparser.js +++ b/src/graph/dotparser.js @@ -327,13 +327,13 @@ next(); } if (token == 'false') { - token = false; // cast to boolean + token = false; // convert to boolean } else if (token == 'true') { - token = true; // cast to boolean + token = true; // convert to boolean } else if (!isNaN(Number(token))) { - token = Number(token); // cast to number + token = Number(token); // convert to number } tokenType = TOKENTYPE.IDENTIFIER; return; diff --git a/src/timeline/Range.js b/src/timeline/Range.js index 526cda35..9f02fe69 100644 --- a/src/timeline/Range.js +++ b/src/timeline/Range.js @@ -139,8 +139,8 @@ Range.prototype.setRange = function(start, end) { * @private */ Range.prototype._applyRange = function(start, end) { - var newStart = (start != null) ? util.cast(start, 'Number') : this.start; - var newEnd = (end != null) ? util.cast(end, 'Number') : this.end; + var newStart = (start != null) ? util.convert(start, 'Number') : this.start; + var newEnd = (end != null) ? util.convert(end, 'Number') : this.end; var diff; // check for valid number diff --git a/src/timeline/Timeline.js b/src/timeline/Timeline.js index 3ef2e6b4..2d34e5b7 100644 --- a/src/timeline/Timeline.js +++ b/src/timeline/Timeline.js @@ -143,7 +143,7 @@ Timeline.prototype.setItems = function(items) { } if (!(items instanceof DataSet)) { newItemSet = new DataSet({ - fieldTypes: { + convert: { start: 'Date', end: 'Date' } diff --git a/src/timeline/component/GroupSet.js b/src/timeline/component/GroupSet.js index b18c1d13..ea2242b9 100644 --- a/src/timeline/component/GroupSet.js +++ b/src/timeline/component/GroupSet.js @@ -120,7 +120,7 @@ GroupSet.prototype.setGroups = function setGroups(groups) { } else { this.groupsData = new DataSet({ - fieldTypes: { + convert: { start: 'Date', end: 'Date' } diff --git a/src/timeline/component/TimeAxis.js b/src/timeline/component/TimeAxis.js index a7631d06..7b8a3535 100644 --- a/src/timeline/component/TimeAxis.js +++ b/src/timeline/component/TimeAxis.js @@ -487,8 +487,8 @@ TimeAxis.prototype.reflow = function () { // calculate range and step this._updateConversion(); - var start = util.cast(range.start, 'Date'), - end = util.cast(range.end, 'Date'), + var start = util.convert(range.start, 'Date'), + end = util.convert(range.end, 'Date'), minimumStep = this.toTime((props.minorCharWidth || 10) * 5) - this.toTime(0); this.step = new TimeStep(start, end, minimumStep); changed += update(props.range, 'start', start.valueOf()); diff --git a/src/util.js b/src/util.js index 5a5f02fe..d09a82a9 100644 --- a/src/util.js +++ b/src/util.js @@ -98,7 +98,7 @@ util.extend = function (a, b) { }; /** - * Cast an object to another type + * Convert an object to another type * @param {Boolean | Number | String | Date | Moment | Null | undefined} object * @param {String | undefined} type Name of the type. Available types: * 'Boolean', 'Number', 'String', @@ -106,7 +106,7 @@ util.extend = function (a, b) { * @return {*} object * @throws Error */ -util.cast = function cast(object, type) { +util.convert = function convert(object, type) { var match; if (object === undefined) { @@ -159,7 +159,7 @@ util.cast = function cast(object, type) { } else { throw new Error( - 'Cannot cast object of type ' + util.getType(object) + + 'Cannot convert object of type ' + util.getType(object) + ' to type Date'); } @@ -185,7 +185,7 @@ util.cast = function cast(object, type) { } else { throw new Error( - 'Cannot cast object of type ' + util.getType(object) + + 'Cannot convert object of type ' + util.getType(object) + ' to type Date'); } @@ -211,7 +211,7 @@ util.cast = function cast(object, type) { } else { throw new Error( - 'Cannot cast object of type ' + util.getType(object) + + 'Cannot convert object of type ' + util.getType(object) + ' to type ISODate'); } @@ -236,12 +236,12 @@ util.cast = function cast(object, type) { } else { throw new Error( - 'Cannot cast object of type ' + util.getType(object) + + 'Cannot convert object of type ' + util.getType(object) + ' to type ASPDate'); } default: - throw new Error('Cannot cast object of type ' + util.getType(object) + + throw new Error('Cannot convert object of type ' + util.getType(object) + ' to type "' + type + '"'); } }; @@ -568,7 +568,7 @@ util.preventDefault = function preventDefault (event) { util.option = {}; /** - * Cast a value as boolean + * Convert a value into a boolean * @param {Boolean | function | undefined} value * @param {Boolean} [defaultValue] * @returns {Boolean} bool @@ -586,7 +586,7 @@ util.option.asBoolean = function (value, defaultValue) { }; /** - * Cast a value as number + * Convert a value into a number * @param {Boolean | function | undefined} value * @param {Number} [defaultValue] * @returns {Number} number @@ -604,7 +604,7 @@ util.option.asNumber = function (value, defaultValue) { }; /** - * Cast a value as string + * Convert a value into a string * @param {String | function | undefined} value * @param {String} [defaultValue] * @returns {String} str @@ -622,7 +622,7 @@ util.option.asString = function (value, defaultValue) { }; /** - * Cast a size or location in pixels or a percentage + * Convert a size or location into a string with pixels or a percentage * @param {String | Number | function | undefined} value * @param {String} [defaultValue] * @returns {String} size @@ -644,7 +644,7 @@ util.option.asSize = function (value, defaultValue) { }; /** - * Cast a value as DOM element + * Convert a value into a DOM element * @param {HTMLElement | function | undefined} value * @param {HTMLElement} [defaultValue] * @returns {HTMLElement | null} dom diff --git a/test/dataset.html b/test/dataset.html index 05135b2a..7cb93ee6 100644 --- a/test/dataset.html +++ b/test/dataset.html @@ -9,7 +9,7 @@