当前位置:网站首页>JS common utils encapsulation
JS common utils encapsulation
2022-07-27 19:14:00 【·So and so·】
1.1、 download excel Method
/** * @description: download excel * @param res {Blob} Back in the background Blob object * @param suffix file extension * @param name File name */
export function downloadFile(res, suffix = 'xlsx', name = '') {
const blob = new Blob([res], {
type: 'application/vnd.ms-excel;charset=utf-8',
})
const downloadElement = document.createElement('a')
const href = window.URL.createObjectURL(blob) // Create a link to download
downloadElement.href = href
downloadElement.download = name + +new Date() + '.' + suffix
// downloadElement.download = +new Date() + '.xlsx' // File name after download
document.body.appendChild(downloadElement)
downloadElement.click() // Click to download
document.body.removeChild(downloadElement) // Download complete remove element
window.URL.revokeObjectURL(href) // release blob object
}
1.2、 Use
import {
downloadFile} from '@/utils/util'
// ExportSaleRecordsData Change to your own real interface
...
methods: {
// File export
async exportHandle() {
ExportSaleRecordsData().then((res) => {
downloadFile(res, 'xlsx', ' A watch ')
})
},
}
2.1、 Find the required object method in the array object
/** * @description: Find a value of an array object * @param objArray Target object data * @param key An attribute * @param value value */
export function parseArray(objArray, key, value) {
for (let i in objArray) {
let element = objArray[i]
if (typeof element === 'object') {
let result = parseArray(element, key, value)
if (result) return result
} else {
if (i === key) {
if (element === value) return objArray
}
}
}
}
2.2、 Use
import {
parseArray } from '@/utils/util'
// objArr = [
// {id: 111, name: '111', age: '111', sex: '111'},
// {id: 222, name: '222', age: '222', sex: '222'},
// {id: 333, name: '333', age: '333', sex: '333'},
// {id: 444, name: '444', age: '444', sex: '444'}
// ]
let aaa = parseArray(objArr, 'id', 111)
// aaa Print out as {id: 111, name: '111', age: '111', sex: '111'}
3.1、 Return the value of the first item of the array
/** * @description: Get the first value of the array , If empty, return the corresponding value * @param arr Target array * @param key attribute * @param val The value you want to return when the array is empty ( Default undefined) */
export function getFirstValue(arr, key, val = undefined) {
if (arr.length !== 0) {
if (typeof arr[0] === 'object') {
return arr[0][key]
} else {
return arr[0]
}
} else {
return val
}
}
3.2、 Use
import {
getFirstValue } from '@/utils/util'
// objArr1 = [
// {id: 111, name: '111', age: '111', sex: '111'},
// {id: 222, name: '222', age: '222', sex: '222'},
// ]
let aaa = getFirstValue(objArr1 , 'id')
// aaa Print out as 111
// objArr2 = []
let bbb = getFirstValue(objArr2 , 'id', null)
// bbb Print out as null
4.1、 Array object same item merge processing method
/** * @description: Merge the same items in the array object * @param arr Target array */
export function repArr(arr) {
let map = {
}
let dest = []
for (let i = 0; i < arr.length; i++) {
let ai = arr[i]
if (!map[ai.name]) {
dest.push({
name: ai.name, data: ai.data })
map[ai.name] = ai
} else {
for (let j = 0; j < dest.length; j++) {
let dj = dest[j]
if (dj.name == ai.name) {
dj.data = dj.data.concat(ai.data)
break
}
}
}
}
return dest
},
4.2、 Use
import {
repArr } from '@/utils/util'
// objArr =[
// {name: ' test 1', data: ['10']},
// {name: ' test 2', data: ['15']},
// {name: ' test 1', data: ['20']},
// {name: ' test 2', data: ['25']}
//]
let aaa = repArr (objArr)
// aaa Print out as
// [
// {name: ' test 1', data: ['10', '20']},
// {name: ' test 2', data: ['15', '25']}
// ]
5.1、 Replication method
/** * @description: Copy text messages * @param text Copied text messages * @param callback Callback function */
export function copyText(text, callback) {
var tag = document.createElement('input')
tag.setAttribute('id', 'cp_hgz_input')
tag.value = text
document.getElementsByTagName('body')[0].appendChild(tag)
document.getElementById('cp_hgz_input').select()
document.execCommand('copy')
document.getElementById('cp_hgz_input').remove()
if (callback) {
callback(text)
} else {
Vue.prototype.$message.success(' Replication success ')
}
}
5.2、 Use
import {
copyText } from '@/utils/util'
...
// Click the trigger event method , eject Replication success
copyText('123456')
6.1、 The regular way
/** * mailbox * @param {*} s */
export function isEmail (s) {
return /^([a-zA-Z0-9_-])[email protected]([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
}
/** * Phone number * @param {*} s */
export function isMobile (s) {
return /^(13[0-9]|14[4579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(s)
}
/** * Phone number * @param {*} s */
export function isPhone (s) {
return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
}
/** * amount of money * @param {*} s */
export function isMoney (s) {
// amount of money Only positive numbers are allowed
var exp = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
// amount of money Allow positive (+) negative
// var exp = /(^([+-]?)[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^([+-]?)(0){1}$)|(^([+-]?)[0-9]\.[0-9]([0-9])?$)/
// amount of money Positive and negative numbers are allowed
// var exp = /(^([-]?)[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^([-]?)(0){1}$)|(^([-]?)[0-9]\.[0-9]([0-9])?$)/
if (exp.test(s)) {
return true
} else {
return false
}
}
边栏推荐
- Selenium automated test interview questions family bucket
- WSN Journal indexed by SCI(转)
- Unity显示Kinect捕获的镜头
- Nacos cluster deployment - high availability guarantee
- `this.$ Emit ` the child component passes multiple parameters to the parent component
- Ridis command notes
- The great idea of NS2
- ES6学习笔记(1)——快速入门
- Extension of ES6 value
- 阿里云视频点播服务的开通和使用
猜你喜欢

Sentinel1.8.4 persistent Nacos configuration

自控原理学习笔记-系统稳定性分析(1)-BIBO稳定及Routh判据

Definition of graph traversal and depth first search and breadth first search (2)

Jmeter接口自动化-如何解决请求头Content-Type冲突问题

Unity显示Kinect捕获的镜头

自控原理学习笔记-系统稳定性分析(2)-环路分析及Nyquist-Bode判据

一篇让你掌握线程和线程池,还解决了线程安全问题,确定不看看?

kettle 分列、合并记录

Unity display Kinect depth data

阿里云视频点播服务的开通和使用
随机推荐
正则表达式的扩展
SSM integration
Blog Garden beautification tutorial
js常用utils封装
自控原理学习笔记-系统稳定性分析(1)-BIBO稳定及Routh判据
express
kettle入门级操作第一篇(读取excel、输出excel)
Unity学习笔记(刚体-物理-碰撞器-触发器)
IDEA成功连接Database但不显示表怎么办
There is a problem with the time zone when the idea connects to the database. The server returns invalid timezone is red Need to set ‘serverTimezone‘ property.
Unity-显示Kinect深度数据
如何用自动化测试搞垮团队
sql 时间处理(SQL SERVER\ORACLE)
JMeter interface automation - how to solve the content type conflict of request headers
贪心法,拟阵和亚模函数(refer)
WSN journal indexed by SCI
NPM basic use
Unity学习笔记(实现传送带)
C interface knowledge collection suggestions collection
The great idea of NS2