From 92f7e3f0a9e6502fec49b8b4346f26e217d113a2 Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 18 Mar 2022 19:04:12 +0800 Subject: [PATCH] update --- js/lib/core.js | 72 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/js/lib/core.js b/js/lib/core.js index 4f96d94..aacc17d 100644 --- a/js/lib/core.js +++ b/js/lib/core.js @@ -1,38 +1,52 @@ -if (!String.prototype.size) { - Object.defineProperty(String.prototype, 'size', { - get() { - var n = 0 - for (let it of this) { - n++ - } - return n - } - }) +/** + * 增强版 String + */ +export class SString { + #origin = '' + #list = [] - Object.defineProperty(String.prototype, 'at16', { - value(n) { - var tmp = [] - for (let it of this) { - tmp.push(it) - } - return tmp[n] + constructor(str = '') { + this.#origin = str + + for (let it of str) { + this.#list.push(it) } - }) - Object.defineProperty(String.prototype, 'split16', { - value(pipe) { - if (pipe === '') { - var tmp = [] - for (let it of this) { - tmp.push(it) - } - return tmp - } else { - return this.split(pipe) + } + + get length() { + return this.#list.length + } + + at(index = 0) { + if (this.length > 0) { + while (index < 0) { + index += this.length + } + return this.#list[index] + } + } + + split() { + return this.#list + } + + forEach(callback) { + for (let i in this.#list) { + if (callback(this.#list[i], i) === false) { + break } } - }) + return this + } + + toString() { + return this.#origin + } } +/** + * 特殊版 Enum + */ export class Enum { #dict_k = Object.create(null) #dict_v = Object.create(null)