diff --git a/assets/core/utils/StringUtil.ts b/assets/core/utils/StringUtil.ts index a7e0929..c1c353d 100644 --- a/assets/core/utils/StringUtil.ts +++ b/assets/core/utils/StringUtil.ts @@ -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; + } } diff --git a/assets/libs/collection/Dictionary.ts b/assets/libs/collection/Dictionary.ts new file mode 100644 index 0000000..23c0f1c --- /dev/null +++ b/assets/libs/collection/Dictionary.ts @@ -0,0 +1,98 @@ + +/** 字典 */ +export class Dictionary { + private map: Map | undefined = new Map(); + private list: Array = []; + + /** + * 设置 + * @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 { + return this.list; + } + + /** 数据数量 */ + get size(): number { + return this.map.size; + } + + destroy(): void { + this.map.clear(); + this.map = null; + this.list = null; + } +} \ No newline at end of file diff --git a/assets/libs/collection/Dictionary.ts.meta b/assets/libs/collection/Dictionary.ts.meta new file mode 100644 index 0000000..953c13b --- /dev/null +++ b/assets/libs/collection/Dictionary.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "27106451-650c-47ac-9c28-1e8da978e0f2", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/libs/collection/List.ts b/assets/libs/collection/List.ts new file mode 100644 index 0000000..5dc8a51 --- /dev/null +++ b/assets/libs/collection/List.ts @@ -0,0 +1,155 @@ +/** 列表 */ +export class List { + private element: Array; + + /** 是否保证元素的唯一性 */ + 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 { + return this.element; + } +} \ No newline at end of file diff --git a/assets/libs/collection/List.ts.meta b/assets/libs/collection/List.ts.meta new file mode 100644 index 0000000..94cc5a3 --- /dev/null +++ b/assets/libs/collection/List.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "f4d36215-9036-4415-8189-c8ecbac22670", + "files": [], + "subMetas": {}, + "userData": {} +}