添加新容器对象

This commit is contained in:
donggang
2024-02-27 09:38:48 +08:00
parent c09363f8c8
commit 6ae9227a4e
5 changed files with 314 additions and 0 deletions

View File

@@ -168,4 +168,47 @@ export class StringUtil {
}
return realLength;
}
/**
* 是否为空
* @param str
*/
public static IsEmpty(str: string): boolean {
if (str == null || str == undefined || str.length == 0) {
return true;
}
return false;
}
/**
* 参数替换
* @param str
* @param rest
*
* @example
*
* var str:string = "here is some info '{0}' and {1}";
* StringUtil.substitute(str, 15.4, true);
*
* "here is some info '15.4' and true"
*/
public static substitute(str: string, ...rest: any[]): string {
if (str == null) return '';
var len: number = rest.length;
var args: any[];
if (len == 1 && rest[0] instanceof Array) {
args = rest[0];
len = args.length;
}
else {
args = rest;
}
for (var i: number = 0; i < len; i++) {
str = str.replace(new RegExp("\\{" + i + "\\}", "g"), args[i]);
}
return str;
}
}

View File

@@ -0,0 +1,98 @@
/** 字典 */
export class Dictionary<TKey, TValue> {
private map: Map<TKey, TValue> | undefined = new Map<TKey, TValue>();
private list: Array<TValue> = [];
/**
* 设置
* @param key
* @param value
*/
set(key: TKey, value: TValue): void {
let old: TValue;
//删除老的
if (this.map.has(key)) {
old = this.map.get(key);
const index: number = this.list.indexOf(old);
if (index < 0) {
throw new Error("Dictionary内部逻辑错误");
}
this.map.delete(key);
this.list.splice(index, 1);
}
this.map.set(key, value);
this.list.push(value);
}
/**
* 指定关键字数据是否存在
* @param key
* @returns
*/
has(key: TKey): boolean {
return this.map.has(key);
}
/**
* 获取指定元素
* @param key
* @returns
*/
get(key: TKey): TValue | undefined {
return this.map.get(key);
}
/**
* 通过索引获取元素
* @param index
* @returns
*/
getValue(index: number): TValue | undefined {
if (index >= this.list.length) {
throw new Error(index + "索引超出0-" + this.list.length + "范围");
}
return this.list[index];
}
/**
* 删除指定元素
* @param key
* @returns
*/
delete(key: TKey): TValue | undefined {
if (!this.map.has(key)) {
return undefined;
}
const result = this.map.get(key);
const index: number = this.list.indexOf(result);
if (index < 0) {
throw new Error("Dictionary内部逻辑错误");
}
this.list.splice(index, 1);
this.map.delete(key);
return result;
}
/** 清除所有元素 */
clear() {
this.map.clear();
this.list.length = 0;
}
/** 元素列表 */
get elements(): Array<TValue> {
return this.list;
}
/** 数据数量 */
get size(): number {
return this.map.size;
}
destroy(): void {
this.map.clear();
this.map = null;
this.list = null;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "27106451-650c-47ac-9c28-1e8da978e0f2",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,155 @@
/** 列表 */
export class List<T> {
private element: Array<T>;
/** 是否保证元素的唯一性 */
private only: boolean = false;
/** 元素数量(内部再增删时会修改这个参数,外部只做计算和绑定使用,切记不可做赋值操作) */
count: number = 0;
constructor(only: boolean = true) {
this.only = only;
this.element = [];
}
/**
* 添加到末尾(注意如果保证唯一性,那么重复时就直接返回)
* @param value
*/
push(value: T): boolean {
if (this.only) {
let index: number = this.element.indexOf(value);
if (index >= 0) {
return false;
}
}
this.element.push(value);
this.count = this.element.length;
return true;
}
/**
* 添加到列表头部(注意如果保证唯一性,那么重复时就直接返回)
* @param value
* @returns
*/
unshift(value: T): boolean {
if (this.only) {
let index: number = this.element.indexOf(value);
if (index >= 0) {
return false;
}
}
this.element.unshift(value);
this.count = this.element.length;
return true;
}
/**
* 获取并删除最后一个元素
* @returns
*/
pop(): T {
if (this.element.length > 0) {
const result = this.element.pop();
this.count = this.element.length;
return result;
}
return null;
}
/**
* 获取并删除第一个元素
* @returns
*/
shift(): T {
if (this.element.length > 0) {
const result = this.element.shift();
this.count = this.element.length;
return result;
}
return null;
}
/**
* 删除指定索引的元素
* @param index
*/
removeAt(index: number): T {
if (index >= this.element.length) {
throw new Error("删除索引超出范围!");
}
const result = this.element[index];
this.element.splice(index, 1);
this.count = this.element.length;
return result;
}
/**
* 删除元素
* @param value
*/
remove(value: T): void {
let index: number = this.element.indexOf(value);
if (index < 0) {
throw new Error("要删除的内容不在列表中!" + value);
}
const result = this.element[index];
this.element.splice(index, 1);
this.count = this.element.length;
}
/** 移除所有元素 */
clear(): void {
this.count = 0;
this.element.length = 0;
}
/**
* 判断是否包含
* @param value
* @returns
*/
has(value: T): boolean {
return this.find(value) >= 0;
}
/**
* 查找元素下标
* @param value
* @returns
*/
find(value: T): number {
return this.element.indexOf(value);
}
/**
* 查找元素下标
* @param predicate
* @returns
*/
findIndex(predicate: (value: T, index: number, obj: T[]) => unknown): number {
let index = this.element.findIndex(predicate);
return index;
}
/**
* 获取指定元素
* @param index
* @returns
*/
get(index: number): T {
if (index >= this.element.length) {
throw new Error("超出索引范围:" + index + "/" + this.element.length);
}
return this.element[index];
}
/**
* 源列表数据(注意不要直接进行增删操作而是通过List.push....等接口进行操作)
*/
get elements(): Array<T> {
return this.element;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "f4d36215-9036-4415-8189-c8ecbac22670",
"files": [],
"subMetas": {},
"userData": {}
}