com.sweaxizone.w3c.extension 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,7 +60,7 @@ ExampleEnum.valueOf(x) // val: number
60
60
  ExampleEnum.plus("variantA", 1) // 1
61
61
  ```
62
62
 
63
- ## Other globals
63
+ ## Other additions
64
64
 
65
65
  - `assert`
66
66
  - `AssertionError`
@@ -73,6 +73,9 @@ ExampleEnum.plus("variantA", 1) // 1
73
73
  - `isXMLName(argument)` (like E4X's `isXMLName()`)
74
74
  - `Namespace` (like E4X's `Namespace`)
75
75
  - `QName` (like E4X's `QName`)
76
+ - `Iterator.prototype.length()`
77
+ - `Chars`
78
+ - `String.prototype.chars()`
76
79
 
77
80
  ## License
78
81
 
@@ -0,0 +1 @@
1
+ export {};
package/dist/Chars.js ADDED
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // Chars
4
+ globalThis.Chars = class Chars extends Iterator {
5
+ _str;
6
+ _length;
7
+ _offset;
8
+ constructor(str, offset = -1) {
9
+ super();
10
+ this._str = str;
11
+ this._length = str.length;
12
+ this._offset = offset == -1 ? 0 : offset;
13
+ }
14
+ get hasRemaining() {
15
+ return this._offset < this._length;
16
+ }
17
+ get reachedEnd() {
18
+ return this._offset >= this._length;
19
+ }
20
+ next() {
21
+ const ch = this._str.codePointAt(this._offset);
22
+ if (ch === undefined) {
23
+ return { done: true, value: 0 };
24
+ }
25
+ if (ch >= 0x10000) {
26
+ this._offset++;
27
+ }
28
+ this._offset++;
29
+ return { done: false, value: ch };
30
+ }
31
+ skipOneInPlace() {
32
+ this.next();
33
+ }
34
+ skipInPlace(count) {
35
+ for (let i = 0; i < count; i++) {
36
+ if (this.next().done) {
37
+ break;
38
+ }
39
+ }
40
+ }
41
+ get index() {
42
+ return this._offset;
43
+ }
44
+ nextOrZero() {
45
+ const r = this.next();
46
+ return r.done ? 0 : r.value;
47
+ }
48
+ peek() {
49
+ return this._str.codePointAt(this._offset) ?? null;
50
+ }
51
+ peekOrZero() {
52
+ return this._str.codePointAt(this._offset) ?? 0;
53
+ }
54
+ at(index) {
55
+ let offset = this._offset;
56
+ for (let i = 0; i < index; i++) {
57
+ const ch = this._str.codePointAt(offset);
58
+ if (ch === undefined) {
59
+ break;
60
+ }
61
+ if (ch >= 0x10000) {
62
+ offset++;
63
+ }
64
+ offset++;
65
+ }
66
+ return this._str.codePointAt(offset) ?? null;
67
+ }
68
+ atOrZero(index) {
69
+ return this.at(index) ?? 0;
70
+ }
71
+ seq(numChars) {
72
+ const r = [];
73
+ let offset = this._offset;
74
+ for (let i = 0; i < numChars; i++) {
75
+ const ch = this._str.codePointAt(offset);
76
+ if (ch === undefined) {
77
+ break;
78
+ }
79
+ if (ch >= 0x10000) {
80
+ offset++;
81
+ }
82
+ offset++;
83
+ r.push(String.fromCodePoint(ch));
84
+ }
85
+ return r.join("");
86
+ }
87
+ };
88
+ // String.prototype.chars()
89
+ String.prototype.chars = function () {
90
+ return new Chars(this);
91
+ };
package/dist/Enum.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  globalThis.Enum = function Enum(variants, methods = null) {
4
4
  const fn = (key) => key;
5
5
  fn.valueOf = (key) => variants[key];
6
- fn.variants = () => Array.from(Object.keys(variants));
6
+ fn.variants = () => Object.keys(variants);
7
7
  fn.from = (arg) => {
8
8
  if (typeof arg == "string") {
9
9
  const v = typeof variants[arg];
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import "./Enum";
2
2
  import "./E4X";
3
3
  import "./SAEventTarget";
4
4
  import "./SAByteArray";
5
+ import "./Chars";
5
6
  import impAssert from "assert";
6
7
  declare global {
7
8
  const assert: typeof import("assert");
@@ -202,3 +203,80 @@ declare global {
202
203
  valueOf(): QName;
203
204
  }
204
205
  }
206
+ declare global {
207
+ interface IteratorObject<T, TReturn, TNext> {
208
+ /**
209
+ * Consumes the iterator and returns the number of
210
+ * iterated entries.
211
+ */
212
+ length(): number;
213
+ }
214
+ }
215
+ declare global {
216
+ /**
217
+ * `Chars` may be used for iterating characters (as Unicode code points)
218
+ * from left-to-right in a string with miscellaneous operation methods.
219
+ */
220
+ class Chars extends Iterator<number> {
221
+ /**
222
+ * Constructs a `Chars` iterator over the specified string,
223
+ * at the specified UTF-16 `offset`.
224
+ */
225
+ constructor(str: string, offset?: number);
226
+ /**
227
+ * Indicates if there are remaining code points to read.
228
+ */
229
+ get hasRemaining(): boolean;
230
+ /**
231
+ * Indicates if the reader has reached the end of the string.
232
+ */
233
+ get reachedEnd(): boolean;
234
+ next(): IteratorResult<number>;
235
+ /**
236
+ * Returns the next code point. If there are no code points
237
+ * available, returns U+00.
238
+ */
239
+ nextOrZero(): number;
240
+ /**
241
+ * Skips a code point. This is equivalent to
242
+ * calling `next()`.
243
+ */
244
+ skipOneInPlace(): void;
245
+ /**
246
+ * Skips the given number of code points.
247
+ */
248
+ skipInPlace(count: number): void;
249
+ /**
250
+ * Returns the current UTF-16 offset in the string.
251
+ */
252
+ get index(): number;
253
+ /**
254
+ * Peeks the next code point.
255
+ */
256
+ peek(): null | number;
257
+ /**
258
+ * Peeks the next code point. If there are no code points
259
+ * available, returns U+00.
260
+ */
261
+ peekOrZero(): number;
262
+ /**
263
+ * Peeks the next code point at the given
264
+ * zero based code point index.
265
+ */
266
+ at(index: number): null | number;
267
+ /**
268
+ * Peeks the next code point at the given zero based code point index.
269
+ * If there are no code points available, returns U+00.
270
+ */
271
+ atOrZero(index: number): number;
272
+ /**
273
+ * Peeks a number of code points until the string's end.
274
+ */
275
+ seq(numChars: number): string;
276
+ }
277
+ }
278
+ declare global {
279
+ interface String {
280
+ chars(): Chars;
281
+ }
282
+ }
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ require("./Enum");
7
7
  require("./E4X");
8
8
  require("./SAEventTarget");
9
9
  require("./SAByteArray");
10
+ require("./Chars");
10
11
  const assert_1 = __importDefault(require("assert"));
11
12
  // assert
12
13
  globalThis.assert = assert_1.default;
@@ -37,3 +38,11 @@ BigInt.max = (...rest) => {
37
38
  }
38
39
  return r;
39
40
  };
41
+ // Iterator.length()
42
+ Iterator.prototype.length = function () {
43
+ let n = 0;
44
+ for (const _ of this) {
45
+ n++;
46
+ }
47
+ return n;
48
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.sweaxizone.w3c.extension",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Global object additions for W3C-compliant runtimes.",
5
5
  "scripts": {
6
6
  "build": "tsc",