工做中經常使用的js函數

yh-js-Kit

Commonly used js tool functions
我將工做中經常使用的一些js函數封裝成了npm package,若是使用的話,能夠直接npm i yh-js-kit --save。
項目採用的是rollup打包,輸出的format有umd和esm使用很方便。
複製代碼

git: github.com/Michaelhooo…android

npm i yh-js-kit --save
複製代碼
import { timeFormat, setCookie } from 'yh-js-kit'

const obj = require('yh-js-kit')
複製代碼

AboutCookie

  • setCookie(name, value)
  • getCookie(name)
  • delCookie(name)

arrayFlattening

  • steamroller
let newArray = steamroller([1,2,[4,5,[1,23]]])
console.log(newArray) //[1, 2, 4, 5, 1, 23]
複製代碼

arraySortByKey

  • arraySortByKey
let ary = [{name:1},{name:12},{name:10}]
 ary.sort(arraySortByKey('name'))
 console.log(ary) // [ { name: 1 }, { name: 10 }, { name: 12 } ]
複製代碼

changeDigitalDisplay

  • formatThousand
let demo = formatThousand(1234567)
console.log(demo) //1,234,567
複製代碼

complexArraysToRepeat

  • unique Deduplication of complex arrays
let array = [
  { name: "yu", artist: "air" },
  { name: "yu", artist: "air" },
  { name: "qing", artist: "zhou" },
  { name: "qing", artist: "zhou" },
];
unique(array, 'name') 
//[ { name: 'yu', artist: 'air' }, { artist: 'zhou', name: 'qing' } ]
複製代碼

countdown

  • getEndTime
console.log(getEndTime('2020/8/8 8:0:0'))
//返回倒計時,距離2020/8/8 8:0:0還剩多久
//{ day: 467, hour: 14, minutes: 35, seconds: 49 }
複製代碼

deepCopy

  • deepCopy
let ary = [{name: 1},{name: 2}]
let ary2 = deepCopy(ary)
console.log(ary2)
// [{name: 1},{name: 2}] 

let ary3 = deepCopy(ary, {name: 3})
console.log(ary3)
// [{name: 3},{name: 3}] 
複製代碼

filterParams

  • filterParams When the value is 0,null,undefined, it gets filtered
console.log(filterParams({a:0, b:1, c:"010", d:null, e:undefined,f:false}) )
// {b:1, c:"010"}
複製代碼

letterSize

  • changeCase
// 1:首字母大寫 / The first child is capitalized
// 2:首子母小寫 / The first child is lowercase
// 3:大小寫轉換 / Case conversion
// 4:所有大寫 / All caps
// 5:所有小寫 / All lowercase
changeCase('Michael',1)
複製代碼

moneyChange

  • moneyChange
moneyChange(168752632)
//"人民幣壹億陸仟捌佰柒拾伍萬貳仟陸佰叄拾貳元整"

moneyChange(1682)
//"人民幣壹仟陸佰捌拾貳元整"

moneyChange(-1693)
//"欠人民幣壹仟陸佰玖拾叄元整"
複製代碼

pagingComponent

  • getPagedArray
/** * * Get paged array * @param {*} total The total number of pages * @param {*} cur The current page 3 * @param {*} around 1 2 3 4 5 around = 2 * @returns */
 
 console.log(getPagedArray(10, 6 , 2))
 //[ 1, '...', 4, 5, 6, 7, 8, 9, 10 ]
複製代碼

timeFormat

  • timeFormat
/** * * * @param {*} {time, format = 'yyyy-MM-dd HH:mm'} * @returns */
 let time = new Date()
 console.log(timeFormat(time, 'yyyy-MM-dd HH:mm'))
 //2019-04-28 17:34
複製代碼

uaJudgment

  • browserInfo type: android, iphone, ipad, weixin return: Boolean

git: github.com/Michaelhooo…git

相關文章
相關標籤/搜索