/** * Graphology Weight Getter * ========================= * * Function creating weight getters. */ function coerceWeight(value) { // Ensuring target value is a correct number if (typeof value !== 'number' || isNaN(value)) return 1; return value; } function createNodeValueGetter(nameOrFunction, defaultValue) { var getter = {}; var coerceToDefault = function (v) { if (typeof v === 'undefined') return defaultValue; return v; }; if (typeof defaultValue === 'function') coerceToDefault = defaultValue; var get = function (attributes) { return coerceToDefault(attributes[nameOrFunction]); }; var returnDefault = function () { return coerceToDefault(undefined); }; if (typeof nameOrFunction === 'string') { getter.fromAttributes = get; getter.fromGraph = function (graph, node) { return get(graph.getNodeAttributes(node)); }; getter.fromEntry = function (node, attributes) { return get(attributes); }; } else if (typeof nameOrFunction === 'function') { getter.fromAttributes = function () { throw new Error( 'graphology-utils/getters/createNodeValueGetter: irrelevant usage.' ); }; getter.fromGraph = function (graph, node) { return coerceToDefault( nameOrFunction(node, graph.getNodeAttributes(node)) ); }; getter.fromEntry = function (node, attributes) { return coerceToDefault(nameOrFunction(node, attributes)); }; } else { getter.fromAttributes = returnDefault; getter.fromGraph = returnDefault; getter.fromEntry = returnDefault; } return getter; } function createEdgeValueGetter(nameOrFunction, defaultValue) { var getter = {}; var coerceToDefault = function (v) { if (typeof v === 'undefined') return defaultValue; return v; }; if (typeof defaultValue === 'function') coerceToDefault = defaultValue; var get = function (attributes) { return coerceToDefault(attributes[nameOrFunction]); }; var returnDefault = function () { return coerceToDefault(undefined); }; if (typeof nameOrFunction === 'string') { getter.fromAttributes = get; getter.fromGraph = function (graph, edge) { return get(graph.getEdgeAttributes(edge)); }; getter.fromEntry = function (edge, attributes) { return get(attributes); }; getter.fromPartialEntry = getter.fromEntry; getter.fromMinimalEntry = getter.fromEntry; } else if (typeof nameOrFunction === 'function') { getter.fromAttributes = function () { throw new Error( 'graphology-utils/getters/createEdgeValueGetter: irrelevant usage.' ); }; getter.fromGraph = function (graph, edge) { // TODO: we can do better, check #310 var extremities = graph.extremities(edge); return coerceToDefault( nameOrFunction( edge, graph.getEdgeAttributes(edge), extremities[0], extremities[1], graph.getNodeAttributes(extremities[0]), graph.getNodeAttributes(extremities[1]), graph.isUndirected(edge) ) ); }; getter.fromEntry = function (e, a, s, t, sa, ta, u) { return coerceToDefault(nameOrFunction(e, a, s, t, sa, ta, u)); }; getter.fromPartialEntry = function (e, a, s, t) { return coerceToDefault(nameOrFunction(e, a, s, t)); }; getter.fromMinimalEntry = function (e, a) { return coerceToDefault(nameOrFunction(e, a)); }; } else { getter.fromAttributes = returnDefault; getter.fromGraph = returnDefault; getter.fromEntry = returnDefault; getter.fromMinimalEntry = returnDefault; } return getter; } exports.createNodeValueGetter = createNodeValueGetter; exports.createEdgeValueGetter = createEdgeValueGetter; exports.createEdgeWeightGetter = function (name) { return createEdgeValueGetter(name, coerceWeight); };