UNPKG

graphology

Version:

A robust and multipurpose Graph object for JavaScript.

101 lines (82 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.capitalize = capitalize; exports.deepMerge = deepMerge; exports.sameMembers = sameMembers; exports.spy = spy; exports.addNodesFrom = addNodesFrom; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } /** * Graphology Specs Helpers * ========================= * * Miscellaneous helpers to test more easily. */ /** * Capitalize function. */ function capitalize(string) { return string[0].toUpperCase() + string.slice(1); } /** * Simplistic deep merger function. */ function deepMerge() { for (var _len = arguments.length, objects = new Array(_len), _key = 0; _key < _len; _key++) { objects[_key] = arguments[_key]; } var o = objects[0]; var t, i, l, k; for (i = 1, l = objects.length; i < l; i++) { t = objects[i]; for (k in t) { if (_typeof(t[k]) === 'object') { o[k] = deepMerge(o[k] || {}, t[k]); } else { o[k] = t[k]; } } } return o; } /** * Checking that two arrays have the same members. */ function sameMembers(a1, a2) { if (a1.length !== a2.length) return false; var set = new Set(a1); for (var i = 0, l = a2.length; i < l; i++) { if (!set.has(a2[i])) return false; } return true; } /** * Function spying on the execution of the provided function to ease some * tests, notably related to event handling. * * @param {function} target - Target function. * @param {function} - The spy. */ function spy(target) { var fn = function fn() { fn.called = true; fn.times++; if (typeof target === 'function') return target.apply(null, arguments); }; fn.called = false; fn.times = 0; return fn; } /** * Function adding multiple nodes from an array to the given graph. * * @param {Graph} graph - Target graph. * @param {array} nodes - Node array. */ function addNodesFrom(graph, nodes) { nodes.forEach(function (node) { graph.addNode(node); }); }