UNPKG

yao-node-client

Version:

A node client for yao application development

248 lines 8.27 kB
"use strict"; // ArrayPluck 将多个数据记录集合,合并为一个数据记录集合 // columns: ["城市", "行业", "计费"] // pluck: { // "行业":{"key":"city", "value":"数量", "items":[{"city":"北京", "数量":32},{"city":"上海", "数量":20}]}, // "计费":{"key":"city", "value":"计费种类", "items":[{"city":"北京", "计费种类":6},{"city":"西安", "计费种类":3}]}, // } // // return: [ // // {"城市":"北京", "行业":32, "计费":6}, // {"城市":"上海", "行业":20, "计费":null}, // {"城市":"西安", "行业":null, "计费":6} Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayMapSetMapStr = exports.ArrayMapSet = exports.ArrayTree = exports.NewArrayTreeOption = exports.arrayStringUnique = exports.arrayUnique = exports.arrayKeep = exports.arrayColumn = exports.arraySplit = exports.ofArrayPluckValue = exports.arrayPluck = void 0; /** * ArrayPluck 将多个数据记录集合,合并为一个数据记录集合 * @param columns * @param pluck * @returns */ function arrayPluck(columns, pluck) { if (columns.length < 2) { throw new Error("ArrayPluck 参数错误, 应至少包含两列。"); } const primary = columns[0]; const data = {}; // 解析数据 for (const [name, value] of Object.entries(pluck)) { // name="行业", value={"key":"city", "value":"数量", "items":[{"city":"北京", "数量":32},{"city":"上海", "数量":20}]}, const arg = ofArrayPluckValue(value); for (const item of arg.items) { // item = [{"city":"北京", "数量":32},{"city":"上海", "数量":20}] if (item.hasOwnProperty(arg.key)) { // arg.Key = "city" const v = item[arg.key]; // v = "北京" const key = `${v}`; // key = "北京" const val = item[arg.value]; // arg.Value = "数量", val = 32 if (!data.hasOwnProperty(key)) { data[key] = {}; // {`"北京"`: {}} data[key][primary] = v; // {`"北京"`: {"城市":"北京"}} } data[key][name] = val; // {`"北京"`: {"城市":"北京", "行业":32}} } } } // 空值处理 const res = []; for (const key of Object.keys(data)) { // key = `"北京"` for (const name of columns) { // name = "行业" if (!data[key].hasOwnProperty(name)) { data[key][name] = null; } } res.push(data[key]); } return res; } exports.arrayPluck = arrayPluck; function ofArrayPluckValue(value) { return { key: value.key, value: value.value, items: value.items, }; } exports.ofArrayPluckValue = ofArrayPluckValue; // ArraySplit 将多条数记录集合,分解为一个 columns:[]string 和 values: [][]interface{} // 输入: // [ { name: "阿里云计算有限公司", short_name: "阿里云" }, // { name: "世纪互联蓝云", short_name: "上海蓝云" }, // ] // 输出 // [["name","short_name"],[["阿里云计算有限公司","阿里云"],["世纪互联蓝云","上海蓝云"]]] function arraySplit(records) { const columns = []; const values = []; if (records.length === 0) { return [columns, values]; } for (const column in records[0]) { columns.push(column); } for (const record of records) { const value = []; for (const key of columns) { value.push(record[key]); } values.push(value); } return [columns, values]; } exports.arraySplit = arraySplit; // ArrayColumn 返回多条数据记录,指定字段数值。 function arrayColumn(records, name) { const values = []; for (const record of records) { values.push(record[name]); } return values; } exports.arrayColumn = arrayColumn; // ArrayKeep 仅保留指定键名的数据 function arrayKeep(records, keeps) { const values = []; for (const record of records) { const value = {}; for (const keep of keeps) { value[keep] = record[keep]; } values.push(value); } return values; } exports.arrayKeep = arrayKeep; // ArrayUnique 数组排重 function arrayUnique(columns) { const res = []; const m = {}; for (const val of columns) { const key = `${val}`; if (!m[key]) { m[key] = true; res.push(val); } } return res; } exports.arrayUnique = arrayUnique; // ArrayStringUnique 数组排重 function arrayStringUnique(columns) { let res = []; let m = {}; for (let key of columns) { if (!m[key]) { m[key] = true; res.push(key); } } return res; } exports.arrayStringUnique = arrayStringUnique; // NewArrayTreeOption 创建配置 function NewArrayTreeOption(option) { const newOption = { empty: 0, key: "id", parent: "parent", children: "children", }; if (option["empty"] != null) { newOption.empty = option["empty"]; } if (typeof option["parent"] === "string") { newOption.parent = option["parent"]; } if (typeof option["primary"] === "string") { newOption.key = option["primary"]; } if (typeof option["children"] === "string") { newOption.children = option["children"]; } return newOption; } exports.NewArrayTreeOption = NewArrayTreeOption; // ArrayTree []map[string]interface{} 转树形结构 function ArrayTree(records, setting) { const opt = NewArrayTreeOption(setting); return Tree(records, opt); } exports.ArrayTree = ArrayTree; // Tree Array 转换为 Tree function Tree(records, opt) { const mapping = {}; for (let i = 0; i < records.length; i++) { if (records[i][opt.key]) { const primary = `${records[i][opt.key]}`; mapping[primary] = {}; mapping[primary][opt.children] = []; for (const [k, v] of Object.entries(records[i])) { mapping[primary][k] = v; } } } // 向上归集 for (const [key, record] of Object.entries(mapping)) { const parent = `${record[opt.parent]}`; const empty = `${opt.empty}`; if (parent === empty) { // 第一级 continue; } const pKey = `${parent}`; if (mapping[pKey] == null) { continue; } let children = mapping[pKey][opt.children]; if (!Array.isArray(children)) { children = []; } children.push(mapping[key]); mapping[pKey][opt.children] = children; } const res = []; for (let i = 0; i < records.length; i++) { if (records[i][opt.key] != null) { const record = mapping[`${records[i][opt.key]}`]; if (record[opt.parent] != null) { const parent = `${record[opt.parent]}`; const empty = `${opt.empty}`; if (parent === empty) { // 父类为空 res.push(record); } else if (mapping[parent] == null) { // 或者父类为定义的 res.push(record); } } } } return res; } // ArrayMapSet []MapT 设定数值 function ArrayMapSet(records, key, value) { const res = []; for (let i = 0; i < records.length; i++) { const record = records[i]; record[key] = value; res.push(record); } return res; } exports.ArrayMapSet = ArrayMapSet; // ArrayMapSetMapStr []MapT 设定数值 function ArrayMapSetMapStr(records, key, value) { const res = []; for (let i = 0; i < records.length; i++) { const record = records[i]; record[key] = value; res.push(record); } return res; } exports.ArrayMapSetMapStr = ArrayMapSetMapStr; //# sourceMappingURL=array.js.map