Cjs 说明
Cjs USE
// npm install cxh-cjs
// ES6环境
// 全部引入 { Cjs, CjsArray, CjsObject },需要注意的是,此处不用解构赋值是因为export default通过babel转义后丢失作用域,所以会导致解构赋值未定义的情况
import CjsObj from 'cxh-cjs'
// 推荐按需引入,例如
import Cjs from 'cxh-cjs/dist/Cjs'
import CjsArray from 'cxh-cjs/dist/CjsArray'
import CjsObject from 'cxh-cjs/dist/CjsObject'
import CjsFile from './CjsFile'
// ES5环境
var CjsObj = require('cxh-cjs').default
// 引入之后使用
const Cjs = new Cjs()
方法immutable()
描述
该方法可以在不改变原数据的情况下做一些数据处理,即使用Proxy实现一个轻量级不可变数据的immutable方法
输入参数 | 描述 |
---|
target | 原数据,预期不可变的数据 |
handler | 需要对数据做处理的函数 |
immutable() USE
const Cjs = new Cjs()
const target = {
id: 0,
name: 'Skycius Chan'
}
const current = Cjs.immutable(target,_target => {
_target.id = 1
})
/**
* target = {
* id: 0,
* name: 'Skycius Chan'
* }
*
* current = {
* id: 1,
* name: 'Skycius Chan'
* }
* /
方法lineToCamel()
描述
中划线转驼峰
lineToCamel() USE
const Cjs = new Cjs()
Cjs.lineToCamel('cjs-test') // cjsTest
方法camelToLine()
描述
驼峰命转中划线
camelToLine() USE
const Cjs = new Cjs()
Cjs.camelToLine('cjsTest') // cjs-test
方法parseParam()
描述
解析URL中的参数转为对象
parseParam() USE
const Cjs = new Cjs()
Cjs.parseParam('http://www.cjs.com/?id=1&code=2') // {id:1,code:2}
方法deepClone()
描述
深拷贝
输入参数 | 描述 |
---|
param | 需要深拷贝的参数 |
type | 深拷贝模式,分为simple和major两种,各有使用场景,默认为simple |
deepClone() USE
const Cjs = new Cjs()
Cjs.deepClone('Cjs') // Cjs
方法debounce()
描述
函数防抖
输入参数 | 描述 |
---|
func | 需要防抖的函数 |
wait | 延迟执行毫秒数 |
immediate | true 表立即执行,false 表非立即执行 |
debounce() USE
<!-- html -->
<button @click="click">点击</button>
<!-- js -->
click() {
const func = () => {
console.log('Cjs')
}
return new Cjs().debounce(func,2000,true)
// 只有在停止点击2000毫秒后才会重新触发func函数,且第一次会立即执行
}
方法throttle()
描述
函数节流
输入参数 | 描述 |
---|
func | 需要防抖的函数 |
wait | 延迟执行毫秒数 |
type | 'stamp' 表时间戳版,'timer' 表定时器版 |
throttle() USE
const func = () => {
console.log('Cjs')
}
const Cjs = new Cjs().throttle(func,2000,'stamp')
setInterval(Cjs,500)
// 每隔2000毫秒才执行一次func函数,且使用的是时间戳版本
方法dateFormat()
描述
日期格式化
输入参数 | 描述 |
---|
format | 日期格式,例如:'yyyy/MM/dd HH:mm:ss' 返回 2019/08/14 14:46:20 |
time | 时间戳,可选,若不指定则返回当前时间戳的指定格式 |
dateFormat() USE
const Cjs = new Cjs().dateFormat('yyyy/MM/dd HH:mm:ss')
// 返回 2019/08/14 14:46:20
CjsArray 说明
CjsArray USE
const CjsArray = new CjsArray(array)
方法sort()
描述
对称冒泡排序,在传统冒泡排序基础上提升一倍性能,时间复杂度O(n²),稳定,不采用希尔排序算法是因为在数据量不大的情况下体现不出希尔排序的效率,且在增量序列动态变化的情况下希尔排序效率不稳定
输入参数 | 类型 | 描述 |
---|
type | String | 排序类型,默认asc升序,可选desc、asc |
返回值参数 | 类型 | 描述 |
---|
array | Array | 排序之后的数组 |
sort() USE
const CjsArray = new CjsArray([2,10,12,1,7])
CjsArray.sort() // [1,2,7,10,12]
CjsArray.sort('desc') // [12,10,7,2,1]
方法toString()
描述
数组以特定符号分割转成字符串
toString() USE
const CjsArray = new CjsArray([0,1,2])
CjsArray.toString(',') // 0,1,2
方法deepClone()
描述
深拷贝
deepClone() USE
const CjsArray = new CjsArray([0,1,2])
CjsArray.deepClone() // [0,1,2]
方法length()
描述
返回数组长度
length() USE
const CjsArray = new CjsArray([0,1,2])
CjsArray.length() // [2,1,0]
方法reverse()
描述
数组倒序
reverse() USE
const CjsArray = new CjsArray([0,1,2])
CjsArray.reverse() // 3
方法max()
描述
求最大值,且返回最大值出现的索引值
返回值参数 | 类型 | 描述 |
---|
max | Number | 最大值 |
indexs | Array | 最大值出现的索引值数组 |
max() USE
const CjsArray = new CjsArray([0,1,2])
CjsArray.max() // {max:2,indexs:[2]}
方法min()
描述
求最小值,且返回最小值出现的索引值
返回值参数 | 类型 | 描述 |
---|
min | Number | 最小值 |
indexs | Array | 最小值出现的索引值数组 |
min() USE
const CjsArray = new CjsArray([0,1,2])
CjsArray.min() // {min:0,indexs:[0]}
CjsObject 说明
CjsObject USE
const CjsObject = new CjsObject(object)
方法deepClone()
描述
深拷贝
deepClone() USE
const CjsObject = new CjsObject({key:'CjsObject'})
CjsObject.deepClone() // {key:'CjsObject'}
CjsFile 说明
CjsFile USE
const CjsFile = new CjsFile()
方法read()
描述
读取文件并返回内容
输入参数 | 类型 | 描述 |
---|
path | String | 需要读取的文件路径 |
返回值参数 | 类型 | 描述 |
---|
data | String | 返回的文件内容 |
read() USE
const CjsFile = new CjsFile()
CjsFile.read('./README.md').then(res => {
console.log(res)
})
// README.md的内容
方法write()
描述
写入文件
输入参数 | 类型 | 描述 |
---|
path | String | 需要写入的文件路径 |
data | String | 写入的文件内容 |
write() USE
const CjsFile = new CjsFile()
CjsFile.write('./README.md','#readme').then(res => {
console.log(res)
})
方法watch()
描述
监听文件变化,有修改文件和重命名
输入参数 | 类型 | 描述 |
---|
path | String | 需要监听的文件路径 |
func | Function | 对监听的文件做处理 |
watch() USE
const CjsFile = new CjsFile()
CjsFile.watch('./README.md',(eventType, filename)=>{
console.log(eventType, filename)
// 接收两个参数eventType(是 'rename' 或 'change'), filename(是触发事件的文件的名称)
}).then(res => {
console.log(res)
})
方法delete()
描述
删除文件
输入参数 | 类型 | 描述 |
---|
path | String | 需要删除的文件路径 |
delete() USE
const CjsFile = new CjsFile()
CjsFile.delete('./README.md').then(res => {
console.log(res)
})