1.0.1 • Published 1 year ago

@liuqisiwu/utils v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Utils

Coveralls GitHub License

😃 简介 Introduction

整理前端日常开发工作中所用到的utils。 Organize utils used in daily front-end development work.

😊 安装 Installation

$ npm i --save @liuqisiwu/utils
# or
$ yarn add @liuqisiwu/utils
# or
$ pnpm add @liuqisiwu/utils

😏 使用 Usage

ESModule:

import { isEmpty } from '@liuqisiwu/utils';
console.log(isEmpty({})) // => true
console.log(isEmpty([])) // => true
console.log(isEmpty([1])) // => false

CommonJS:

const { checkType } = require('@liuqisiwu/utils')
console.log(checkType('1')) // => 'String'
console.log(checkType([])) // => 'Array'

In a browser:

download index.umd.js in release

<script src="./index.umd.js"></script>
<script>
    // $utils => umd module name
    console.log($utils.isEmpty([])) // => true
</script>

😉 功能 Functions

数据校验

checkType

校验数据类型

import { checkType } from '@liuqisiwu/utils'

console.log(checkType('1'))         // => 'String'
console.log(checkType(['1']))       // => 'Array'
console.log(checkType({}))          // => 'Object'
console.log(checkType(new Map()))   // => 'Map'

isEmpty

判断对象、数组或者其他集合是否为空

import { isEmpty } from '@liuqisiwu/utils'

console.log(isEmpty('1'))         // => false
console.log(isEmpty(['1']))       // => false
console.log(isEmpty(null))        // => true
console.log(isEmpty({}))          // => true
console.log(isEmpty(new Map()))   // => true

文件下载

downloadByUrl

通过url下载文件

import { downloadByUrl } from '@liuqisiwu/utils';

downloadByUrl('http://www.xxxxx.com', 'filename');

downloadByFileStream

通过文件流下载文件

import { downloadByFileStream } from '@liuqisiwu/utils';

downloadByFileStream('file stream', 'filename', 'MIME type');

数据处理

flatToTree

将扁平数组转换为树形结构的方法

参数是否必需类型说明
listtruearray需要转换为树形结构的数组
optionfalseTreeTransformOption如下 TreeTransformOption

TreeTransformOption

属性是否必需类型说明默认值
idfalsestring指定节点的id为节点对象的某个属性值'id'
parentfalsestring指定节点的父级id为节点对象的某个属性值'pid'
childrenfalsestring指定节点的子级为节点对象的某个属性值'children'
import { flatToTree } from '@liuqisiwu/utils';

const flatList = [
  { id: 1, label: '1', parentId: 0 },
  { id: 2, label: '2', parentId: 1 },
  { id: 3, label: '3', parentId: 0 },
  { id: 4, label: '4', parentId: 3 },
  { id: 5, label: '5', parentId: 4 },
];
const result = flatToTree(flatList, { parent: 'parentId' });
console.log(result);
// => [
//  {
//     id: 1,
//     label: '1',
//     parentId: 0,
//     children: [{ id: 2, label: '2', parentId: 1 }]
//   },
//   {
//     id: 3,
//     label: '3',
//     parentId: 0,
//     children: [
//        {
//          id: 4,
//          label: '4',
//          parentId: 3,
//          children: [{ id: 5,label: '5',parentId: 4 }]
//        }
//     ]
//   }
// ];

findNodes

在树形结构中找出所有符合条件的节点

参数是否必需类型说明
treetruearray需要查询的数组
cbtrue(node: Record<string, any>) => boolean查询条件
childrenfalsestring指定节点的子级为节点对象的某个属性值
const tree = [
  { id: '1', value: 1, children: [{ id: '1-1', value: 2 }] },
  { id: '2', value: 1 },
  { id: '3', value: 4, children: [{ id: '3-1', value: 1 }] },
]
const result = findNodes(tree, (node) => node.value & 2 === 0)
console.log(result)
// => [
//  { id: '1-1', value: 2 },
//  { id: '3', value: 4, children: [{ id: '3-1', value: 1 }] }
// ]

findBranches

在树形结构中找出所有符合条件的节点所在的分支(到符合条件的节点为止)

参数是否必需类型说明
treetruearray需要查询的数组
cbtrue(node: Record<string, any>) => boolean查询条件
childrenfalsestring指定节点的子级为节点对象的某个属性值
const tree = [
  { id: '1', value: 1, children: [{ id: '1-1', value: 2 }] },
  { id: '2', value: 1 },
]
const result = findBranches(tree, (node) => node.value === 2)
console.log(result)
// => [
//  { id: '1', value: 1, children: [{ id: '1-1', value: 2 }] }
// ]
1.0.1

1 year ago

1.0.0

1 year ago