UNPKG

typescript-util

Version:

JS/TS 的简单工具

122 lines 3.6 kB
import { StringConstant } from '../constant/StringConstant'; import { StringJoiner } from '../util/StringJoiner'; import { ObjectTool } from './ObjectTool'; /** * StrTool * @author 冰凝 * @date 2022-09-19 09:43:23 **/ export class StrTool extends StringConstant { static HTTP_ADDRESS = new RegExp('^https?://.*'); /** * 字符串是空? {@code null | undefined | 全是空格的字符串} 被认为是空 * @param {string} str 待判断字符串 * @return {boolean} */ static isEmpty(str) { if (ObjectTool.isNull(str)) { return true; } // @ts-ignore return this.EMPTY === str || String(str).trim().length === 0; } /** * 字符串非空? 楼上 {@link #isEmpty} 的取反 * @param {string} str 待判断字符串 * @return {boolean} */ static isNotEmpty(str) { return !this.isEmpty(str); } /** * 判断字符串非空 * null, undefined => true * "null", "undefined" => true (不区分大小写) * " " => true * "" => true * " xxx " => false * * 对于非 string 类型, 抛出错误 * @param {string} str 待检查字符串 * @return {boolean} 是否为空? */ static isGenerEmpty(str) { if (this.isEmpty(str)) { return true; } // @ts-ignore const s = str.trim().toLocaleLowerCase(); return s === this.NULL || s === this.UNDEFINED; } /** * 重复 * @param {string} source * @param {number} size * @return {string} */ static repeat(source, size) { if (this.isEmpty(source) || size <= 0) { return this.EMPTY; } const arr = new Array(size); for (let i = 0; i < size; i++) { arr[i] = source; } return arr.join(this.EMPTY); } /** * 字符串连接 * TODO: 有点问题 * @param {string} separator 分隔符 * @param {string} str 需要连接的字符串的可变参数列表 * @return {string} 最终连接字符串 */ static join(separator, ...str) { return new StringJoiner(separator) .addAllNotNull(str) .toString(); } /** * 对象形式的字符串连接 * <p> * 例: * <pre> * const query = StrUtil.joinObject('&', {name: 'test', query: 'other', order: true}) * // 输出: name=test$query=other&order=true * console.log(query) * </pre> * * TODO: 有点问题 * * @param {string} separator 每个字符串单元的分隔符 * @param {Record<PropertyKey, any>} o 对象的 一组 key -> value 为一个字符串单元 * @param {string} conn 对象 key value 连接字符串, 默认 {@link #EQUAL} (等号) * @return {string} 最终连接字符 */ static joinObject(separator, o, conn = this.EQUAL) { return new StringJoiner(separator) .addAllNotNull(ObjectTool.toArray(o).map(kv => kv.toStr(conn))) .toString(); } /** * 是HTTP 绝对地址? */ static isAbsoluteAddress(s) { return this.HTTP_ADDRESS.test(s); } /** * 不是 HTTP 绝对地址 */ static notAbsoluteAddress(s) { return !this.HTTP_ADDRESS.test(s); } /** * 对象转 HTTP 查询字符串 */ static toQuery(obj) { return Object.keys(obj) .map(key => `${key}=${obj[key]}`) .join('&'); } } //# sourceMappingURL=StrTool.js.map