UNPKG

ayakashi

Version:

The next generation web scraping framework

74 lines (73 loc) 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.attachJoinActions = void 0; function attachJoinActions(ayakashiInstance) { //@ts-ignore ayakashiInstance.join = function (obj) { if (!obj || typeof obj !== "object" || Object.keys(obj).length === 0) { throw new Error("Invalid object"); } //get the max value length let max = 0; for (const val of Object.values(obj)) { if (Array.isArray(val)) { if (val.length > max) { max = val.length; } } } //verify that all values' length equal the max for (const [key, val] of Object.entries(obj)) { if (Array.isArray(val) && val.length !== max) { throw new Error(`Property <${key}> does not have the correct length`); } } //if we have empty values return an empty array for (const val of Object.values(obj)) { if (Array.isArray(val) && val.length === 0) { return []; } } //save all non-arrays const nonArrays = []; for (const [key, val] of Object.entries(obj)) { if (!Array.isArray(val)) { nonArrays.push({ [key]: val }); } } //check if we only have non-array values if (max === 0) { let j = {}; if (nonArrays.length > 0) { for (const nonArray of nonArrays) { j = Object.assign(Object.assign({}, j), nonArray); } } return [j]; } else { //create the joined value const joined = []; for (let i = 0; i < max; i += 1) { let j = {}; for (const [key, val] of Object.entries(obj)) { if (Array.isArray(val)) { //@ts-ignore j[key] = val[i]; } } //merge non-arrays if (nonArrays.length > 0) { for (const nonArray of nonArrays) { j = Object.assign(Object.assign({}, j), nonArray); } } joined.push(j); } return joined; } }; } exports.attachJoinActions = attachJoinActions;