60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
export const apiUrl = 'https://bot.trj.tw'
|
|
|
|
export const chkObject = function (key = '', type = '', empty = false) {
|
|
const uuidChk = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
if (!(key in this.body)) return false
|
|
|
|
switch (type) {
|
|
case 'string':
|
|
if (typeof this.body[key] !== 'string' || (!empty && !this.body[key].trim())) return false
|
|
break
|
|
case 'number':
|
|
if (!isFinite(this.body[key])) return false
|
|
break
|
|
case 'boolean':
|
|
if (typeof this.body[key] !== 'boolean') return false
|
|
break
|
|
case 'array':
|
|
if (!Array.isArray(this.body[key]) || (!empty && this.body[key].length === 0)) return false
|
|
break
|
|
case 'uuid':
|
|
if (typeof this.body[key] !== 'string') return false
|
|
if (!empty && this.body[key] === '') return false
|
|
if (!empty && !uuidChk.test(this.body[key])) return false
|
|
break
|
|
case 'object':
|
|
if (typeof this.body[key] !== 'object') return false
|
|
try {
|
|
let str = JSON.stringify(this.body[key])
|
|
JSON.parse(str)
|
|
} catch (err) {
|
|
return false
|
|
}
|
|
break
|
|
default:
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
/**
|
|
* parse number
|
|
* @param {any} v input number
|
|
* @param {number} def default number
|
|
* @param {number} min min number
|
|
* @param {number} max max number
|
|
* @return {number}
|
|
*/
|
|
export const toInt = (v, def = 0, min = null, max = null) => {
|
|
if (!isFinite(def)) def = 0
|
|
if (typeof def === 'string') def = parseInt(def)
|
|
min = isFinite(min) ? (typeof min === 'string' ? parseInt(min) : min) : null
|
|
max = isFinite(max) ? (typeof max === 'string' ? parseInt(max) : max) : null
|
|
if (!isFinite(v)) return def
|
|
if (typeof v === 'string') v = parseInt(v)
|
|
if (min !== null && v < min) v = min
|
|
if (max !== null && v > max) v = max
|
|
return v
|
|
}
|