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)