Skip to content

Commit 2bb2526

Browse files
committed
feat: add duplicate method to both iterables
1 parent a8b4588 commit 2bb2526

18 files changed

+902
-419
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Added:
1212
- Added `mapKey` method to both iterables
1313
- Added `mapValue` method to both iterables
14+
- Added `duplicate` method to both iterables
1415

1516
### Changed:
1617
- Improved types of `toObject` and `toMap` methods

docs/polyethylene.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ The entry point for this library is the [Poly](./polyethylene.poly.md) namespace
4646
| [IndexedReducer](./polyethylene.indexedreducer.md) | A function that receives an accumulated result, an element of an iteration, and returns a new accumulated result for the next call or as a final return value. |
4747
| [IndexedRunnable](./polyethylene.indexedrunnable.md) | A function that receives an object (<code>elem</code>) and its <code>index</code> in the iteration and doesn't return anything |
4848
| [IndexedTypePredicate](./polyethylene.indexedtypepredicate.md) | A function that receives an object (<code>elem</code>) and its <code>index</code> in the iteration and returns a <code>boolean</code> value indicating if <code>elem</code> is of the generic type <code>U</code> |
49+
| [Tuple](./polyethylene.tuple.md) | A tuple of <code>N</code> elements of type <code>T</code>. |
4950

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [polyethylene](./polyethylene.md) &gt; [PolyAsyncIterable](./polyethylene.polyasynciterable.md) &gt; [duplicate](./polyethylene.polyasynciterable.duplicate.md)
4+
5+
## PolyAsyncIterable.duplicate() method
6+
7+
Returns a tuple containing `num` iterables that will yield independent copies of the elements yielded by `this`<!-- -->.
8+
9+
**Signature:**
10+
11+
```typescript
12+
duplicate<N extends number>(num: N): Tuple<PolyAsyncIterable<T>, N>;
13+
```
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| --- | --- | --- |
19+
| num | N | the number of copies to be returned |
20+
21+
**Returns:**
22+
23+
[Tuple](./polyethylene.tuple.md)<!-- -->&lt;[PolyAsyncIterable](./polyethylene.polyasynciterable.md)<!-- -->&lt;T&gt;, N&gt;
24+
25+
An array of `num` elements containing independent copies of this iterable
26+
27+
## Remarks
28+
29+
Note that, as with every other method of this class, this instance is unusable after calling this method.
30+
31+
In order to provide a truly independent iteration for all returned iterables, a buffer is kept, which can grow as big as the whole iteration in certain circumstances. The buffer is filled as fast as the fastest iterable requests new items, and emptied as fast as the slowest iterable consumes those items.
32+

docs/polyethylene.polyasynciterable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The constructor for this class is marked as internal. Third-party code should no
4242
| [drop(num)](./polyethylene.polyasynciterable.drop.md) | | Return a new iteration that skips the first <code>num</code> elements. If there were less than <code>num</code> elements in the iteration, no elements are yielded. |
4343
| [dropLast(num)](./polyethylene.polyasynciterable.droplast.md) | | Return a new iteration that skips the last <code>num</code> elements. If there were less than <code>num</code> elements in the iteration, no elements are yielded. |
4444
| [dropWhile(func)](./polyethylene.polyasynciterable.dropwhile.md) | | Return a new iteration that skips the first few elements for which <code>func(element)</code> returns <code>true</code>. |
45+
| [duplicate(num)](./polyethylene.polyasynciterable.duplicate.md) | | Returns a tuple containing <code>num</code> iterables that will yield independent copies of the elements yielded by <code>this</code>. |
4546
| [every(func, options)](./polyethylene.polyasynciterable.every.md) | | Returns <code>true</code> if calling <code>func(element)</code> returns <code>true</code> for every element, and <code>false</code> otherwise |
4647
| [filter(func, options)](./polyethylene.polyasynciterable.filter.md) | | Return an iteration of the elements of <code>this</code> for which <code>func(element)</code> returns <code>true</code>. |
4748
| [filter(func, options)](./polyethylene.polyasynciterable.filter_1.md) | | Return an iteration of the elements of <code>this</code> for which <code>func(element)</code> returns <code>true</code>. |

docs/polyethylene.polysynciterable._symbol.iterator_.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ Allows this class to work as a regular `Iterable<T>`
99
**Signature:**
1010

1111
```typescript
12-
[Symbol.iterator](): Generator<T, void, undefined>;
12+
[Symbol.iterator](): Iterator<T, void, undefined>;
1313
```
1414
**Returns:**
1515

16-
Generator&lt;T, void, undefined&gt;
16+
Iterator&lt;T, void, undefined&gt;
1717

1818
an iterable that will yield the same elements as the iterable used to create this instance
1919

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [polyethylene](./polyethylene.md) &gt; [PolySyncIterable](./polyethylene.polysynciterable.md) &gt; [duplicate](./polyethylene.polysynciterable.duplicate.md)
4+
5+
## PolySyncIterable.duplicate() method
6+
7+
Returns a tuple containing `num` iterables that will yield independent copies of the elements yielded by `this`<!-- -->.
8+
9+
**Signature:**
10+
11+
```typescript
12+
duplicate<N extends number>(num: N): Tuple<PolySyncIterable<T>, N>;
13+
```
14+
15+
## Parameters
16+
17+
| Parameter | Type | Description |
18+
| --- | --- | --- |
19+
| num | N | the number of copies to be returned |
20+
21+
**Returns:**
22+
23+
[Tuple](./polyethylene.tuple.md)<!-- -->&lt;[PolySyncIterable](./polyethylene.polysynciterable.md)<!-- -->&lt;T&gt;, N&gt;
24+
25+
An array of `num` elements containing independent copies of this iterable
26+
27+
## Remarks
28+
29+
Note that, as with every other method of this class, this instance is unusable after calling this method.
30+
31+
In order to provide a truly independent iteration for all returned iterables, a buffer is kept, which can grow as big as the whole iteration in certain circumstances. The buffer is filled as fast as the fastest iterable requests new items, and emptied as fast as the slowest iterable requests new items.
32+
33+
Note that for synchronous iterations, it's common to end up with a full buffer if the returned duplicated elements are used in sequence. In this situation, it might be more useful to simply convert the iteration to an array and pass it around, rather than pay the overhead of this method.
34+

docs/polyethylene.polysynciterable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The constructor for this class is marked as internal. Third-party code should no
3434
| [drop(num)](./polyethylene.polysynciterable.drop.md) | | Return a new iteration that skips the first <code>num</code> elements. If there were less than <code>num</code> elements in the iteration, no elements are yielded. |
3535
| [dropLast(num)](./polyethylene.polysynciterable.droplast.md) | | Return a new iteration that skips the last <code>num</code> elements. If there were less than <code>num</code> elements in the iteration, no elements are yielded. |
3636
| [dropWhile(func)](./polyethylene.polysynciterable.dropwhile.md) | | Return a new iteration that skips the first few elements for which <code>func(element)</code> returns <code>true</code>. |
37+
| [duplicate(num)](./polyethylene.polysynciterable.duplicate.md) | | Returns a tuple containing <code>num</code> iterables that will yield independent copies of the elements yielded by <code>this</code>. |
3738
| [every(func)](./polyethylene.polysynciterable.every.md) | | Returns <code>true</code> if calling <code>func(element)</code> returns <code>true</code> for every element, and <code>false</code> otherwise |
3839
| [filter(func)](./polyethylene.polysynciterable.filter.md) | | Return an iteration of the elements of <code>this</code> for which <code>func(element)</code> returns <code>true</code>. |
3940
| [filter(func)](./polyethylene.polysynciterable.filter_1.md) | | Return an iteration of the elements of <code>this</code> for which <code>func(element)</code> returns <code>true</code>. |

docs/polyethylene.tuple.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [polyethylene](./polyethylene.md) &gt; [Tuple](./polyethylene.tuple.md)
4+
5+
## Tuple type
6+
7+
A tuple of `N` elements of type `T`<!-- -->.
8+
9+
**Signature:**
10+
11+
```typescript
12+
export type Tuple<T, N extends number> = [T, ...T[]] & {
13+
length: N;
14+
};
15+
```

etc/polyethylene.api.json

Lines changed: 227 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"toolPackage": "@microsoft/api-extractor",
4-
"toolVersion": "7.34.4",
4+
"toolVersion": "7.38.2",
55
"schemaVersion": 1011,
66
"oldestForwardsCompatibleVersion": 1001,
77
"tsdocConfig": {
@@ -3242,6 +3242,89 @@
32423242
"isAbstract": false,
32433243
"name": "dropWhile"
32443244
},
3245+
{
3246+
"kind": "Method",
3247+
"canonicalReference": "polyethylene!PolyAsyncIterable#duplicate:member(1)",
3248+
"docComment": "/**\n * Returns a tuple containing `num` iterables that will yield independent copies of the elements yielded by `this`.\n *\n * @remarks\n *\n * Note that, as with every other method of this class, this instance is unusable after calling this method.\n *\n * In order to provide a truly independent iteration for all returned iterables, a buffer is kept, which can grow as big as the whole iteration in certain circumstances. The buffer is filled as fast as the fastest iterable requests new items, and emptied as fast as the slowest iterable consumes those items.\n *\n * @param num - the number of copies to be returned\n *\n * @returns An array of `num` elements containing independent copies of this iterable\n */\n",
3249+
"excerptTokens": [
3250+
{
3251+
"kind": "Content",
3252+
"text": "duplicate<N extends "
3253+
},
3254+
{
3255+
"kind": "Content",
3256+
"text": "number"
3257+
},
3258+
{
3259+
"kind": "Content",
3260+
"text": ">(num: "
3261+
},
3262+
{
3263+
"kind": "Content",
3264+
"text": "N"
3265+
},
3266+
{
3267+
"kind": "Content",
3268+
"text": "): "
3269+
},
3270+
{
3271+
"kind": "Reference",
3272+
"text": "Tuple",
3273+
"canonicalReference": "polyethylene!Tuple:type"
3274+
},
3275+
{
3276+
"kind": "Content",
3277+
"text": "<"
3278+
},
3279+
{
3280+
"kind": "Reference",
3281+
"text": "PolyAsyncIterable",
3282+
"canonicalReference": "polyethylene!PolyAsyncIterable:class"
3283+
},
3284+
{
3285+
"kind": "Content",
3286+
"text": "<T>, N>"
3287+
},
3288+
{
3289+
"kind": "Content",
3290+
"text": ";"
3291+
}
3292+
],
3293+
"typeParameters": [
3294+
{
3295+
"typeParameterName": "N",
3296+
"constraintTokenRange": {
3297+
"startIndex": 1,
3298+
"endIndex": 2
3299+
},
3300+
"defaultTypeTokenRange": {
3301+
"startIndex": 0,
3302+
"endIndex": 0
3303+
}
3304+
}
3305+
],
3306+
"isStatic": false,
3307+
"returnTypeTokenRange": {
3308+
"startIndex": 5,
3309+
"endIndex": 9
3310+
},
3311+
"releaseTag": "Public",
3312+
"isProtected": false,
3313+
"overloadIndex": 1,
3314+
"parameters": [
3315+
{
3316+
"parameterName": "num",
3317+
"parameterTypeTokenRange": {
3318+
"startIndex": 3,
3319+
"endIndex": 4
3320+
},
3321+
"isOptional": false
3322+
}
3323+
],
3324+
"isOptional": false,
3325+
"isAbstract": false,
3326+
"name": "duplicate"
3327+
},
32453328
{
32463329
"kind": "Method",
32473330
"canonicalReference": "polyethylene!PolyAsyncIterable#every:member(1)",
@@ -6384,8 +6467,8 @@
63846467
},
63856468
{
63866469
"kind": "Reference",
6387-
"text": "Generator",
6388-
"canonicalReference": "!Generator:interface"
6470+
"text": "Iterator",
6471+
"canonicalReference": "!Iterator:interface"
63896472
},
63906473
{
63916474
"kind": "Content",
@@ -6942,6 +7025,89 @@
69427025
"isAbstract": false,
69437026
"name": "dropWhile"
69447027
},
7028+
{
7029+
"kind": "Method",
7030+
"canonicalReference": "polyethylene!PolySyncIterable#duplicate:member(1)",
7031+
"docComment": "/**\n * Returns a tuple containing `num` iterables that will yield independent copies of the elements yielded by `this`.\n *\n * @remarks\n *\n * Note that, as with every other method of this class, this instance is unusable after calling this method.\n *\n * In order to provide a truly independent iteration for all returned iterables, a buffer is kept, which can grow as big as the whole iteration in certain circumstances. The buffer is filled as fast as the fastest iterable requests new items, and emptied as fast as the slowest iterable requests new items.\n *\n * Note that for synchronous iterations, it's common to end up with a full buffer if the returned duplicated elements are used in sequence. In this situation, it might be more useful to simply convert the iteration to an array and pass it around, rather than pay the overhead of this method.\n *\n * @param num - the number of copies to be returned\n *\n * @returns An array of `num` elements containing independent copies of this iterable\n */\n",
7032+
"excerptTokens": [
7033+
{
7034+
"kind": "Content",
7035+
"text": "duplicate<N extends "
7036+
},
7037+
{
7038+
"kind": "Content",
7039+
"text": "number"
7040+
},
7041+
{
7042+
"kind": "Content",
7043+
"text": ">(num: "
7044+
},
7045+
{
7046+
"kind": "Content",
7047+
"text": "N"
7048+
},
7049+
{
7050+
"kind": "Content",
7051+
"text": "): "
7052+
},
7053+
{
7054+
"kind": "Reference",
7055+
"text": "Tuple",
7056+
"canonicalReference": "polyethylene!Tuple:type"
7057+
},
7058+
{
7059+
"kind": "Content",
7060+
"text": "<"
7061+
},
7062+
{
7063+
"kind": "Reference",
7064+
"text": "PolySyncIterable",
7065+
"canonicalReference": "polyethylene!PolySyncIterable:class"
7066+
},
7067+
{
7068+
"kind": "Content",
7069+
"text": "<T>, N>"
7070+
},
7071+
{
7072+
"kind": "Content",
7073+
"text": ";"
7074+
}
7075+
],
7076+
"typeParameters": [
7077+
{
7078+
"typeParameterName": "N",
7079+
"constraintTokenRange": {
7080+
"startIndex": 1,
7081+
"endIndex": 2
7082+
},
7083+
"defaultTypeTokenRange": {
7084+
"startIndex": 0,
7085+
"endIndex": 0
7086+
}
7087+
}
7088+
],
7089+
"isStatic": false,
7090+
"returnTypeTokenRange": {
7091+
"startIndex": 5,
7092+
"endIndex": 9
7093+
},
7094+
"releaseTag": "Public",
7095+
"isProtected": false,
7096+
"overloadIndex": 1,
7097+
"parameters": [
7098+
{
7099+
"parameterName": "num",
7100+
"parameterTypeTokenRange": {
7101+
"startIndex": 3,
7102+
"endIndex": 4
7103+
},
7104+
"isOptional": false
7105+
}
7106+
],
7107+
"isOptional": false,
7108+
"isAbstract": false,
7109+
"name": "duplicate"
7110+
},
69457111
{
69467112
"kind": "Method",
69477113
"canonicalReference": "polyethylene!PolySyncIterable#every:member(1)",
@@ -9596,6 +9762,64 @@
95969762
"endIndex": 3
95979763
}
95989764
]
9765+
},
9766+
{
9767+
"kind": "TypeAlias",
9768+
"canonicalReference": "polyethylene!Tuple:type",
9769+
"docComment": "/**\n * A tuple of `N` elements of type `T`.\n *\n * @typeParam T - The type of elements of the tuple\n *\n * @typeParam N - The number of elements in this tuple\n */\n",
9770+
"excerptTokens": [
9771+
{
9772+
"kind": "Content",
9773+
"text": "export type Tuple<T, N extends "
9774+
},
9775+
{
9776+
"kind": "Content",
9777+
"text": "number"
9778+
},
9779+
{
9780+
"kind": "Content",
9781+
"text": "> = "
9782+
},
9783+
{
9784+
"kind": "Content",
9785+
"text": "[T, ...T[]] & {\n length: N;\n}"
9786+
},
9787+
{
9788+
"kind": "Content",
9789+
"text": ";"
9790+
}
9791+
],
9792+
"fileUrlPath": "src/lib/types.ts",
9793+
"releaseTag": "Public",
9794+
"name": "Tuple",
9795+
"typeParameters": [
9796+
{
9797+
"typeParameterName": "T",
9798+
"constraintTokenRange": {
9799+
"startIndex": 0,
9800+
"endIndex": 0
9801+
},
9802+
"defaultTypeTokenRange": {
9803+
"startIndex": 0,
9804+
"endIndex": 0
9805+
}
9806+
},
9807+
{
9808+
"typeParameterName": "N",
9809+
"constraintTokenRange": {
9810+
"startIndex": 1,
9811+
"endIndex": 2
9812+
},
9813+
"defaultTypeTokenRange": {
9814+
"startIndex": 0,
9815+
"endIndex": 0
9816+
}
9817+
}
9818+
],
9819+
"typeTokenRange": {
9820+
"startIndex": 3,
9821+
"endIndex": 4
9822+
}
95999823
}
96009824
]
96019825
}

0 commit comments

Comments
 (0)