UNPKG

@thatcompany/ts-tool

Version:

基于TypeScript编写的工具库

263 lines 7.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EmptyArgs = exports.Empty = void 0; const error_1 = require("../error"); class Empty { /** * 判断一个值是否不为空 * @param value 要判断的值 * @param strict 是否严格模式,默认 true * @returns {boolean} */ static notEmpty = (value, strict = true) => !Empty.isEmpty(value, strict); /** * 判断一个值是否为空 * @param value 要判断的值 * @param strict 是否严格模式,默认 true * @returns {boolean} */ static isEmpty(value, strict = true) { // 处理 null 和 undefined if (value == null) { return true; // null 和 undefined 被视为空 } // 处理字符串 if (typeof value === 'string') { if (strict) { // 严格模式下,空字符串被视为空 return value.trim().length === 0; } else { // 非严格模式下,空字符串被视为不为空 return value.length === 0; } } // 处理数组 if (Array.isArray(value)) { return value.length === 0; // 空数组 } // 处理对象 if (typeof value === 'object') { return Object.keys(value).length === 0; // 空对象 } // 处理 Map 和 Set if (value instanceof Map || value instanceof Set) { return value.size === 0; // 空 Map 和 Set } // 处理函数 if (typeof value === 'function') { return false; // 函数不算空 } // 处理 NaN 和 Infinity if (typeof value === 'number') { return isNaN(value) || value === Infinity || value === -Infinity; } // 处理日期 if (value instanceof Date) { return false; // 日期不算空 } // 处理正则表达式 if (value instanceof RegExp) { return false; // 正则表达式不算空 } // 其他类型(例如数字、布尔值等)都不算空 return false; } /** * 判断多个参数是否都为空 * @param args 多个参数 * @returns {empty: boolean, list: string[]} */ static isEmptyArgs(args) { let empty = false; const errorKeys = []; for (const key in args) { // @ts-ignore if (Empty.isEmpty(args[key])) { empty = true; errorKeys.push(key); } } return { empty, list: errorKeys }; } static isEmptyError(args, printStack, handle) { const result = this.isEmptyArgs(args); if (result.empty) { const message = `以下参数为空或无法解析: [ ${result.list.join(', ')} ]`; throw new error_1.EmptyError({ message, printStack, handle, }); } } /** * 包装一个值,使其成为 Optional 对象 * @param value 要包装的值 * @returns {Optional} */ static ofNullable(value) { return new Optional(value); } } exports.Empty = Empty; class EmptyArgs { empty; list; constructor(args) { this.empty = false; this.list = []; for (const key in args) { // @ts-ignore if (Empty.isEmpty(args[key])) { this.empty = true; this.list.push(key); } } } /** * 调用默认 EmptyError 处理错误 * 可重写或传入 EmptyError.handle 方法来自定义错误处理 * @param handle * @param printStack */ end(handle, printStack) { if (!this.empty) { return; } const message = `以下参数为空或无法解析: [ ${this.list.join(', ')} ]`; throw new error_1.EmptyError({ message, printStack, handle, }); } error(ErrorType) { if (!this.empty) { return; } const message = `以下参数为空或无法解析: [ ${this.list.join(', ')} ]`; throw new ErrorType({ message, }); } } exports.EmptyArgs = EmptyArgs; /** * Optional 类 * 用于包装可能为空的值,提供链式调用的方法 * @class Optional */ class Optional { value; constructor(value) { this.value = value; } // 返回一个空的 Optional 实例 static empty() { return new Optional(null); } // 判断是否有值存在 isPresent() { return this.value !== null && this.value !== undefined; } // 如果值存在,执行传入的函数。否则什么都不做 ifPresent(func) { if (this.isPresent()) { func(this.value); } } // 根据条件过滤值,如果不满足条件返回空的 Optional 对象 filter(predicate) { if (!this.isPresent() || predicate(this.value)) { return this; } else { return Optional.empty(); } } // 将值映射为另一类型的 Optional,如果值为空,返回空的 Optional 对象 map(func) { if (this.isPresent()) { return new Optional(func(this.value)); } else { return Optional.empty(); } } // 类似 map,但允许返回另一个 Optional,并自动解包 flatMap(func) { if (this.isPresent()) { return func(this.value); } else { return Optional.empty(); } } // 如果值为空,返回默认值。否则返回值本身 orElse(other) { if (null === other) { return this.orElseThis(); } return this.isPresent() ? this.value : other; } // 如果值为空,通过提供的函数返回一个默认值 orElseGet(supplier) { return this.isPresent() ? this.value : supplier(); } // 如果 Optional 对象包含值,则返回该值,否则抛出异常 orElseThrow(error) { if (this.isPresent()) { return this.value; } else { throw error || new Error('No value present'); } } // 获取 Optional 对象的值,如果值不存在则抛出异常 get() { if (this.isPresent()) { return this.value; } else { throw new Error('No value present'); } } // 获取 Optional 对象的值,如果值不存在则返回默认值 orElseThis() { if (this.isPresent()) { return this.value; } else { return Optional.defaultValue(); } } static defaultValue() { // 根据类型提供默认值 if (typeof null === 'string') { return ''; } else if (typeof null === 'number') { return 0; } else if (typeof null === 'boolean') { return false; } else if (Array.isArray(null)) { return []; } else if (null instanceof Map) { return new Map(); } else if (null instanceof Set) { return new Set(); } else if (typeof null === 'object') { return {}; } else { return null; } } } //# sourceMappingURL=Empty.js.map