UNPKG

apass-opensdk-hugong

Version:

飞书Apass低代码平台-飞书开放平台-相关的接口整合和常用的方法整合

89 lines (79 loc) 2.06 kB
const Url = require('./url') const File_ = require('./file_') const Date_ = require('./date_') class Utils { #hg = null constructor(hg) { this.#hg = hg this.url = new Url(hg) this.file = new File_(hg) this.date = new Date_(hg) } /** * 数组分割 * @param {*} list * @param {*} chunkSize 分割长度 * @param {*} callback(item) 分割后回调(不传递则方法返回切割长度后的数组) * @returns */ async splitArray(list, chunkSize, callback) { const result = []; this.#hg.log4('splitArray len ', list.length) for (let i = 0; i < list.length; i += chunkSize) { const batch = list.slice(i, i + chunkSize) this.#hg.log8('splitArray item ', i, i + chunkSize) if (callback) { await callback(batch) continue } result.push(batch); // 使用 slice 方法按 chunkSize 进行切割 } return result; } toMD5(data) { const crypto = require('crypto'); const hash = crypto.createHash('md5'); hash.update(data); const md5Hash = hash.digest('hex'); this.#hg.log4(`MD5 Hash of "${data}": ${md5Hash}`) return md5Hash } /** * 格式化货币 * @param {*} amount * @param {*} locale * @param {*} currency * @returns */ formatCurrency(amount, locale = 'en-US', currency = 'USD') { return new Intl.NumberFormat(locale, { style: 'currency', currency, }).format(amount); } /** * 分块数组 * @param {*} arr * @param {*} size * @returns */ chunkAll(arr, size) { return Array.from({ length: Math.ceil(arr.length / size) }, (_, i) => arr.slice(i * size, i * size + size)); } /** * 范围生成器 * @param {*} start * @param {*} end * @param {*} step * @returns */ range(start, end, step = 1) { return Array.from({ length: (end - start) / step + 1 }, (_, i) => start + i * step); } /** * 移除重复项 * @param {*} arr * @returns */ unique(arr) { return [...new Set(arr)]; } } module.exports = Utils