1. Obtain the library name

export function libraryNameString(libraryList, libraryString) {
    let string = '';
    //let serviceArray = [];
    if (libraryList) {
        libraryList.forEach(i => {
            if (libraryString ===  i.code) {
                string = i.name
                //serviceArray.push(i.name)
            }
        });
    }
    return string;
}
Copy the code

2. Obtain the file name extension

@param {String} filename */ export function getExt(filename) {if (typeof filename == 'String ') {return  filename .split('.') .pop() .toLowerCase() } else { throw new Error('filename must be a string type') } }Copy the code

3. Copy content to clipboard

 export function copyToBoard(value) {
    const element = document.createElement('textarea')
    document.body.appendChild(element)
    element.value = value
    element.select()
    if (document.execCommand('copy')) {
        document.execCommand('copy')
        document.body.removeChild(element)
        return true
    }
    document.body.removeChild(element)
    return false
}
Copy the code

4. Array deduplication

@param {*} arr */ export function uniqueArray(arr) {if (! Array.isArray(arr)) { throw new Error('The first parameter must be an array') } if (arr.length == 1) { return arr } return [...new Set(arr)] }Copy the code

5. Retain several digits after the decimal point. The default value is 2 digits

Export function cutNumber(number, no = 2) {if (typeof number! = 'number') { number = Number(number) } return Number(number.toFixed(no)) }Copy the code

6. Convert the object to formData

Export function cutNumber(number, no = 2) {if (typeof number! = 'number') { number = Number(number) } return Number(number.toFixed(no)) } function getFormData(object) { const formData = new FormData() Object.keys(object).forEach(key => { const value = object[key] if (Array.isArray(value)) { value.forEach((subValue, i) => formData.append(key + `[${i}]`, subValue) ) } else { formData.append(key, object[key]) } }) return formData }; let req={ file:'1.txt', userId:1, phone:'15198763636', //... } fetch(getFormData(req))Copy the code

7. Simple deep copy

/** * export * @param {*} obj * @returns */ export function deepCopy(obj) {if (typeof obj! = 'object') { return obj } if (obj == null) { return obj } return JSON.parse(JSON.stringify(obj)) }Copy the code

8. Generate random strings

@param {*} chars */ export function uuid(length, chars) { chars = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' length = length || 8 var result = '' for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)] return result }Copy the code

The original address