简介
@beisen/ocean-helper
是一个 npm 包,主要是为了帮助前端开发者在进行开发过程中更加高效、方便地处理一些常用的业务逻辑。本教程将详细介绍如何使用该包。
用法
- 安装
npm install @beisen/ocean-helper
- 导入
import helper from '@beisen/ocean-helper';
- 使用
3.1. sortBy
用于对一个数组进行排序。支持对数组中的数字和字符串进行排序。
参数
arr
:Array,需要排序的数组key
:String,对象中排序的属性名称order
:String,排序规则,可选值为"asc"
和"desc"
,默认值为"asc"
示例
const arr = [{ id: 1, name: 'Tom' }, { id: 2, name: 'Jerry' }, { id: 3, name: 'Alice' }]; const sortedArr = helper.sortBy(arr, 'id', 'asc'); console.log(sortedArr); // [{ id: 1, name: 'Tom' }, { id: 2, name: 'Jerry' }, { id: 3, name: 'Alice' }]
3.2. formatMoney
用于将一个数字格式化成金额的形式。
参数
num
:Number/String,需要格式化的数字fixed
:Number,保留的小数位数,默认值为2
示例
const result = helper.formatMoney(10000.123456); console.log(result); // "10,000.12"
3.3. getUrlParam
用于获取当前页面 URL 中的指定参数值。
参数
name
:String,需要获取的参数名称
返回值
获取到的参数值。如果该参数不存在,则返回 null
。
示例
// 当前页面的 URL 为 http://localhost:8080/?id=123&name=Tom const id = helper.getUrlParam('id'); console.log(id); // "123" const age = helper.getUrlParam('age'); console.log(age); // null
3.4. debounce
用于控制一个函数在一定时间内连续触发时仅执行一次。
参数
func
:Function,需要控制的函数delay
:Number,控制的时间间隔,单位为毫秒
示例
-- -------------------- ---- ------- -------- ----------- - --------------------------- - ----- ----------------- - -------------------------- ------ -------------------- -------------------- -------------------- -- -----------
总结
以上就是 @beisen/ocean-helper
这个 npm 包的使用方法。通过使用它提供的方法,能够有效地提高我们的开发效率,让我们在进行业务开发时更加得心应手。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/5eedabebb5cbfe1ea06108cf