This commit is contained in:
bobobobo
2026-02-04 23:01:03 +08:00
parent f59e09f4ee
commit 737d8e9a69
25 changed files with 12456 additions and 106 deletions

View File

@@ -0,0 +1,3 @@
import { RCIMIWLogLevel } from "../RCIMDefines";
export declare function log(level: RCIMIWLogLevel, msg: string): void;
export declare function setLogLevel(level: RCIMIWLogLevel): void;

View File

@@ -0,0 +1,39 @@
import { RCIMIWLogLevel } from "../RCIMDefines";
let logLevel = RCIMIWLogLevel.info;
export function log(level, msg) {
if (level <= logLevel) {
let logStr = `${getLogLevelFlag(level)}/[RC:UniLog][IM]: ${msg}`;
switch (level) {
case RCIMIWLogLevel.error:
console.error(logStr);
break;
case RCIMIWLogLevel.warn:
console.warn(logStr);
break;
case RCIMIWLogLevel.info:
console.info(logStr);
break;
case RCIMIWLogLevel.debug:
console.debug(logStr);
break;
default:
console.log(logStr);
break;
}
}
}
export function setLogLevel(level) {
logLevel = level;
}
function getLogLevelFlag(level) {
switch (level) {
case RCIMIWLogLevel.error:
return 'E';
case RCIMIWLogLevel.warn:
return 'W';
case RCIMIWLogLevel.debug:
return 'D';
default:
return 'I';
}
}

View File

@@ -0,0 +1,63 @@
/**
* 预定义的验证规则,只包含`值类型`数据验证
* 引用类型数据需使用自定义 validator 校验函数进行校验
*/
export declare enum AssertRules {
/**
* 类型为字符串,且长度大于 0
*/
STRING = 0,
/**
* 类型仅为 String
*/
ONLY_STRING = 1,
/**
* 类型为数字
*/
NUMBER = 2,
/**
* 类型为布尔值
*/
BOOLEAN = 3,
/**
* 类型为对象
*/
OBJECT = 4,
/**
* 类型为数组
*/
ARRAY = 5,
/**
* 类型为 callback 回调对象,包含 callback.onSuccess、callback.onError
*/
/**
类型为数组,数组中为 number 类型
*/
NUMBER_ARRAY = 6
}
export declare class RCAssertError extends Error {
constructor(message?: string);
}
/**
* 参数校验,该方法用于对业务层入参数据检查,及时抛出异常通知业务层进行修改
* @deprecated 优先使用 `validate` 替代,禁止直接 throw error 阻断调用栈
* @description
* 1. 必填参数value 需符合 validator 验证规,否则抛出异常
* 2. 非必填参数value 可为 undefined | null 或符合 validator 规则
* @param key 字段名,仅用于验证失败时给出提示信息
* @param value 待验证的值
* @param validator 期望类型或校验规则函数,若使用规则函数
* @param required 是否为必填参数,默认为 `false`
*/
export declare const assert: (key: string, value: any, validator: AssertRules | ((value: any) => boolean), required?: boolean) => void;
/**
* 参数校验,该方法用于对业务层入参数据检查,与 `assert` 函数不同的是其返回 boolean 值而非直接抛出异常
* @description
* 1. 必填参数value 需符合 validator 验证规,否则抛出异常
* 2. 非必填参数value 可为 undefined | null 或符合 validator 规则
* @param key 字段名,仅用于验证失败时给出提示信息
* @param value 待验证的值
* @param validator 期望类型或校验规则函数,若使用规则函数
* @param required 是否为必填参数,默认为 `false`
*/
export declare const validate: (key: string, value: any, validator: AssertRules | ((value?: any) => boolean), required?: boolean) => boolean;

View File

@@ -0,0 +1,104 @@
import { isNumber, isNumberArray, isObject, isString, isUndefined, notEmptyArray, notEmptyString, } from './validator';
/**
* 预定义的验证规则,只包含`值类型`数据验证
* 引用类型数据需使用自定义 validator 校验函数进行校验
*/
export var AssertRules;
(function (AssertRules) {
/**
* 类型为字符串,且长度大于 0
*/
AssertRules[AssertRules["STRING"] = 0] = "STRING";
/**
* 类型仅为 String
*/
AssertRules[AssertRules["ONLY_STRING"] = 1] = "ONLY_STRING";
/**
* 类型为数字
*/
AssertRules[AssertRules["NUMBER"] = 2] = "NUMBER";
/**
* 类型为布尔值
*/
AssertRules[AssertRules["BOOLEAN"] = 3] = "BOOLEAN";
/**
* 类型为对象
*/
AssertRules[AssertRules["OBJECT"] = 4] = "OBJECT";
/**
* 类型为数组
*/
AssertRules[AssertRules["ARRAY"] = 5] = "ARRAY";
/**
* 类型为 callback 回调对象,包含 callback.onSuccess、callback.onError
*/
// CALLBACK,
// /**
// * ChannelId 验证,必须为 String 且不超过 20 位 且不能包含下划线
// */
// CHANNEL_ID,
/**
类型为数组,数组中为 number 类型
*/
AssertRules[AssertRules["NUMBER_ARRAY"] = 6] = "NUMBER_ARRAY";
})(AssertRules || (AssertRules = {}));
const RulesDesc = {
[AssertRules.STRING]: 'type is String and length > 0',
[AssertRules.ONLY_STRING]: 'type is string',
[AssertRules.NUMBER]: 'type is number',
[AssertRules.BOOLEAN]: 'type is Boolean',
[AssertRules.OBJECT]: 'type is Object',
[AssertRules.ARRAY]: 'type is Array',
[AssertRules.NUMBER_ARRAY]: 'type is Array and item is Number',
};
const validators = {
[AssertRules.STRING]: notEmptyString,
[AssertRules.ONLY_STRING]: isString,
[AssertRules.NUMBER]: isNumber,
[AssertRules.BOOLEAN]: (value) => typeof value === 'boolean',
[AssertRules.OBJECT]: isObject,
[AssertRules.ARRAY]: notEmptyArray,
[AssertRules.NUMBER_ARRAY]: isNumberArray,
};
export class RCAssertError extends Error {
constructor(message) {
super(message);
this.name = 'RCAssertError';
}
}
/**
* 参数校验,该方法用于对业务层入参数据检查,及时抛出异常通知业务层进行修改
* @deprecated 优先使用 `validate` 替代,禁止直接 throw error 阻断调用栈
* @description
* 1. 必填参数value 需符合 validator 验证规,否则抛出异常
* 2. 非必填参数value 可为 undefined | null 或符合 validator 规则
* @param key 字段名,仅用于验证失败时给出提示信息
* @param value 待验证的值
* @param validator 期望类型或校验规则函数,若使用规则函数
* @param required 是否为必填参数,默认为 `false`
*/
export const assert = (key, value, validator, required = false) => {
if (!validate(key, value, validator, required)) {
throw new RCAssertError(`'${key}' is invalid: ${JSON.stringify(value)}`);
}
};
/**
* 参数校验,该方法用于对业务层入参数据检查,与 `assert` 函数不同的是其返回 boolean 值而非直接抛出异常
* @description
* 1. 必填参数value 需符合 validator 验证规,否则抛出异常
* 2. 非必填参数value 可为 undefined | null 或符合 validator 规则
* @param key 字段名,仅用于验证失败时给出提示信息
* @param value 待验证的值
* @param validator 期望类型或校验规则函数,若使用规则函数
* @param required 是否为必填参数,默认为 `false`
*/
export const validate = (key, value, validator, required = false) => {
let validatorFunc = validators[validator] || validator;
const isValid = required ? validatorFunc(value) : (isUndefined(value) || value == null || validatorFunc(value));
if (!isValid) {
// 打印无效参数到控制台便于定位问题
// eslint-disable-next-line no-console
console?.error(`'${key}' is invalid: ${JSON.stringify(value)}, the supported ${RulesDesc[validator]}`);
}
return isValid;
};

View File

@@ -0,0 +1,64 @@
/**
* 检查参数是否为字符串
* 只做类型检查,不做长度检查,故当字符串长度为 0结果依然为 true
* @param value
*/
export declare const isString: (value: unknown) => boolean;
/**
* 检测参数是否为布尔值
* @param value
*/
export declare const isBoolean: (value: unknown) => boolean;
/**
* 检查参数是否为 number 数据
* @param value
*/
export declare const isNumber: (value: unknown) => boolean;
/**
* 检查参数是否为数组
* 只做类型检查,不做长度检查
* 如 UnitArray、BufferArray 等也属于数组
* @param arr
*/
export declare const isArray: (arr: unknown) => boolean;
/**
* 检查参数是否为 ArrayBuffer
* @param arr
*/
export declare const isArrayBuffer: (arr: unknown) => boolean;
/**
* 检查参数是否为长度非 0 的字符串
* @param str
*/
export declare const notEmptyString: (str: string) => boolean;
/**
* 检查参数是否为长度非 0 的数组
* @param str
*/
export declare const notEmptyArray: (arr: any[]) => boolean;
/**
* 检查参数是否为对象
* @param val
*/
export declare const isObject: (val: any) => boolean;
/**
* 检查参数是否为函数
* @param val
*/
export declare const isFunction: (val: any) => boolean;
/**
* 检查参数是否为undefined
* @param val
*/
export declare const isUndefined: (val: any) => boolean;
/**
* 检查参数是否为 null
*/
export declare const isNull: (val: any) => boolean;
/**
* 检查对象不为空
* @param val
*/
export declare const notEmptyObject: (val: Object) => boolean;
export declare const isValidChannelId: (value: any) => boolean;
export declare const isNumberArray: (value: any) => boolean;

View File

@@ -0,0 +1,85 @@
/**
* 检查参数是否为字符串
* 只做类型检查,不做长度检查,故当字符串长度为 0结果依然为 true
* @param value
*/
export const isString = (value) => typeof value === 'string';
/**
* 检测参数是否为布尔值
* @param value
*/
export const isBoolean = (value) => typeof value === 'boolean';
/**
* 检查参数是否为 number 数据
* @param value
*/
export const isNumber = (value) => typeof value === 'number' && !Number.isNaN(value);
/**
* 检查参数是否为数组
* 只做类型检查,不做长度检查
* 如 UnitArray、BufferArray 等也属于数组
* @param arr
*/
export const isArray = (arr) => Object.prototype.toString.call(arr).indexOf('Array') !== -1;
/**
* 检查参数是否为 ArrayBuffer
* @param arr
*/
export const isArrayBuffer = (arr) => Object.prototype.toString.call(arr) === '[object ArrayBuffer]';
/**
* 检查参数是否为长度非 0 的字符串
* @param str
*/
export const notEmptyString = (str) => isString(str) && str.length > 0;
/**
* 检查参数是否为长度非 0 的数组
* @param str
*/
export const notEmptyArray = (arr) => isArray(arr) && arr.length > 0;
/**
* 检查参数是否为对象
* @param val
*/
export const isObject = (val) => Object.prototype.toString.call(val) === '[object Object]';
/**
* 检查参数是否为函数
* @param val
*/
export const isFunction = (val) => Object.prototype.toString.call(val) === '[object Function]';
/**
* 检查参数是否为undefined
* @param val
*/
// IE 下 undefined 为 Object
export const isUndefined = (val) => val === undefined
|| Object.prototype.toString.call(val) === '[object Undefined]';
/**
* 检查参数是否为 null
*/
export const isNull = (val) => Object.prototype.toString.call(val) === '[object Null]';
/**
* 检查对象不为空
* @param val
*/
export const notEmptyObject = (val) => {
for (let key in val) {
if (Object.prototype.hasOwnProperty.call(val, key)) {
return true;
}
}
return false;
};
export const isValidChannelId = (value) => {
let flag = false;
if (isString(value) && !(/_/g.test(value)) && value.length <= 20) {
flag = true;
}
return flag;
};
export const isNumberArray = (value) => {
let flag = false;
if (notEmptyArray(value)) {
flag = value.every((item) => isNumber(item));
}
return flag;
};