UNPKG

@schukai/monster

Version:

Monster is a simple library for creating fast, robust and lightweight websites.

64 lines (59 loc) 1.95 kB
/** * Copyright © Volker Schukai 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 Volker Schukai. * * SPDX-License-Identifier: AGPL-3.0 */ export { extractKeys }; /** * Extracts the keys from the given object and returns a map with the keys and values. * * @private * @param {object} obj * @param {string} keyPrefix * @param {string} keySeparator * @param {string} valueSeparator * @return {Map<any, any>} */ function extractKeys( obj, keyPrefix = "", keySeparator = "-", valueSeparator = ".", ) { const resultMap = new Map(); function helper(currentObj, currentKeyPrefix, currentValuePrefix) { for (const key in currentObj) { if ( currentObj[key] !== null && typeof currentObj[key] === "object" && !Array.isArray(currentObj[key]) ) { const newKeyPrefix = currentKeyPrefix ? currentKeyPrefix + keySeparator + key.toLowerCase() : key.toLowerCase(); const newValuePrefix = currentValuePrefix ? currentValuePrefix + valueSeparator + key : key; helper(currentObj[key], newKeyPrefix, newValuePrefix); } else { const finalKey = currentKeyPrefix ? currentKeyPrefix + keySeparator + key.toLowerCase() : key.toLowerCase(); const finalValue = currentValuePrefix ? currentValuePrefix + valueSeparator + key : key; resultMap.set(finalKey, finalValue); } } } helper(obj, keyPrefix, keyPrefix); return resultMap; }