npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zero-org/utils

v1.2.0

Published

常用的工具函数集合

Downloads

9

Readme

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[] | | key | 键 | string | | value | 值 | any |

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,则立即停止遍历并返回true | Function | | 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.