vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

932 lines
24 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. var util = require('./util');
  2. var Queue = require('./Queue');
  3. /**
  4. * DataSet
  5. *
  6. * Usage:
  7. * var dataSet = new DataSet({
  8. * fieldId: '_id',
  9. * type: {
  10. * // ...
  11. * }
  12. * });
  13. *
  14. * dataSet.add(item);
  15. * dataSet.add(data);
  16. * dataSet.update(item);
  17. * dataSet.update(data);
  18. * dataSet.remove(id);
  19. * dataSet.remove(ids);
  20. * var data = dataSet.get();
  21. * var data = dataSet.get(id);
  22. * var data = dataSet.get(ids);
  23. * var data = dataSet.get(ids, options, data);
  24. * dataSet.clear();
  25. *
  26. * A data set can:
  27. * - add/remove/update data
  28. * - gives triggers upon changes in the data
  29. * - can import/export data in various data formats
  30. *
  31. * @param {Array} [data] Optional array with initial data
  32. * @param {Object} [options] Available options:
  33. * {String} fieldId Field name of the id in the
  34. * items, 'id' by default.
  35. * {Object.<String, String} type
  36. * A map with field names as key,
  37. * and the field type as value.
  38. * {Object} queue Queue changes to the DataSet,
  39. * flush them all at once.
  40. * Queue options:
  41. * - {number} delay Delay in ms, null by default
  42. * - {number} max Maximum number of entries in the queue, Infinity by default
  43. * @constructor DataSet
  44. */
  45. // TODO: add a DataSet constructor DataSet(data, options)
  46. function DataSet (data, options) {
  47. // correctly read optional arguments
  48. if (data && !Array.isArray(data)) {
  49. options = data;
  50. data = null;
  51. }
  52. this._options = options || {};
  53. this._data = {}; // map with data indexed by id
  54. this.length = 0; // number of items in the DataSet
  55. this._fieldId = this._options.fieldId || 'id'; // name of the field containing id
  56. this._type = {}; // internal field types (NOTE: this can differ from this._options.type)
  57. // all variants of a Date are internally stored as Date, so we can convert
  58. // from everything to everything (also from ISODate to Number for example)
  59. if (this._options.type) {
  60. var fields = Object.keys(this._options.type);
  61. for (var i = 0, len = fields.length; i < len; i++) {
  62. var field = fields[i];
  63. var value = this._options.type[field];
  64. if (value == 'Date' || value == 'ISODate' || value == 'ASPDate') {
  65. this._type[field] = 'Date';
  66. }
  67. else {
  68. this._type[field] = value;
  69. }
  70. }
  71. }
  72. // TODO: deprecated since version 1.1.1 (or 2.0.0?)
  73. if (this._options.convert) {
  74. throw new Error('Option "convert" is deprecated. Use "type" instead.');
  75. }
  76. this._subscribers = {}; // event subscribers
  77. // add initial data when provided
  78. if (data) {
  79. this.add(data);
  80. }
  81. this.setOptions(options);
  82. }
  83. /**
  84. * @param {Object} [options] Available options:
  85. * {Object} queue Queue changes to the DataSet,
  86. * flush them all at once.
  87. * Queue options:
  88. * - {number} delay Delay in ms, null by default
  89. * - {number} max Maximum number of entries in the queue, Infinity by default
  90. * @param options
  91. */
  92. DataSet.prototype.setOptions = function(options) {
  93. if (options && options.queue !== undefined) {
  94. if (options.queue === false) {
  95. // delete queue if loaded
  96. if (this._queue) {
  97. this._queue.destroy();
  98. delete this._queue;
  99. }
  100. }
  101. else {
  102. // create queue and update its options
  103. if (!this._queue) {
  104. this._queue = Queue.extend(this, {
  105. replace: ['add', 'update', 'remove']
  106. });
  107. }
  108. if (typeof options.queue === 'object') {
  109. this._queue.setOptions(options.queue);
  110. }
  111. }
  112. }
  113. };
  114. /**
  115. * Subscribe to an event, add an event listener
  116. * @param {String} event Event name. Available events: 'put', 'update',
  117. * 'remove'
  118. * @param {function} callback Callback method. Called with three parameters:
  119. * {String} event
  120. * {Object | null} params
  121. * {String | Number} senderId
  122. */
  123. DataSet.prototype.on = function(event, callback) {
  124. var subscribers = this._subscribers[event];
  125. if (!subscribers) {
  126. subscribers = [];
  127. this._subscribers[event] = subscribers;
  128. }
  129. subscribers.push({
  130. callback: callback
  131. });
  132. };
  133. // TODO: remove this deprecated function some day (replaced with `on` since version 0.5, deprecated since v4.0)
  134. DataSet.prototype.subscribe = function () {
  135. throw new Error('DataSet.subscribe is deprecated. Use DataSet.on instead.');
  136. };
  137. /**
  138. * Unsubscribe from an event, remove an event listener
  139. * @param {String} event
  140. * @param {function} callback
  141. */
  142. DataSet.prototype.off = function(event, callback) {
  143. var subscribers = this._subscribers[event];
  144. if (subscribers) {
  145. this._subscribers[event] = subscribers.filter(listener => listener.callback != callback);
  146. }
  147. };
  148. // TODO: remove this deprecated function some day (replaced with `on` since version 0.5, deprecated since v4.0)
  149. DataSet.prototype.unsubscribe = function () {
  150. throw new Error('DataSet.unsubscribe is deprecated. Use DataSet.off instead.');
  151. };
  152. /**
  153. * Trigger an event
  154. * @param {String} event
  155. * @param {Object | null} params
  156. * @param {String} [senderId] Optional id of the sender.
  157. * @private
  158. */
  159. DataSet.prototype._trigger = function (event, params, senderId) {
  160. if (event == '*') {
  161. throw new Error('Cannot trigger event *');
  162. }
  163. var subscribers = [];
  164. if (event in this._subscribers) {
  165. subscribers = subscribers.concat(this._subscribers[event]);
  166. }
  167. if ('*' in this._subscribers) {
  168. subscribers = subscribers.concat(this._subscribers['*']);
  169. }
  170. for (var i = 0, len = subscribers.length; i < len; i++) {
  171. var subscriber = subscribers[i];
  172. if (subscriber.callback) {
  173. subscriber.callback(event, params, senderId || null);
  174. }
  175. }
  176. };
  177. /**
  178. * Add data.
  179. * Adding an item will fail when there already is an item with the same id.
  180. * @param {Object | Array} data
  181. * @param {String} [senderId] Optional sender id
  182. * @return {Array} addedIds Array with the ids of the added items
  183. */
  184. DataSet.prototype.add = function (data, senderId) {
  185. var addedIds = [],
  186. id,
  187. me = this;
  188. if (Array.isArray(data)) {
  189. // Array
  190. for (var i = 0, len = data.length; i < len; i++) {
  191. id = me._addItem(data[i]);
  192. addedIds.push(id);
  193. }
  194. }
  195. else if (data instanceof Object) {
  196. // Single item
  197. id = me._addItem(data);
  198. addedIds.push(id);
  199. }
  200. else {
  201. throw new Error('Unknown dataType');
  202. }
  203. if (addedIds.length) {
  204. this._trigger('add', {items: addedIds}, senderId);
  205. }
  206. return addedIds;
  207. };
  208. /**
  209. * Update existing items. When an item does not exist, it will be created
  210. * @param {Object | Array} data
  211. * @param {String} [senderId] Optional sender id
  212. * @return {Array} updatedIds The ids of the added or updated items
  213. */
  214. DataSet.prototype.update = function (data, senderId) {
  215. var addedIds = [];
  216. var updatedIds = [];
  217. var oldData = [];
  218. var updatedData = [];
  219. var me = this;
  220. var fieldId = me._fieldId;
  221. var addOrUpdate = function (item) {
  222. var id = item[fieldId];
  223. if (me._data[id]) {
  224. var oldItem = util.extend({}, me._data[id]);
  225. // update item
  226. id = me._updateItem(item);
  227. updatedIds.push(id);
  228. updatedData.push(item);
  229. oldData.push(oldItem);
  230. }
  231. else {
  232. // add new item
  233. id = me._addItem(item);
  234. addedIds.push(id);
  235. }
  236. };
  237. if (Array.isArray(data)) {
  238. // Array
  239. for (var i = 0, len = data.length; i < len; i++) {
  240. addOrUpdate(data[i]);
  241. }
  242. }
  243. else if (data instanceof Object) {
  244. // Single item
  245. addOrUpdate(data);
  246. }
  247. else {
  248. throw new Error('Unknown dataType');
  249. }
  250. if (addedIds.length) {
  251. this._trigger('add', {items: addedIds}, senderId);
  252. }
  253. if (updatedIds.length) {
  254. var props = { items: updatedIds, oldData: oldData, data: updatedData };
  255. // TODO: remove deprecated property 'data' some day
  256. //Object.defineProperty(props, 'data', {
  257. // 'get': (function() {
  258. // console.warn('Property data is deprecated. Use DataSet.get(ids) to retrieve the new data, use the oldData property on this object to get the old data');
  259. // return updatedData;
  260. // }).bind(this)
  261. //});
  262. this._trigger('update', props, senderId);
  263. }
  264. return addedIds.concat(updatedIds);
  265. };
  266. /**
  267. * Get a data item or multiple items.
  268. *
  269. * Usage:
  270. *
  271. * get()
  272. * get(options: Object)
  273. *
  274. * get(id: Number | String)
  275. * get(id: Number | String, options: Object)
  276. *
  277. * get(ids: Number[] | String[])
  278. * get(ids: Number[] | String[], options: Object)
  279. *
  280. * Where:
  281. *
  282. * {Number | String} id The id of an item
  283. * {Number[] | String{}} ids An array with ids of items
  284. * {Object} options An Object with options. Available options:
  285. * {String} [returnType] Type of data to be returned.
  286. * Can be 'Array' (default) or 'Object'.
  287. * {Object.<String, String>} [type]
  288. * {String[]} [fields] field names to be returned
  289. * {function} [filter] filter items
  290. * {String | function} [order] Order the items by a field name or custom sort function.
  291. * @throws Error
  292. */
  293. DataSet.prototype.get = function (args) {
  294. var me = this;
  295. // parse the arguments
  296. var id, ids, options;
  297. var firstType = util.getType(arguments[0]);
  298. if (firstType == 'String' || firstType == 'Number') {
  299. // get(id [, options])
  300. id = arguments[0];
  301. options = arguments[1];
  302. }
  303. else if (firstType == 'Array') {
  304. // get(ids [, options])
  305. ids = arguments[0];
  306. options = arguments[1];
  307. }
  308. else {
  309. // get([, options])
  310. options = arguments[0];
  311. }
  312. // determine the return type
  313. var returnType;
  314. if (options && options.returnType) {
  315. var allowedValues = ['Array', 'Object'];
  316. returnType = allowedValues.indexOf(options.returnType) == -1 ? 'Array' : options.returnType;
  317. }
  318. else {
  319. returnType = 'Array';
  320. }
  321. // build options
  322. var type = options && options.type || this._options.type;
  323. var filter = options && options.filter;
  324. var items = [], item, itemIds, itemId, i, len;
  325. // convert items
  326. if (id != undefined) {
  327. // return a single item
  328. item = me._getItem(id, type);
  329. if (filter && !filter(item)) {
  330. item = null;
  331. }
  332. }
  333. else if (ids != undefined) {
  334. // return a subset of items
  335. for (i = 0, len = ids.length; i < len; i++) {
  336. item = me._getItem(ids[i], type);
  337. if (!filter || filter(item)) {
  338. items.push(item);
  339. }
  340. }
  341. }
  342. else {
  343. // return all items
  344. itemIds = Object.keys(this._data);
  345. for (i = 0, len = itemIds.length; i < len; i++) {
  346. itemId = itemIds[i];
  347. item = me._getItem(itemId, type);
  348. if (!filter || filter(item)) {
  349. items.push(item);
  350. }
  351. }
  352. }
  353. // order the results
  354. if (options && options.order && id == undefined) {
  355. this._sort(items, options.order);
  356. }
  357. // filter fields of the items
  358. if (options && options.fields) {
  359. var fields = options.fields;
  360. if (id != undefined) {
  361. item = this._filterFields(item, fields);
  362. }
  363. else {
  364. for (i = 0, len = items.length; i < len; i++) {
  365. items[i] = this._filterFields(items[i], fields);
  366. }
  367. }
  368. }
  369. // return the results
  370. if (returnType == 'Object') {
  371. var result = {},
  372. resultant;
  373. for (i = 0, len = items.length; i < len; i++) {
  374. resultant = items[i];
  375. result[resultant.id] = resultant;
  376. }
  377. return result;
  378. }
  379. else {
  380. if (id != undefined) {
  381. // a single item
  382. return item;
  383. }
  384. else {
  385. // just return our array
  386. return items;
  387. }
  388. }
  389. };
  390. /**
  391. * Get ids of all items or from a filtered set of items.
  392. * @param {Object} [options] An Object with options. Available options:
  393. * {function} [filter] filter items
  394. * {String | function} [order] Order the items by
  395. * a field name or custom sort function.
  396. * @return {Array} ids
  397. */
  398. DataSet.prototype.getIds = function (options) {
  399. var data = this._data,
  400. filter = options && options.filter,
  401. order = options && options.order,
  402. type = options && options.type || this._options.type,
  403. itemIds = Object.keys(data),
  404. i,
  405. len,
  406. id,
  407. item,
  408. items,
  409. ids = [];
  410. if (filter) {
  411. // get filtered items
  412. if (order) {
  413. // create ordered list
  414. items = [];
  415. for (i = 0, len = itemIds.length; i < len; i++) {
  416. id = itemIds[i];
  417. item = this._getItem(id, type);
  418. if (filter(item)) {
  419. items.push(item);
  420. }
  421. }
  422. this._sort(items, order);
  423. for (i = 0, len = items.length; i < len; i++) {
  424. ids.push(items[i][this._fieldId]);
  425. }
  426. }
  427. else {
  428. // create unordered list
  429. for (i = 0, len = itemIds.length; i < len; i++) {
  430. id = itemIds[i];
  431. item = this._getItem(id, type);
  432. if (filter(item)) {
  433. ids.push(item[this._fieldId]);
  434. }
  435. }
  436. }
  437. }
  438. else {
  439. // get all items
  440. if (order) {
  441. // create an ordered list
  442. items = [];
  443. for (i = 0, len = itemIds.length; i < len; i++) {
  444. id = itemIds[i];
  445. items.push(data[id]);
  446. }
  447. this._sort(items, order);
  448. for (i = 0, len = items.length; i < len; i++) {
  449. ids.push(items[i][this._fieldId]);
  450. }
  451. }
  452. else {
  453. // create unordered list
  454. for (i = 0, len = itemIds.length; i < len; i++) {
  455. id = itemIds[i];
  456. item = data[id];
  457. ids.push(item[this._fieldId]);
  458. }
  459. }
  460. }
  461. return ids;
  462. };
  463. /**
  464. * Returns the DataSet itself. Is overwritten for example by the DataView,
  465. * which returns the DataSet it is connected to instead.
  466. */
  467. DataSet.prototype.getDataSet = function () {
  468. return this;
  469. };
  470. /**
  471. * Execute a callback function for every item in the dataset.
  472. * @param {function} callback
  473. * @param {Object} [options] Available options:
  474. * {Object.<String, String>} [type]
  475. * {String[]} [fields] filter fields
  476. * {function} [filter] filter items
  477. * {String | function} [order] Order the items by
  478. * a field name or custom sort function.
  479. */
  480. DataSet.prototype.forEach = function (callback, options) {
  481. var filter = options && options.filter,
  482. type = options && options.type || this._options.type,
  483. data = this._data,
  484. itemIds = Object.keys(data),
  485. i,
  486. len,
  487. item,
  488. id;
  489. if (options && options.order) {
  490. // execute forEach on ordered list
  491. var items = this.get(options);
  492. for (i = 0, len = items.length; i < len; i++) {
  493. item = items[i];
  494. id = item[this._fieldId];
  495. callback(item, id);
  496. }
  497. }
  498. else {
  499. // unordered
  500. for (i = 0, len = itemIds.length; i < len; i++) {
  501. id = itemIds[i];
  502. item = this._getItem(id, type);
  503. if (!filter || filter(item)) {
  504. callback(item, id);
  505. }
  506. }
  507. }
  508. };
  509. /**
  510. * Map every item in the dataset.
  511. * @param {function} callback
  512. * @param {Object} [options] Available options:
  513. * {Object.<String, String>} [type]
  514. * {String[]} [fields] filter fields
  515. * {function} [filter] filter items
  516. * {String | function} [order] Order the items by
  517. * a field name or custom sort function.
  518. * @return {Object[]} mappedItems
  519. */
  520. DataSet.prototype.map = function (callback, options) {
  521. var filter = options && options.filter,
  522. type = options && options.type || this._options.type,
  523. mappedItems = [],
  524. data = this._data,
  525. itemIds = Object.keys(data),
  526. i,
  527. len,
  528. id,
  529. item;
  530. // convert and filter items
  531. for (i = 0, len = itemIds.length; i < len; i++) {
  532. id = itemIds[i];
  533. item = this._getItem(id, type);
  534. if (!filter || filter(item)) {
  535. mappedItems.push(callback(item, id));
  536. }
  537. }
  538. // order items
  539. if (options && options.order) {
  540. this._sort(mappedItems, options.order);
  541. }
  542. return mappedItems;
  543. };
  544. /**
  545. * Filter the fields of an item
  546. * @param {Object | null} item
  547. * @param {String[]} fields Field names
  548. * @return {Object | null} filteredItem or null if no item is provided
  549. * @private
  550. */
  551. DataSet.prototype._filterFields = function (item, fields) {
  552. if (!item) { // item is null
  553. return item;
  554. }
  555. var filteredItem = {},
  556. itemFields = Object.keys(item),
  557. len = itemFields.length,
  558. i,
  559. field;
  560. if(Array.isArray(fields)){
  561. for (i = 0; i < len; i++) {
  562. field = itemFields[i];
  563. if (fields.indexOf(field) != -1) {
  564. filteredItem[field] = item[field];
  565. }
  566. }
  567. }else{
  568. for (i = 0; i < len; i++) {
  569. field = itemFields[i];
  570. if (fields.hasOwnProperty(field)) {
  571. filteredItem[fields[field]] = item[field];
  572. }
  573. }
  574. }
  575. return filteredItem;
  576. };
  577. /**
  578. * Sort the provided array with items
  579. * @param {Object[]} items
  580. * @param {String | function} order A field name or custom sort function.
  581. * @private
  582. */
  583. DataSet.prototype._sort = function (items, order) {
  584. if (util.isString(order)) {
  585. // order by provided field name
  586. var name = order; // field name
  587. items.sort(function (a, b) {
  588. var av = a[name];
  589. var bv = b[name];
  590. return (av > bv) ? 1 : ((av < bv) ? -1 : 0);
  591. });
  592. }
  593. else if (typeof order === 'function') {
  594. // order by sort function
  595. items.sort(order);
  596. }
  597. // TODO: extend order by an Object {field:String, direction:String}
  598. // where direction can be 'asc' or 'desc'
  599. else {
  600. throw new TypeError('Order must be a function or a string');
  601. }
  602. };
  603. /**
  604. * Remove an object by pointer or by id
  605. * @param {String | Number | Object | Array} id Object or id, or an array with
  606. * objects or ids to be removed
  607. * @param {String} [senderId] Optional sender id
  608. * @return {Array} removedIds
  609. */
  610. DataSet.prototype.remove = function (id, senderId) {
  611. var removedIds = [],
  612. i, len, removedId;
  613. if (Array.isArray(id)) {
  614. for (i = 0, len = id.length; i < len; i++) {
  615. removedId = this._remove(id[i]);
  616. if (removedId != null) {
  617. removedIds.push(removedId);
  618. }
  619. }
  620. }
  621. else {
  622. removedId = this._remove(id);
  623. if (removedId != null) {
  624. removedIds.push(removedId);
  625. }
  626. }
  627. if (removedIds.length) {
  628. this._trigger('remove', {items: removedIds}, senderId);
  629. }
  630. return removedIds;
  631. };
  632. /**
  633. * Remove an item by its id
  634. * @param {Number | String | Object} id id or item
  635. * @returns {Number | String | null} id
  636. * @private
  637. */
  638. DataSet.prototype._remove = function (id) {
  639. if (util.isNumber(id) || util.isString(id)) {
  640. if (this._data[id]) {
  641. delete this._data[id];
  642. this.length--;
  643. return id;
  644. }
  645. }
  646. else if (id instanceof Object) {
  647. var itemId = id[this._fieldId];
  648. if (itemId && this._data[itemId]) {
  649. delete this._data[itemId];
  650. this.length--;
  651. return itemId;
  652. }
  653. }
  654. return null;
  655. };
  656. /**
  657. * Clear the data
  658. * @param {String} [senderId] Optional sender id
  659. * @return {Array} removedIds The ids of all removed items
  660. */
  661. DataSet.prototype.clear = function (senderId) {
  662. var ids = Object.keys(this._data);
  663. this._data = {};
  664. this.length = 0;
  665. this._trigger('remove', {items: ids}, senderId);
  666. return ids;
  667. };
  668. /**
  669. * Find the item with maximum value of a specified field
  670. * @param {String} field
  671. * @return {Object | null} item Item containing max value, or null if no items
  672. */
  673. DataSet.prototype.max = function (field) {
  674. var data = this._data,
  675. itemIds = Object.keys(data),
  676. max = null,
  677. maxField = null,
  678. i,
  679. len;
  680. for (i = 0, len = itemIds.length; i < len; i++) {
  681. var id = itemIds[i];
  682. var item = data[id];
  683. var itemField = item[field];
  684. if (itemField != null && (!max || itemField > maxField)) {
  685. max = item;
  686. maxField = itemField;
  687. }
  688. }
  689. return max;
  690. };
  691. /**
  692. * Find the item with minimum value of a specified field
  693. * @param {String} field
  694. * @return {Object | null} item Item containing max value, or null if no items
  695. */
  696. DataSet.prototype.min = function (field) {
  697. var data = this._data,
  698. itemIds = Object.keys(data),
  699. min = null,
  700. minField = null,
  701. i,
  702. len;
  703. for (i = 0, len = itemIds.length; i < len; i++) {
  704. var id = itemIds[i];
  705. var item = data[id];
  706. var itemField = item[field];
  707. if (itemField != null && (!min || itemField < minField)) {
  708. min = item;
  709. minField = itemField;
  710. }
  711. }
  712. return min;
  713. };
  714. /**
  715. * Find all distinct values of a specified field
  716. * @param {String} field
  717. * @return {Array} values Array containing all distinct values. If data items
  718. * do not contain the specified field are ignored.
  719. * The returned array is unordered.
  720. */
  721. DataSet.prototype.distinct = function (field) {
  722. var data = this._data;
  723. var itemIds = Object.keys(data);
  724. var values = [];
  725. var fieldType = this._options.type && this._options.type[field] || null;
  726. var count = 0;
  727. var i,
  728. j,
  729. len;
  730. for (i = 0, len = itemIds.length; i < len; i++) {
  731. var id = itemIds[i];
  732. var item = data[id];
  733. var value = item[field];
  734. var exists = false;
  735. for (j = 0; j < count; j++) {
  736. if (values[j] == value) {
  737. exists = true;
  738. break;
  739. }
  740. }
  741. if (!exists && (value !== undefined)) {
  742. values[count] = value;
  743. count++;
  744. }
  745. }
  746. if (fieldType) {
  747. for (i = 0, len = values.length; i < len; i++) {
  748. values[i] = util.convert(values[i], fieldType);
  749. }
  750. }
  751. return values;
  752. };
  753. /**
  754. * Add a single item. Will fail when an item with the same id already exists.
  755. * @param {Object} item
  756. * @return {String} id
  757. * @private
  758. */
  759. DataSet.prototype._addItem = function (item) {
  760. var id = item[this._fieldId];
  761. if (id != undefined) {
  762. // check whether this id is already taken
  763. if (this._data[id]) {
  764. // item already exists
  765. throw new Error('Cannot add item: item with id ' + id + ' already exists');
  766. }
  767. }
  768. else {
  769. // generate an id
  770. id = util.randomUUID();
  771. item[this._fieldId] = id;
  772. }
  773. var d = {},
  774. fields = Object.keys(item),
  775. i,
  776. len;
  777. for (i = 0, len = fields.length; i < len; i++) {
  778. var field = fields[i];
  779. var fieldType = this._type[field]; // type may be undefined
  780. d[field] = util.convert(item[field], fieldType);
  781. }
  782. this._data[id] = d;
  783. this.length++;
  784. return id;
  785. };
  786. /**
  787. * Get an item. Fields can be converted to a specific type
  788. * @param {String} id
  789. * @param {Object.<String, String>} [types] field types to convert
  790. * @return {Object | null} item
  791. * @private
  792. */
  793. DataSet.prototype._getItem = function (id, types) {
  794. var field, value, i, len;
  795. // get the item from the dataset
  796. var raw = this._data[id];
  797. if (!raw) {
  798. return null;
  799. }
  800. // convert the items field types
  801. var converted = {},
  802. fields = Object.keys(raw);
  803. if (types) {
  804. for (i = 0, len = fields.length; i < len; i++) {
  805. field = fields[i];
  806. value = raw[field];
  807. converted[field] = util.convert(value, types[field]);
  808. }
  809. }
  810. else {
  811. // no field types specified, no converting needed
  812. for (i = 0, len = fields.length; i < len; i++) {
  813. field = fields[i];
  814. value = raw[field];
  815. converted[field] = value;
  816. }
  817. }
  818. return converted;
  819. };
  820. /**
  821. * Update a single item: merge with existing item.
  822. * Will fail when the item has no id, or when there does not exist an item
  823. * with the same id.
  824. * @param {Object} item
  825. * @return {String} id
  826. * @private
  827. */
  828. DataSet.prototype._updateItem = function (item) {
  829. var id = item[this._fieldId];
  830. if (id == undefined) {
  831. throw new Error('Cannot update item: item has no id (item: ' + JSON.stringify(item) + ')');
  832. }
  833. var d = this._data[id];
  834. if (!d) {
  835. // item doesn't exist
  836. throw new Error('Cannot update item: no item with id ' + id + ' found');
  837. }
  838. // merge with current item
  839. var fields = Object.keys(item);
  840. for (var i = 0, len = fields.length; i < len; i++) {
  841. var field = fields[i];
  842. var fieldType = this._type[field]; // type may be undefined
  843. d[field] = util.convert(item[field], fieldType);
  844. }
  845. return id;
  846. };
  847. module.exports = DataSet;