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.

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