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.

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