1.2.0 • Published 2 years ago

@zero-org/utils v1.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Getting started

Install and import the package:

npm install --save @zero-org/utils
import * as Utils from '@zero-org/utils'
Utils.isEmpty('a')
import { isEmpty } from '@zero-org/utils'
isEmpty('a')

API reference

getObjType

获取对象类型。

入参说明类型
obj需要获取类型的对象any

Example:

getObjType(null) // Null
getObjType(undefined) // Undefined
getObjType('abc') // String

isEmpty

判断对象是否为空。

入参说明类型
obj判断的对象any

Example:

isEmpty();                 // true
isEmpty(null);             // true
isEmpty(undefined);        // true
isEmpty('');               // true
isEmpty('hello');          // false
isEmpty([]);               // true
isEmpty([1, 2, 3]);        // false
isEmpty(Symbol('test'));   // false
isEmpty({});               // true
isEmpty({ key: 'value' }); // false

getVal

获取某个对象的多层属性的值。

入参说明类型
target获取的属性对象any
path属性路径,用.分割string
defaultVal默认值any

Example:

var obj = {
    detail: {
        address: {
            country: 'china'
        }
    }
}

var country = getVal(obj, 'detail.address.country', '') // china

getFileExtension

获取文件扩展名,不包含.,扩展名有可能为空。

入参说明类型
fileName文件名string

Example:

getFileExtension('测试.xlsx') // xlsx

deleteFromArray

根据键值对从数组中删除特定项。

入参说明类型
array数组any[]
keystring
valueany

Example:

var array = [{
    id: '01'
    name: '张三'
}, {
    id: '02',
    name: '李四'
}]
var _array = deleteFromArray(array, 'id', '02')

// 返回值和原array都等于下面的数组
[{
    id: '01'
    name: '张三'
}]

getBase64

获取文件的base64。

入参说明类型
file文件File

Example:

const blob = new Blob(['Hello, File!'], { type: 'text/plain' })
const fileName = 'hello.txt'
const file = new File([blob], fileName)
getBase64(file).then(base64 => {
    console.log(base64) // data:text/plain;base64,SGVsbG8sIEZpbGUh
})

downloadWithBlob

将Blob对象转换为URL并下载。

入参说明类型
blob要被转换为URL并下载的Blob对象Blob
fileName下载后保存的文件名string

Example:

const blob = new Blob(['Hello, File!'], { type: 'text/plain' })
const fileName = 'hello.txt'
downloadWithBlob(blob, fileName) // 下载的文件名称为hello.txt, 内容为'Hello, File!'

downloadWithUrl

通过url下载文件。

入参说明类型
url下载的url地址string
fileName下载后保存的文件名string

Example:

const blob = new Blob(['Hello, File!'], { type: 'text/plain' })
const fileName = 'hello.txt'
const url = window.URL.createObjectURL(new Blob([blob]))
downloadWithUrl(url, fileName) // 下载的文件名称为hello.txt, 内容为'Hello, File!'

eachTreeObject

遍历数组并对每个元素执行回调函数的函数。如果回调函数返回false,则立即停止遍历并返回true。

入参说明类型
array需要遍历的数组any[]
cb回调函数。如果返回false,则立即停止遍历并返回trueFunction
childKey指定子元素的键名。默认为'children'string

Example:

const array = [
    { id: 1, children: [{ id: 2 }, { id: 3 }] },
    { id: 4, children: [{ id: 5 }, { id: 6 }] },
]

const list = []

eachTreeObject(array, (item) => {
    if (item.id === 2) {
        return false
    }
    list.push(item.id)
})
// list = [1]

parseJson

将符合json格式的字符串格式化为json。

入参说明类型
str待转换的字符串string
defaultValue转换失败后的默认值any

Example:

const input = '{"key1": {"key2": "value"}}'
const result = parseJson(input) 
// 转换后
{
    key1: {
        key2: 'value'
    }
}

transTreeData

用于将一个树形结构的数据数组进行转换。

入参说明类型
array要转换的对象数组any[]
cb回调函数,在该函数中进行健值映射Function
childKey指定子元素的键名。默认为'children'string

Example:

const array = [{
    uuid: '01',
    name: '节点1',
    child: [{
        uuid: '03',
        name: '节点03'
    }]
}]
transTreeData(array, (obj) => {
   return {
       key: obj.uuid,
       value: obj.uuid,
       title: obj.name
   } 
}, 'child')

// 转换后
[{
    key: '01',
    value: '01',
    title: '节点1',
    children: [{
        key: '03',
    	value: '03',
        title: '节点03'
    }]
}]

logVersionInfo

打印带有特定样式的版本信息的函数。版本信息将以特定的颜色和背景色显示,并且会在控制台输出。

入参说明类型
versionInfo需要打印的版本信息string

Example:

logVersionInfo('文字客服: 2.0.7')

License

The MIT License (MIT). Please see License File for more information.

1.2.0

2 years ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago