typescript-util
Version:
JS/TS 的简单工具
104 lines • 2.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringJoiner = void 0;
const StringConstant_1 = require("../constant/StringConstant");
const BaseObject_1 = require("../model/BaseObject");
// noinspection JSUnusedGlobalSymbols
/**
* StringJoiner
* 字符串连接
* {@link #_separator} 分隔符, 默认为空, 允许后期更改, 影响更改后的 {@link #toString}
* @author LL
* @date 2022-01-24 下午 1:29
**/
class StringJoiner extends BaseObject_1.BaseObject {
strList;
constructor(separator = StringConstant_1.StringConstant.EMPTY) {
super();
this._separator = separator;
this.strList = [];
}
equals(o) {
if (super.equals(o)) {
return true;
}
if (!(o instanceof StringJoiner)) {
return false;
}
if (!(this._separator === o._separator)) {
return false;
}
return JSON.stringify(this.strList) === JSON.stringify(o.strList);
}
/**
* 新增一个元素, 可以链式调用
* @param {string} str
* @return {StringJoiner}
*/
add(str) {
this.strList.push(String(str));
return this;
}
/**
* 新增一个元素, 可以链式调用
* 如果 str 为 Null (Null | undefined) 忽略
* @param {string} str
* @return {StringJoiner}
*/
addNotNull(str) {
if (str !== null && str !== undefined) {
this.strList.push(String(str));
}
return this;
}
/**
* 新增一个元素, 可以链式调用
* 如果 str 为 空 (Null | undefined | '' ) 忽略
* @param {string} str
* @return {StringJoiner}
*/
addNotEmpty(str) {
if (str !== null && str !== undefined && String(str).trim().length > 0) {
this.strList.push(String(str));
}
return this;
}
/**
* 新增一堆元素
* @param {string[]} arr
* @return {StringJoiner}
*/
addAllNotNull(arr) {
if (arr && Array.isArray(arr) && arr.length > 0) {
this.strList = this.strList.concat(arr);
}
return this;
}
/**
* 清空以前通过 各种 add 设置的元素
* 不清空 {@link #separator}
* (如果 separator 不同, 没必要复用吧...)
* @return {StringJoiner}
*/
clear() {
this.strList = [];
return this;
}
toString() {
return this.strList.join(this.separator);
}
/**
* 分隔符
* @type {string}
* @private
*/
_separator;
get separator() {
return this._separator;
}
set separator(value) {
this._separator = value;
}
}
exports.StringJoiner = StringJoiner;
//# sourceMappingURL=StringJoiner.js.map