UNPKG

metagraph

Version:

A framework for building higher-order graph data structures

92 lines (88 loc) 3.15 kB
/*! * metagraph.js <%= conf.pkg.version %> * http://gordonwoodhull.github.io/metagraph.js/ * Copyright 2019 AT&T Intellectual Property * * Licensed under the MIT License * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ /** * The version of the metagraph library. */ const version = '0.0.7'; /** * Converts a value to an array. If the value is already an array, returns it as-is. * If the value is null/undefined, returns an empty array. Otherwise, wraps the value in an array. * * @param a - The value to convert to an array * @returns An array containing the value(s) * * @example * ```typescript * as_array(5) // [5] * as_array([1, 2, 3]) // [1, 2, 3] * as_array(null) // [] * ``` */ function as_array(a) { return !a ? [] : (Array.isArray(a) ? a : [a]); } /** * Converts an object or array to a standardized key-value array format. * If input is an object, converts it to an array of {key, value} pairs. * If input is already an array, returns it as-is. * * @param o - The object or array to convert * @returns An array of key-value pairs * * @example * ```typescript * as_keyvalue({a: 1, b: 2}) // [{key: 'a', value: 1}, {key: 'b', value: 2}] * as_keyvalue([{key: 'x', value: 10}]) // [{key: 'x', value: 10}] * ``` */ function as_keyvalue(o) { return !o ? [] : (Array.isArray(o) ? o : Object.keys(o).map(key => ({ key, value: o[key] }))); } /** * Creates a map from an array of values using provided key and value transformation functions. * * @param vals - Array of values to transform into a map * @param keyf - Function to extract the key from each value * @param wrap - Function to transform each value * @returns A record/map from keys to transformed values * * @example * ```typescript * build_map([{id: 'a', name: 'Alice'}], x => x.id, x => x.name) * // Result: {a: 'Alice'} * ``` */ function build_map(vals, keyf, wrap) { return vals.reduce((o, val) => { o[keyf(val)] = wrap(val); return o; }, {}); } export { as_array, as_keyvalue, build_map, version }; //# sourceMappingURL=core.js.map