@anion155/proposal-iterator-helpers 0.1.0 → 1.0.0

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 ADDED
@@ -0,0 +1,39 @@
1
+ # @anion155/proposal-iterator-helpers
2
+
3
+ Implementation of the [Iterator Helpers proposal for ECMAScript](https://github.com/tc39/proposal-iterator-helpers).
4
+ This package provides the proposal semantics and polyfills intended for testing, polyfilling, and proposal exploration.
5
+
6
+ ## Install
7
+
8
+ Install with npm:
9
+
10
+ ```bash
11
+ npm install @anion155/proposal-iterator-helpers
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```ts
17
+ // To polyfill prototypes and constructor without modifying global scope
18
+ import "@anion155/proposal-iterator-helpers";
19
+ // or
20
+ // To also polyfill global scope use this import
21
+ import "@anion155/proposal-iterator-helpers/global";
22
+ // and
23
+ // To install types
24
+ import "@anion155/proposal-iterator-helpers/types";
25
+
26
+ function* naturals() {
27
+ let i = 0;
28
+ while (true) {
29
+ yield i;
30
+ i += 1;
31
+ }
32
+ }
33
+ const result = naturals().map((value) => {
34
+ return value * value;
35
+ });
36
+ result.next(); // { value: 0, done: false };
37
+ result.next(); // { value: 1, done: false };
38
+ result.next(); // { value: 4, done: false };
39
+ ```
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anion155/proposal-iterator-helpers",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "Polyfills implementing the Iterator Helpers proposal for ECMAScript.",
5
5
  "keywords": [
6
6
  "iterator",
@@ -10,6 +10,7 @@
10
10
  "proposal",
11
11
  "polyfill"
12
12
  ],
13
+ "license": "MIT",
13
14
  "author": {
14
15
  "name": "anion155",
15
16
  "url": "https://github.com/anion155"
@@ -19,27 +20,10 @@
19
20
  "url": "https://github.com/anion155/monorepo.git",
20
21
  "directory": "packages/proposal-iterator-helpers"
21
22
  },
22
- "license": "MIT",
23
23
  "type": "module",
24
24
  "exports": {
25
- "./global-iterator": "./src/global-iterator.ts",
26
- "./global": "./src/global.ts",
27
25
  ".": "./src/index.ts",
28
- "./iterator-constructor": "./src/iterator-constructor.ts",
29
- "./iterator-from": "./src/iterator-from.ts",
30
- "./iterator-prototype-drop": "./src/iterator-prototype-drop.ts",
31
- "./iterator-prototype-every": "./src/iterator-prototype-every.ts",
32
- "./iterator-prototype-filter": "./src/iterator-prototype-filter.ts",
33
- "./iterator-prototype-find": "./src/iterator-prototype-find.ts",
34
- "./iterator-prototype-flat-map": "./src/iterator-prototype-flat-map.ts",
35
- "./iterator-prototype-for-each": "./src/iterator-prototype-for-each.ts",
36
- "./iterator-prototype-map": "./src/iterator-prototype-map.ts",
37
- "./iterator-prototype-reduce": "./src/iterator-prototype-reduce.ts",
38
- "./iterator-prototype-some": "./src/iterator-prototype-some.ts",
39
- "./iterator-prototype-take": "./src/iterator-prototype-take.ts",
40
- "./iterator-prototype-to-array": "./src/iterator-prototype-to-array.ts",
41
- "./iterator-prototype": "./src/iterator-prototype.ts",
42
- "./utils": "./src/utils.ts"
43
- },
44
- "dependencies": {}
26
+ "./global": "./src/global.ts",
27
+ "./types": "./src/types.ts"
28
+ }
45
29
  }
@@ -39,7 +39,9 @@ ProxyIterator.prototype = Object.create(IteratorPrototype, {
39
39
  }) as ProxyIterator<unknown, unknown, unknown>;
40
40
 
41
41
  polyfillProperty(IteratorConstructor, "from", {
42
- value: function from<T, TReturn = unknown, TNext = unknown>(it: Iterator<T, TReturn, TNext> | Iterable<T>): IteratorObject<T, TReturn, TNext> {
42
+ value: function from<T, TReturn = unknown, TNext = undefined>(
43
+ it: Iterator<T, TReturn, TNext> | Iterable<T, TReturn, TNext>,
44
+ ): IteratorObject<T, TReturn, TNext> {
43
45
  if (isIteratorInstance<T, TReturn, TNext>(it)) return it;
44
46
  const iterator = isIterable(it) ? it[Symbol.iterator]() : it;
45
47
  if (isIteratorInstance<T, TReturn, TNext>(iterator)) return iterator;
package/src/types.ts ADDED
@@ -0,0 +1,28 @@
1
+ interface Iterator<T, TResult, TNext> extends globalThis.IteratorObject<T, TResult, TNext> {}
2
+ declare abstract class Iterator<T, TResult = undefined, TNext = unknown> {
3
+ abstract next(value?: TNext): IteratorResult<T, TResult>;
4
+ }
5
+ type IteratorObjectConstructor = typeof Iterator;
6
+ declare global {
7
+ interface IteratorObject<T, TReturn, TNext> {
8
+ readonly [Symbol.toStringTag]: string;
9
+ [Symbol.iterator](): IteratorObject<T, TReturn, TNext>;
10
+ drop(limit: number): Generator<T, TReturn, TNext>;
11
+ every(predicate: (value: T, index: number) => boolean): boolean;
12
+ filter(predicate: (value: T, index: number) => boolean): Generator<T, TReturn, TNext>;
13
+ find(predicate: (value: T, index: number) => boolean): T | undefined;
14
+ flatMap<U>(project: (value: T, index: number) => Iterator<U> | Iterable<U>): Generator<U, TReturn, undefined>;
15
+ forEach(callback: (value: T, index: number) => void): void;
16
+ map<U>(project: (value: T, index: number) => U): Generator<U, TReturn, TNext>;
17
+ reduce<U>(reducer: (aggregation: U, value: T, index: number) => U, ...args: [initialValue?: U]): U;
18
+ some(predicate: (value: T, index: number) => boolean): boolean;
19
+ take(limit: number): Generator<T, T | TReturn | undefined, TNext>;
20
+ toArray(): T[];
21
+ }
22
+ interface IteratorConstructor extends IteratorObjectConstructor {
23
+ from<T, TReturn = unknown, TNext = undefined>(it: Iterator<T, TReturn, TNext> | Iterable<T, TReturn, TNext>): IteratorObject<T, TReturn, TNext>;
24
+ }
25
+ var Iterator: IteratorConstructor;
26
+ }
27
+
28
+ export {};