UNPKG

@d3plus/data

Version:

JavaScript data loading, manipulation, and analysis functions.

956 lines (907 loc) 37 kB
/* @d3plus/data v3.0.6 JavaScript data loading, manipulation, and analysis functions. Copyright (c) 2025 D3plus - https://d3plus.org @license MIT */ (function (factory) { typeof define === 'function' && define.amd ? define(factory) : factory(); })((function () { 'use strict'; if (typeof window !== "undefined") { (function () { try { if (typeof SVGElement === 'undefined' || Boolean(SVGElement.prototype.innerHTML)) { return; } } catch (e) { return; } function serializeNode (node) { switch (node.nodeType) { case 1: return serializeElementNode(node); case 3: return serializeTextNode(node); case 8: return serializeCommentNode(node); } } function serializeTextNode (node) { return node.textContent.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); } function serializeCommentNode (node) { return '<!--' + node.nodeValue + '-->' } function serializeElementNode (node) { var output = ''; output += '<' + node.tagName; if (node.hasAttributes()) { [].forEach.call(node.attributes, function(attrNode) { output += ' ' + attrNode.name + '="' + attrNode.value + '"'; }); } output += '>'; if (node.hasChildNodes()) { [].forEach.call(node.childNodes, function(childNode) { output += serializeNode(childNode); }); } output += '</' + node.tagName + '>'; return output; } Object.defineProperty(SVGElement.prototype, 'innerHTML', { get: function () { var output = ''; [].forEach.call(this.childNodes, function(childNode) { output += serializeNode(childNode); }); return output; }, set: function (markup) { while (this.firstChild) { this.removeChild(this.firstChild); } try { var dXML = new DOMParser(); dXML.async = false; var sXML = '<svg xmlns=\'http://www.w3.org/2000/svg\' xmlns:xlink=\'http://www.w3.org/1999/xlink\'>' + markup + '</svg>'; var svgDocElement = dXML.parseFromString(sXML, 'text/xml').documentElement; [].forEach.call(svgDocElement.childNodes, function(childNode) { this.appendChild(this.ownerDocument.importNode(childNode, true)); }.bind(this)); } catch (e) { throw new Error('Error parsing markup string'); } } }); Object.defineProperty(SVGElement.prototype, 'innerSVG', { get: function () { return this.innerHTML; }, set: function (markup) { this.innerHTML = markup; } }); })(); } })); (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define('@d3plus/data', ['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3plus = {})); })(this, (function (exports) { /** @function isData @desc Returns true/false whether the argument provided to the function should be loaded using an internal XHR request. Valid data can either be a string URL or an Object with "url" and "headers" keys. @param {*} dataItem The value to be tested */ var isData = ((dataItem)=>typeof dataItem === "string" || typeof dataItem === "object" && dataItem.url && dataItem.headers); var prefix = "$"; function Map() {} Map.prototype = map.prototype = { constructor: Map, has: function(key) { return prefix + key in this; }, get: function(key) { return this[prefix + key]; }, set: function(key, value) { this[prefix + key] = value; return this; }, remove: function(key) { var property = prefix + key; return property in this && delete this[property]; }, clear: function() { for(var property in this)if (property[0] === prefix) delete this[property]; }, keys: function() { var keys = []; for(var property in this)if (property[0] === prefix) keys.push(property.slice(1)); return keys; }, values: function() { var values = []; for(var property in this)if (property[0] === prefix) values.push(this[property]); return values; }, entries: function() { var entries = []; for(var property in this)if (property[0] === prefix) entries.push({ key: property.slice(1), value: this[property] }); return entries; }, size: function() { var size = 0; for(var property in this)if (property[0] === prefix) ++size; return size; }, empty: function() { for(var property in this)if (property[0] === prefix) return false; return true; }, each: function(f) { for(var property in this)if (property[0] === prefix) f(this[property], property.slice(1), this); } }; function map(object, f) { var map = new Map; // Copy constructor. if (object instanceof Map) object.each(function(value, key) { map.set(key, value); }); else if (Array.isArray(object)) { var i = -1, n = object.length, o; if (f == null) while(++i < n)map.set(i, object[i]); else while(++i < n)map.set(f(o = object[i], i, object), o); } else if (object) for(var key in object)map.set(key, object[key]); return map; } function nest$1() { var keys = [], sortKeys = [], sortValues, rollup, nest; function apply(array, depth, createResult, setResult) { if (depth >= keys.length) { if (sortValues != null) array.sort(sortValues); return rollup != null ? rollup(array) : array; } var i = -1, n = array.length, key = keys[depth++], keyValue, value, valuesByKey = map(), values, result = createResult(); while(++i < n){ if (values = valuesByKey.get(keyValue = key(value = array[i]) + "")) { values.push(value); } else { valuesByKey.set(keyValue, [ value ]); } } valuesByKey.each(function(values, key) { setResult(result, key, apply(values, depth, createResult, setResult)); }); return result; } function entries(map, depth) { if (++depth > keys.length) return map; var array, sortKey = sortKeys[depth - 1]; if (rollup != null && depth >= keys.length) array = map.entries(); else array = [], map.each(function(v, k) { array.push({ key: k, values: entries(v, depth) }); }); return sortKey != null ? array.sort(function(a, b) { return sortKey(a.key, b.key); }) : array; } return nest = { object: function(array) { return apply(array, 0, createObject, setObject); }, map: function(array) { return apply(array, 0, createMap, setMap); }, entries: function(array) { return entries(apply(array, 0, createMap, setMap), 0); }, key: function(d) { keys.push(d); return nest; }, sortKeys: function(order) { sortKeys[keys.length - 1] = order; return nest; }, sortValues: function(order) { sortValues = order; return nest; }, rollup: function(f) { rollup = f; return nest; } }; } function createObject() { return {}; } function setObject(object, key, value) { object[key] = value; } function createMap() { return map(); } function setMap(map, key, value) { map.set(key, value); } function Set() {} var proto = map.prototype; Set.prototype = { constructor: Set, has: proto.has, add: function(value) { value += ""; this[prefix + value] = value; return this; }, remove: proto.remove, clear: proto.clear, values: proto.keys, size: proto.size, empty: proto.empty, each: proto.each }; var noop = { value: function() {} }; function dispatch() { for(var i = 0, n = arguments.length, _ = {}, t; i < n; ++i){ if (!(t = arguments[i] + "") || t in _ || /[\s.]/.test(t)) throw new Error("illegal type: " + t); _[t] = []; } return new Dispatch(_); } function Dispatch(_) { this._ = _; } function parseTypenames(typenames, types) { return typenames.trim().split(/^|\s+/).map(function(t) { var name = "", i = t.indexOf("."); if (i >= 0) name = t.slice(i + 1), t = t.slice(0, i); if (t && !types.hasOwnProperty(t)) throw new Error("unknown type: " + t); return { type: t, name: name }; }); } Dispatch.prototype = dispatch.prototype = { constructor: Dispatch, on: function(typename, callback) { var _ = this._, T = parseTypenames(typename + "", _), t, i = -1, n = T.length; // If no callback was specified, return the callback of the given type and name. if (arguments.length < 2) { while(++i < n)if ((t = (typename = T[i]).type) && (t = get(_[t], typename.name))) return t; return; } // If a type was specified, set the callback for the given type and name. // Otherwise, if a null callback was specified, remove callbacks of the given name. if (callback != null && typeof callback !== "function") throw new Error("invalid callback: " + callback); while(++i < n){ if (t = (typename = T[i]).type) _[t] = set(_[t], typename.name, callback); else if (callback == null) for(t in _)_[t] = set(_[t], typename.name, null); } return this; }, copy: function() { var copy = {}, _ = this._; for(var t in _)copy[t] = _[t].slice(); return new Dispatch(copy); }, call: function(type, that) { if ((n = arguments.length - 2) > 0) for(var args = new Array(n), i = 0, n, t; i < n; ++i)args[i] = arguments[i + 2]; if (!this._.hasOwnProperty(type)) throw new Error("unknown type: " + type); for(t = this._[type], i = 0, n = t.length; i < n; ++i)t[i].value.apply(that, args); }, apply: function(type, that, args) { if (!this._.hasOwnProperty(type)) throw new Error("unknown type: " + type); for(var t = this._[type], i = 0, n = t.length; i < n; ++i)t[i].value.apply(that, args); } }; function get(type, name) { for(var i = 0, n = type.length, c; i < n; ++i){ if ((c = type[i]).name === name) { return c.value; } } } function set(type, name, callback) { for(var i = 0, n = type.length; i < n; ++i){ if (type[i].name === name) { type[i] = noop, type = type.slice(0, i).concat(type.slice(i + 1)); break; } } if (callback != null) type.push({ name: name, value: callback }); return type; } function request(url, callback) { var request, event = dispatch("beforesend", "progress", "load", "error"), mimeType, headers = map(), xhr = new XMLHttpRequest, user = null, password = null, response, responseType, timeout = 0; // If IE does not support CORS, use XDomainRequest. if (typeof XDomainRequest !== "undefined" && !("withCredentials" in xhr) && /^(http(s)?:)?\/\//.test(url)) xhr = new XDomainRequest; "onload" in xhr ? xhr.onload = xhr.onerror = xhr.ontimeout = respond : xhr.onreadystatechange = function(o) { xhr.readyState > 3 && respond(o); }; function respond(o) { var status = xhr.status, result; if (!status && hasResponse(xhr) || status >= 200 && status < 300 || status === 304) { if (response) { try { result = response.call(request, xhr); } catch (e) { event.call("error", request, e); return; } } else { result = xhr; } event.call("load", request, result); } else { event.call("error", request, o); } } xhr.onprogress = function(e) { event.call("progress", request, e); }; request = { header: function(name, value) { name = (name + "").toLowerCase(); if (arguments.length < 2) return headers.get(name); if (value == null) headers.remove(name); else headers.set(name, value + ""); return request; }, // If mimeType is non-null and no Accept header is set, a default is used. mimeType: function(value) { if (!arguments.length) return mimeType; mimeType = value == null ? null : value + ""; return request; }, // Specifies what type the response value should take; // for instance, arraybuffer, blob, document, or text. responseType: function(value) { if (!arguments.length) return responseType; responseType = value; return request; }, timeout: function(value) { if (!arguments.length) return timeout; timeout = +value; return request; }, user: function(value) { return arguments.length < 1 ? user : (user = value == null ? null : value + "", request); }, password: function(value) { return arguments.length < 1 ? password : (password = value == null ? null : value + "", request); }, // Specify how to convert the response content to a specific type; // changes the callback value on "load" events. response: function(value) { response = value; return request; }, // Alias for send("GET", …). get: function(data, callback) { return request.send("GET", data, callback); }, // Alias for send("POST", …). post: function(data, callback) { return request.send("POST", data, callback); }, // If callback is non-null, it will be used for error and load events. send: function(method, data, callback) { xhr.open(method, url, true, user, password); if (mimeType != null && !headers.has("accept")) headers.set("accept", mimeType + ",*/*"); if (xhr.setRequestHeader) headers.each(function(value, name) { xhr.setRequestHeader(name, value); }); if (mimeType != null && xhr.overrideMimeType) xhr.overrideMimeType(mimeType); if (responseType != null) xhr.responseType = responseType; if (timeout > 0) xhr.timeout = timeout; if (callback == null && typeof data === "function") callback = data, data = null; if (callback != null && callback.length === 1) callback = fixCallback(callback); if (callback != null) request.on("error", callback).on("load", function(xhr) { callback(null, xhr); }); event.call("beforesend", request, xhr); xhr.send(data == null ? null : data); return request; }, abort: function() { xhr.abort(); return request; }, on: function() { var value = event.on.apply(event, arguments); return value === event ? request : value; } }; return request; } function fixCallback(callback) { return function(error, xhr) { callback(error == null ? xhr : null); }; } function hasResponse(xhr) { var type = xhr.responseType; return type && type !== "text" ? xhr.response // null on error : xhr.responseText; // "" on error } function type(defaultMimeType, response) { return function(url, callback) { var r = request(url).mimeType(defaultMimeType).response(response); if (callback != null) { if (typeof callback !== "function") throw new Error("invalid callback: " + callback); return r.get(callback); } return r; }; } var json = type("application/json", function(xhr) { return JSON.parse(xhr.responseText); }); var text = type("text/plain", function(xhr) { return xhr.responseText; }); var EOL = {}, EOF = {}, QUOTE = 34, NEWLINE = 10, RETURN = 13; function objectConverter(columns) { return new Function("d", "return {" + columns.map(function(name, i) { return JSON.stringify(name) + ": d[" + i + "] || \"\""; }).join(",") + "}"); } function customConverter(columns, f) { var object = objectConverter(columns); return function(row, i) { return f(object(row), i, columns); }; } // Compute unique columns in order of discovery. function inferColumns(rows) { var columnSet = Object.create(null), columns = []; rows.forEach(function(row) { for(var column in row){ if (!(column in columnSet)) { columns.push(columnSet[column] = column); } } }); return columns; } function pad(value, width) { var s = value + "", length = s.length; return length < width ? new Array(width - length + 1).join(0) + s : s; } function formatYear(year) { return year < 0 ? "-" + pad(-year, 6) : year > 9999 ? "+" + pad(year, 6) : pad(year, 4); } function formatDate(date) { var hours = date.getUTCHours(), minutes = date.getUTCMinutes(), seconds = date.getUTCSeconds(), milliseconds = date.getUTCMilliseconds(); return isNaN(date) ? "Invalid Date" : formatYear(date.getUTCFullYear()) + "-" + pad(date.getUTCMonth() + 1, 2) + "-" + pad(date.getUTCDate(), 2) + (milliseconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "." + pad(milliseconds, 3) + "Z" : seconds ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2) + "Z" : minutes || hours ? "T" + pad(hours, 2) + ":" + pad(minutes, 2) + "Z" : ""); } function dsv$1(delimiter) { var reFormat = new RegExp("[\"" + delimiter + "\n\r]"), DELIMITER = delimiter.charCodeAt(0); function parse(text, f) { var convert, columns, rows = parseRows(text, function(row, i) { if (convert) return convert(row, i - 1); columns = row, convert = f ? customConverter(row, f) : objectConverter(row); }); rows.columns = columns || []; return rows; } function parseRows(text, f) { var rows = [], N = text.length, I = 0, n = 0, t, eof = N <= 0, eol = false; // current token followed by EOL? // Strip the trailing newline. if (text.charCodeAt(N - 1) === NEWLINE) --N; if (text.charCodeAt(N - 1) === RETURN) --N; function token() { if (eof) return EOF; if (eol) return eol = false, EOL; // Unescape quotes. var i, j = I, c; if (text.charCodeAt(j) === QUOTE) { while(I++ < N && text.charCodeAt(I) !== QUOTE || text.charCodeAt(++I) === QUOTE); if ((i = I) >= N) eof = true; else if ((c = text.charCodeAt(I++)) === NEWLINE) eol = true; else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; } return text.slice(j + 1, i - 1).replace(/""/g, "\""); } // Find next delimiter or newline. while(I < N){ if ((c = text.charCodeAt(i = I++)) === NEWLINE) eol = true; else if (c === RETURN) { eol = true; if (text.charCodeAt(I) === NEWLINE) ++I; } else if (c !== DELIMITER) continue; return text.slice(j, i); } // Return last token before EOF. return eof = true, text.slice(j, N); } while((t = token()) !== EOF){ var row = []; while(t !== EOL && t !== EOF)row.push(t), t = token(); if (f && (row = f(row, n++)) == null) continue; rows.push(row); } return rows; } function preformatBody(rows, columns) { return rows.map(function(row) { return columns.map(function(column) { return formatValue(row[column]); }).join(delimiter); }); } function format(rows, columns) { if (columns == null) columns = inferColumns(rows); return [ columns.map(formatValue).join(delimiter) ].concat(preformatBody(rows, columns)).join("\n"); } function formatBody(rows, columns) { if (columns == null) columns = inferColumns(rows); return preformatBody(rows, columns).join("\n"); } function formatRows(rows) { return rows.map(formatRow).join("\n"); } function formatRow(row) { return row.map(formatValue).join(delimiter); } function formatValue(value) { return value == null ? "" : value instanceof Date ? formatDate(value) : reFormat.test(value += "") ? "\"" + value.replace(/"/g, "\"\"") + "\"" : value; } return { parse: parse, parseRows: parseRows, format: format, formatBody: formatBody, formatRows: formatRows, formatRow: formatRow, formatValue: formatValue }; } var csv$1 = dsv$1(","); var csvParse = csv$1.parse; var tsv$1 = dsv$1("\t"); var tsvParse = tsv$1.parse; function dsv(defaultMimeType, parse) { return function(url, row, callback) { if (arguments.length < 3) callback = row, row = null; var r = request(url).mimeType(defaultMimeType); r.row = function(_) { return arguments.length ? r.response(responseOf(parse, row = _)) : row; }; r.row(row); return callback ? r.get(callback) : r; }; } function responseOf(parse, row) { return function(request) { return parse(request.responseText, row); }; } var csv = dsv("text/csv", csvParse); var tsv = dsv("text/tab-separated-values", tsvParse); /** @function isObject @desc Detects if a variable is a javascript Object. @param {*} item */ function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function isObject(item) { return item && (typeof item === "undefined" ? "undefined" : _type_of(item)) === "object" && (typeof window === "undefined" || item !== window && item !== window.document && !_instanceof(item, Element)) && !Array.isArray(item) ? true : false; } /** @function dataFold @desc Given a JSON object where the data values and headers have been split into separate key lookups, this function will combine the data values with the headers and returns one large array of objects. @param {Object} json A JSON data Object with `data` and `headers` keys. @param {String} [data = "data"] The key used for the flat data array inside of the JSON object. @param {String} [headers = "headers"] The key used for the flat headers array inside of the JSON object. */ var fold = ((json, data = "data", headers = "headers")=>json[data].map((data)=>json[headers].reduce((obj, header, i)=>(obj[header] = data[i], obj), {}))); /** @function dataConcat @desc Reduce and concat all the elements included in arrayOfArrays if they are arrays. If it is a JSON object try to concat the array under given key data. If the key doesn't exists in object item, a warning message is lauched to the console. You need to implement DataFormat callback to concat the arrays manually. @param {Array} arrayOfArray Array of elements @param {String} [data = "data"] The key used for the flat data array if exists inside of the JSON object. */ var concat = ((arrayOfArrays, data = "data")=>arrayOfArrays.reduce((acc, item)=>{ let dataArray = []; if (Array.isArray(item)) { dataArray = item; } else { if (item[data]) { dataArray = item[data]; } // else { // console.warn(`d3plus-viz: Please implement a "dataFormat" callback to concat the arrays manually (consider using the d3plus.dataConcat method in your callback). Currently unable to concatenate (using key: "${data}") the following response:`, item); // } } return acc.concat(dataArray); }, [])); /** @function dataLoad @desc Loads data from a filepath or URL, converts it to a valid JSON object, and returns it to a callback function. @param {Array|String} path The path to the file or url to be loaded. Also support array of paths strings. If an Array of objects is passed, the xhr request logic is skipped. @param {Function} [formatter] An optional formatter function that is run on the loaded data. @param {String} [key] The key in the `this` context to save the resulting data to. @param {Function} [callback] A function that is called when the final data is loaded. It is passed 2 variables, any error present and the data loaded. */ function load(path, formatter, key, callback) { let parser; const getParser = (path)=>{ const ext = path.slice(path.length - 4); switch(ext){ case ".csv": return csv; case ".tsv": return tsv; case ".txt": return text; default: return json; } }; const validateData = (err, parser, data)=>{ if (parser !== json && !err && data && data instanceof Array) { data.forEach((d)=>{ for(const k in d){ if (!isNaN(d[k])) d[k] = parseFloat(d[k]); else if (d[k].toLowerCase() === "false") d[k] = false; else if (d[k].toLowerCase() === "true") d[k] = true; else if (d[k].toLowerCase() === "null") d[k] = null; else if (d[k].toLowerCase() === "undefined") d[k] = undefined; } }); } return data; }; const loadedLength = (loadedArray)=>loadedArray.reduce((prev, current)=>current ? prev + 1 : prev, 0); const getPathIndex = (url, array)=>array.indexOf(url); // If path param is a not an Array then convert path to a 1 element Array to re-use logic if (!(path instanceof Array)) path = [ path ]; const needToLoad = path.find(isData); let loaded = new Array(path.length); const toLoad = []; // If there is a string I'm assuming is a Array to merge, urls or data if (needToLoad) { path.forEach((dataItem, ix)=>{ if (isData(dataItem)) toLoad.push(dataItem); else loaded[ix] = dataItem; }); } else { loaded[0] = path; } // Load all urls an combine them with data arrays const alreadyLoaded = loadedLength(loaded); toLoad.forEach((dataItem)=>{ let headers = {}, url = dataItem; if (typeof dataItem === "object") { url = dataItem.url; headers = dataItem.headers; } parser = getParser(url); const request = parser(url); for(const key in headers){ if (({}).hasOwnProperty.call(headers, key)) { request.header(key, headers[key]); } } request.get((err, data)=>{ data = err ? [] : data; if (data && !(data instanceof Array) && data.data && data.headers) data = fold(data); data = validateData(err, parser, data); loaded[getPathIndex(url, path)] = data; if (loadedLength(loaded) - alreadyLoaded === toLoad.length) { // Format data data = loadedLength(loaded) === 1 ? loaded[0] : loaded; if (this._cache) this._lrucache.set(`${key}_${url}`, data); if (formatter) { const formatterResponse = formatter(loadedLength(loaded) === 1 ? loaded[0] : loaded); if (key === "data" && isObject(formatterResponse)) { data = formatterResponse.data || []; delete formatterResponse.data; this.config(formatterResponse); } else data = formatterResponse || []; } else if (key === "data") { data = concat(loaded, "data"); } if (key && `_${key}` in this) this[`_${key}`] = data; if (callback) callback(err, data); } }); }); // If there is no data to Load response is immediately if (toLoad.length === 0) { loaded = loaded.map((data)=>{ if (data && !(data instanceof Array) && data.data && data.headers) data = fold(data); return data; }); // Format data let data = loadedLength(loaded) === 1 ? loaded[0] : loaded; if (formatter) { const formatterResponse = formatter(loadedLength(loaded) === 1 ? loaded[0] : loaded); if (key === "data" && isObject(formatterResponse)) { data = formatterResponse.data || []; delete formatterResponse.data; this.config(formatterResponse); } else data = formatterResponse || []; } else if (key === "data") { data = concat(loaded, "data"); } if (key && `_${key}` in this) this[`_${key}`] = data; if (callback) callback(null, data); } } /** @function isData @desc Adds the provided value to the internal queue to be loaded, if necessary. This is used internally in new d3plus visualizations that fold in additional data sources, like the nodes and links of Network or the topojson of Geomap. @param {Array|String|Object} data The data to be loaded @param {Function} [data] An optional data formatter/callback @param {String} data The internal Viz method to be modified */ function addToQueue(_, f, key) { if (!(_ instanceof Array)) _ = [ _ ]; const needToLoad = _.find(isData); if (needToLoad) { const prev = this._queue.find((q)=>q[3] === key); const d = [ load.bind(this), _, f, key ]; if (prev) this._queue[this._queue.indexOf(prev)] = d; else this._queue.push(d); } else { this[`_${key}`] = _; } } function* flatten(arrays) { for (const array of arrays){ yield* array; } } function merge(arrays) { return Array.from(flatten(arrays)); } function sum(values, valueof) { let sum = 0; { for (let value of values){ if (value = +value) { sum += value; } } } return sum; } /** @function unique @desc ES5 implementation to reduce an Array of values to unique instances. @param {Array} arr The Array of objects to be filtered. @param {Function} [accessor] An optional accessor function used to extract data points from an Array of Objects. @example <caption>this</caption> unique(["apple", "banana", "apple"]); @example <caption>returns this</caption> ["apple", "banana"] */ function unique(arr, accessor = (d)=>d) { const values = arr.map(accessor).map((d)=>d instanceof Date ? +d : d); return arr.filter((obj, i)=>{ const d = accessor(obj); return values.indexOf(d instanceof Date ? +d : d) === i; }); } /** @function merge @desc Combines an Array of Objects together and returns a new Object. @param {Array} objects The Array of objects to be merged together. @param {Object} aggs An object containing specific aggregation methods (functions) for each key type. By default, numbers are summed and strings are returned as an array of unique values. @example <caption>this</caption> merge([ {id: "foo", group: "A", value: 10, links: [1, 2]}, {id: "bar", group: "A", value: 20, links: [1, 3]} ]); @example <caption>returns this</caption> {id: ["bar", "foo"], group: "A", value: 30, links: [1, 2, 3]} */ function objectMerge(objects, aggs = {}) { const availableKeys = unique(merge(objects.map((o)=>Object.keys(o)))), newObject = {}; availableKeys.forEach((k)=>{ let value; if (aggs[k]) value = aggs[k](objects, (o)=>o[k]); else { const values = objects.map((o)=>o[k]); const types = values.map((v)=>v || v === false ? v.constructor : v).filter((v)=>v !== void 0); if (!types.length) value = undefined; else if (types.indexOf(Array) >= 0) { value = merge(values.map((v)=>v instanceof Array ? v : [ v ])); value = unique(value); if (value.length === 1) value = value[0]; } else if (types.indexOf(String) >= 0) { value = unique(values); if (value.length === 1) value = value[0]; } else if (types.indexOf(Number) >= 0) value = sum(values); else if (types.indexOf(Object) >= 0) { value = unique(values.filter((v)=>v)); if (value.length === 1) value = value[0]; else value = objectMerge(value); } else { value = unique(values.filter((v)=>v !== void 0)); if (value.length === 1) value = value[0]; } } newObject[k] = value; }); return newObject; } /** @function nest @summary Extends the base behavior of d3.nest to allow for multiple depth levels. @param {Array} *data* The data array to be nested. @param {Array} *keys* An array of key accessors that signify each nest level. @private */ function nest(data, keys) { if (!(keys instanceof Array)) keys = [ keys ]; const dataNest = nest$1(); for(let i = 0; i < keys.length; i++)dataNest.key(keys[i]); const nestedData = dataNest.entries(data); return bubble(nestedData); } /** Bubbles up values that do not nest to the furthest key. @param {Array} *values* The "values" of a nest object. @private */ function bubble(values) { return values.map((d)=>{ if (d.key && d.values) { if (d.values[0].key === "undefined") return d.values[0].values[0]; else d.values = bubble(d.values); } return d; }); } exports.addToQueue = addToQueue; exports.concat = concat; exports.fold = fold; exports.isData = isData; exports.load = load; exports.merge = objectMerge; exports.nest = nest; exports.unique = unique; })); //# sourceMappingURL=d3plus-data.full.js.map