Next Perpendicular Moonlanding

    @gaopeng123/utils.array
    TypeScript icon, indicating that this package has built-in type declarations

    0.5.3 • Public • Published

    Array

    array

    convertToTwodimensional

    (arr: Array<any>, len: number): Array<Array<any>>

    将一维数组,转为指定长度的二维数组

    convertToTwodimensional([], 1);
    pageTurnerFixedLength

    (arr: any[], len = 5): [NextAndPreviousType, NextAndPreviousFn, NextAndPreviousFn]

    固定长度的分页器函数,最后一页不满条数,补满条数

    usage

    export type NextAndPreviousType = [any[], boolean, boolean];
    export type NextAndPreviousFn = () => NextAndPreviousType;
    
    const [[pageData, canNext, canPrevious], next, previous] = pageTurnerFixedLength(rows, 5);
    // pageData 当前分页数据  any[];
    // canNext 是否可点击下一页  boolen
    // canPrevious 是否可点击上一页
    // next 获取下一页数据的函数 范围值也是pageData, canNext, canPrevious
    // previous 获取上一页数据的函数 范围值也是pageData, canNext, canPrevious

    list2tree

    (props: TraverseTreeProps): Array<TreeNode>

    将数组根据id pid组装成tree

    const listData = [{ id: 2, pid: 1 }, {id: 10, pid: 3},{ id: 3, pid: 2 }, { id: 4, pid: 6 },{ id: 5, pid: 4 },  {id: 7, pid: 9}];
    
    const listTreeData: any = [
        {
          "id": 2,
          "pid": 1,
          "children": [
            {
              "id": 3,
              "pid": 2,
              "children": [
                {
                  "id": 10,
                  "pid": 3,
                  "children": []
                }
              ]
            }
          ]
        },
        {
          "id": 4,
          "pid": 6,
          "children": [
            {
              "id": 5,
              "pid": 4,
              "children": []
            }
          ]
        },
        {
          "id": 7,
          "pid": 9,
          "children": []
        }
      ];
    
    expect(list2tree({
      list: listData, 
      options: {idKey: 'id', pidKey: 'pid', childrenKey: 'children'}
    })).toStrictEqual(listTreeData);

    tree

    findTreeOrder

    (tree: Array<any>, rely: RelyFn, options?: TreeOptions): Array<number>

    根据rely返回的条件,查找树的位置

    type TreeOptions = {
    	childrenKey: string;
    }
    const treeData = [{
    	id: 1,
    	children: [{
    		id: 11,
    		children: [{
    			id: 7,
    		}]
    	}, {
    		id: 11
    	}, {
    		id: 7
    	}]
    }, {
    	id: 2,
    	children: [{
    		id: 11,
    	}, {
    		id: 11
    	}, {
    		id: 6
    	}]
    }, {
    	id: 3,
    }, {
    	id: 4,
    }, {
    	id: 5,
    	children: [{
    		id: 6,
    		children: [{
    			id: 7,
    		}, {
    			id: 8,
    		}]
    	}]
    }];
    
    findTreeOrder(treeData, (item) => {
    			return item.id === 7;
    		});  // [0, 0, 0]
    findSubtreeByOrder

    (tree: any, order: Array<number>, options?: TreeOptions): TreeNode

    根据order顺序 查找treeNode

    findSubtreeByOrder(treeData, [1, 2]); // {id: 6}
    mapTree

    (tree: any[], callBack: TraverseTreeCallBack, options?: TreeOptions):TreeNode

    遍历树,并根据callBack处理遍历中的数据,默认子级字段是children

    const treeData1 =  [{
        id: 1,
        children: [{
            id: 11,
            children: [{
                id: 7,
            }]
        }, {
            id: 11
        }, {
            id: 7
        }]
    }];
    
    const treeData2 = mapTree(treeData1, (item) => {
        return Object.assign({}, item, item.id === 1 ? {name: `${item.id}`} : {});
    });
    
    const treeData2 = [{
    	id: 1,
    	__path: '0',
    	name: '1',
    	children: [{
    		id: 11,
    		__path: '0-0',
    		children: [{
    			__path: '0-0-0',
    			id: 7,
    		}]
    	}, {
    		__path: '0-1',
    		id: 11
    	}, {
    		__path: '0-2',
    		id: 7
    	}]
    }];

    arr2enum: (arr: object[], keyProp?: string, labelPro?: string): object

    数组转换成基于object形式的枚举, 比如可以用于生成antd procomponents里的selectEnum

    const res = await loadOptions()
    const optionEnum = arr2enum(res.data, 'deviceId', 'deviceName')

    binarySearch: (arr: any[], compare: (element: any) => number, start?: number, end?: number): index

    二分查找法获取有序数组的匹配元素下标, 失败返回-1, compare函数返回0则匹配成功, 大于0表示往左继续匹配, 小于0表示往右继续匹配

    binarySearch(arr, e => e[0] - 3333)

    Keywords

    none

    Install

    npm i @gaopeng123/utils.array

    DownloadsWeekly Downloads

    6

    Version

    0.5.3

    License

    MIT

    Unpacked Size

    34.4 kB

    Total Files

    17

    Last publish

    Collaborators

    • gaopeng123