2021-08-31 10:24:42 +00:00
|
|
|
/* eslint-disable no-restricted-globals,no-param-reassign,guard-for-in,no-restricted-syntax,no-continue */
|
2021-09-01 12:46:41 +00:00
|
|
|
const mod = {}
|
|
|
|
module.exports = mod
|
2021-08-31 10:24:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check value is number
|
|
|
|
* @param {any} v input value
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2021-09-01 12:46:41 +00:00
|
|
|
mod.isNumber = v => !(!isFinite(v) || v === true || v === false || v === null || v === '')
|
2021-08-31 10:24:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* value to number
|
|
|
|
* @param {object} v source value
|
|
|
|
* @param {number=} defVal default value
|
|
|
|
* @param {number=} min range min
|
|
|
|
* @param {number=} max range max
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
mod.toNumber = (v, defVal, min, max) => {
|
2021-09-01 12:46:41 +00:00
|
|
|
let defaultVal = defVal
|
|
|
|
let inVal = v
|
|
|
|
if (!mod.isNumber(defVal)) defVal = 0
|
|
|
|
if (typeof defVal === 'string') defaultVal = parseFloat(defVal)
|
2021-08-31 10:24:42 +00:00
|
|
|
const minVal = !mod.isNumber(min) ? null : typeof min === 'string' ? parseFloat(min) : min; // eslint-disable-line
|
|
|
|
const maxVal = !mod.isNumber(max) ? null : typeof max === 'string' ? parseFloat(max) : max; // eslint-disable-line
|
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
if (!mod.isNumber(v)) return defaultVal
|
|
|
|
if (typeof v === 'string') inVal = parseFloat(v)
|
|
|
|
if (minVal !== null && inVal < minVal) inVal = min
|
|
|
|
if (maxVal !== null && inVal > maxVal) inVal = max
|
2021-08-31 10:24:42 +00:00
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
return inVal
|
|
|
|
}
|
2021-08-31 10:24:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @exports
|
|
|
|
* @typedef pageObject
|
|
|
|
* @prop {number} total page number
|
|
|
|
* @prop {number} page number
|
|
|
|
* @prop {number} count all count number
|
|
|
|
* @prop {number} limit query limit item
|
|
|
|
* @prop {number} offset query offset number
|
|
|
|
*
|
|
|
|
* calc page object
|
|
|
|
* @param {number} argCount all count
|
|
|
|
* @param {number} argPage page number
|
|
|
|
* @param {number} argMaxItem per page item show
|
|
|
|
* @return {pageObject}
|
|
|
|
*/
|
|
|
|
mod.calcPage = (argCount, argPage, argMaxItem = 10) => {
|
2021-09-01 12:46:41 +00:00
|
|
|
const count = mod.toNumber(argCount, 0, 0)
|
|
|
|
let page = mod.toNumber(argPage, 1, 1)
|
|
|
|
const maxItem = mod.toNumber(argMaxItem, 10, 1)
|
|
|
|
|
|
|
|
let total = Math.ceil(count / maxItem)
|
|
|
|
if (total < 1) total = 1
|
|
|
|
if (page > total) page = total
|
|
|
|
let offset = (page - 1) * maxItem
|
|
|
|
if (offset > count) offset = count
|
|
|
|
const limit = maxItem
|
|
|
|
|
|
|
|
const pageObject = { total, page, count, offset, limit }
|
2021-08-31 10:24:42 +00:00
|
|
|
for (const key in pageObject) {
|
2021-09-01 12:46:41 +00:00
|
|
|
pageObject[key] = mod.toNumber(pageObject[key])
|
2021-08-31 10:24:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
return pageObject
|
|
|
|
}
|
2021-08-31 10:24:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* deep copy object
|
|
|
|
* @param {any} src
|
|
|
|
* @return {any}
|
|
|
|
*/
|
|
|
|
mod.copyObject = src => {
|
2021-09-01 12:46:41 +00:00
|
|
|
if (typeof src !== 'object') return src
|
2021-08-31 10:24:42 +00:00
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
const isArray = Array.isArray(src)
|
|
|
|
const copy = isArray ? [] : {}
|
2021-08-31 10:24:42 +00:00
|
|
|
for (let it in src) {
|
|
|
|
// @ts-ignore
|
2021-09-01 12:46:41 +00:00
|
|
|
if (isArray) it = parseInt(it, 10)
|
|
|
|
if (typeof src[it] !== 'object') copy[it] = src[it]
|
|
|
|
else copy[it] = mod.copyObject(src[it])
|
2021-08-31 10:24:42 +00:00
|
|
|
}
|
2021-09-01 12:46:41 +00:00
|
|
|
return copy
|
|
|
|
}
|
2021-08-31 10:24:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {(string|number)} SelectObjectAlias
|
|
|
|
* @description pass string and length > 0 rename key to alias, other keep key name
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @exports
|
|
|
|
* @typedef {{[key: string]: SelectObjectAlias }} SelectObjectParam
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* select object keys
|
|
|
|
* @param {Object<string, any>} obj
|
|
|
|
* @param {SelectObjectParam} param
|
|
|
|
*/
|
|
|
|
mod.selectObject = (obj, param) => {
|
2021-09-01 12:46:41 +00:00
|
|
|
if (typeof obj !== 'object' || typeof param !== 'object') throw new Error('input arg wrong')
|
2021-08-31 10:24:42 +00:00
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
let newObj = {}
|
2021-08-31 10:24:42 +00:00
|
|
|
|
|
|
|
for (const key in param) {
|
2021-09-01 12:46:41 +00:00
|
|
|
const strs = key.split('.')
|
|
|
|
const alias = param[key]
|
2021-08-31 10:24:42 +00:00
|
|
|
if (strs.length > 1) {
|
2021-09-01 12:46:41 +00:00
|
|
|
if (!(strs[0] in obj)) continue
|
2021-08-31 10:24:42 +00:00
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
newObj = { ...newObj, ...mod.selectObject(obj[strs[0]], { [strs.slice(1).join('.')]: alias }) }
|
2021-08-31 10:24:42 +00:00
|
|
|
}
|
2021-09-01 12:46:41 +00:00
|
|
|
const toAlias = param[key] && typeof param[key] === 'string'
|
|
|
|
if (!(key in obj)) continue
|
2021-08-31 10:24:42 +00:00
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
newObj[toAlias ? alias : key] = obj[key]
|
2021-08-31 10:24:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 12:46:41 +00:00
|
|
|
return newObj
|
|
|
|
}
|