Something went wrong on our end
Select Git revision
buildmap.mjs
-
Volker Schukai authoredVolker Schukai authored
buildmap.mjs 12.88 KiB
/**
* Copyright © schukai GmbH and all contributing authors, {{copyRightYear}}. All rights reserved.
* Node module: @schukai/monster
*
* This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
* The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
*
* For those who do not wish to adhere to the AGPLv3, a commercial license is available.
* Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
* For more information about purchasing a commercial license, please contact schukai GmbH.
*
* SPDX-License-Identifier: AGPL-3.0
*/
import { isFunction, isObject, isString } from "../types/is.mjs";
import { validateString } from "../types/validate.mjs";
import { clone } from "../util/clone.mjs";
import { DELIMITER, Pathfinder, WILDCARD } from "./pathfinder.mjs";
export { buildMap, PARENT, assembleParts };
/**
* @type {string}
* @memberOf Monster.Data
*/
const PARENT = "^";
/**
* Maps can be easily created from data objects with the help of the function `buildMap()`.
*
* The path can be specified as either a simple definition a.b.c or a template ${a.b.c}.
* Key and value can be either a definition or a template. The key does not have to be defined.
* The templates determine the appearance of the keys and the value of the map. Either a single value
* id can be taken or a composite key ${id} ${name} can be used.
*
* If you want to access the values of the parent data set, you have to use the ^ character, for example, ${id} ${^.name}.
*
* @externalExample ../../example/data/buildmap.mjs
*
* @param {*} subject - The data object from which the map will be created
* @param {string|Monster.Data~exampleSelectorCallback} selector - The path to the data object, or a callback that returns a map.
* @param {string} [valueTemplate] - A template for the value of the map.
* @param {string} [keyTemplate] - A template for the key of the map.
* @param {Monster.Data~exampleFilterCallback} [filter] - A callback function to filter out values.
* @return {*} - The created map.
* @memberOf Monster.Data
* @throws {TypeError} - If the value is neither a string nor a function.
* @throws {TypeError} - If the selector callback does not return a map.
**/
function buildMap(subject, selector, valueTemplate, keyTemplate, filter) {
return assembleParts(subject, selector, filter, function (v, k, m) {
k = build(v, keyTemplate, k);
v = build(v, valueTemplate);
this.set(k, v);
});
}
/**
* The assembleParts function is a private function that helps in building a map from a subject object based on a provided
* selector. The selector can either be a string or a callback function. This function is meant to be used as a
* helper function by other functions in the module.
*
* The function takes four parameters:
*
* subject: The subject object from which the map is to be built
* selector: The selector to determine the structure of the map. It can be a string or a callback function.
* filter (optional): A callback function that can be used to filter values based on some criteria.
* callback: A function to be called for each element in the map.
* If the selector parameter is a callback function, it is executed passing the subject as its argument,
* and the resulting value must be an instance of Map. Otherwise, if the selector parameter is a string,