Skip to content

Commit b06dd47

Browse files
committed
Update Iterable types
1 parent fcd82ba commit b06dd47

File tree

6 files changed

+126
-23
lines changed

6 files changed

+126
-23
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ import 'linqes'
4747
.toArray()
4848
```
4949
50+
## Iterable types
51+
```ts
52+
String
53+
Array
54+
Int8Array
55+
Uint8Array
56+
Uint8ClampedArray
57+
Int16Array
58+
Uint16Array
59+
Int32Array
60+
Uint32Array
61+
Float32Array
62+
Float64Array
63+
BigInt64Array
64+
BigUint64Array
65+
Map
66+
```
67+
5068
## IEnumerable
5169
5270
```ts

index.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,15 @@ declare type IEnumerable<T> = Generator<T> & {
483483
where(predicate : (item : T, index : number) => boolean) : IEnumerable<T>;
484484
}
485485

486-
declare interface Array<T> extends IEnumerable<T> {
486+
/**
487+
* All iterable types can be IEnumerable
488+
*/
489+
declare interface RelativeIndexable<T> extends IEnumerable<T>{}
490+
491+
/**
492+
* Extended array methods
493+
*/
494+
declare interface Array<T>{
487495

488496
/**
489497
* Adds the given object to the end of this list. The size of the list is
@@ -551,6 +559,9 @@ declare interface Array<T> extends IEnumerable<T> {
551559
removeAt(index : number) : T | undefined
552560
}
553561

562+
/**
563+
* Extended map methods
564+
*/
554565
declare interface Map<K, V> extends IEnumerable<[K, V]> {
555566

556567
/**

index.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
22+
// noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols
2223
(function () {
2324
class Enumerable {
2425
[Symbol.iterator]() {
@@ -46,7 +47,7 @@
4647
return true;
4748
}
4849
any(predicate) {
49-
return this.firstOrDefault(predicate) !== null;
50+
return this.firstOrDefault(predicate) != null;
5051
}
5152
*append(element) {
5253
for (const item of this)
@@ -453,7 +454,13 @@
453454
}
454455
}
455456
}
456-
class PartialArray extends Enumerable {
457+
class PartialArrayLike extends Enumerable {
458+
*asEnumerable() {
459+
for (const item of this)
460+
yield item;
461+
}
462+
}
463+
class PartialArray extends PartialArrayLike {
457464
add(item) {
458465
this.push(item);
459466
}
@@ -558,6 +565,14 @@
558565
throw new Error("Method not implemented.");
559566
}
560567
}
568+
// noinspection JSUnresolvedReference
569+
const Generator = (function* () {
570+
// @ts-ignore
571+
})().__proto__.__proto__;
572+
const excepts = Object.getOwnPropertyNames(Generator);
573+
function propertyNames(prototype) {
574+
return Object.getOwnPropertyNames(prototype).filter(x => excepts.find(y => x == y) == null);
575+
}
561576
function defineProperty(prototype, name, method) {
562577
if (prototype[name] != undefined)
563578
return;
@@ -566,17 +581,38 @@
566581
writable: false
567582
});
568583
}
569-
Object.getOwnPropertyNames(PartialMap.prototype).forEach(name => {
584+
const iterable = [
585+
Map.prototype,
586+
String.prototype,
587+
Array.prototype,
588+
Int8Array.prototype,
589+
Uint8Array.prototype,
590+
Uint8ClampedArray.prototype,
591+
Int16Array.prototype,
592+
Uint16Array.prototype,
593+
Int32Array.prototype,
594+
Uint32Array.prototype,
595+
Float32Array.prototype,
596+
Float64Array.prototype,
597+
BigInt64Array.prototype,
598+
BigUint64Array.prototype,
599+
];
600+
propertyNames(PartialMap.prototype).forEach(name => {
570601
defineProperty(Map.prototype, name, PartialMap.prototype[name]);
571602
});
572-
Object.getOwnPropertyNames(PartialArray.prototype).forEach(name => {
603+
propertyNames(PartialArray.prototype).forEach(name => {
573604
defineProperty(Array.prototype, name, PartialArray.prototype[name]);
574605
});
575-
Object.getOwnPropertyNames(Enumerable.prototype).forEach(name => {
576-
defineProperty((function* () {
577-
// @ts-ignore
578-
})().__proto__.__proto__, name, Enumerable.prototype[name]);
579-
defineProperty(Array.prototype, name, Enumerable.prototype[name]);
580-
defineProperty(Map.prototype, name, Enumerable.prototype[name]);
606+
propertyNames(PartialArrayLike.prototype).forEach(name => {
607+
iterable.forEach(proto => {
608+
defineProperty(proto, name, PartialArrayLike.prototype[name]);
609+
});
610+
});
611+
propertyNames(Enumerable.prototype).forEach(name => {
612+
const method = Enumerable.prototype[name];
613+
defineProperty(Generator, name, method);
614+
iterable.forEach(proto => {
615+
defineProperty(proto, name, method);
616+
});
581617
});
582618
})();

index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.ts

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
22+
// noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols
2223

2324
declare interface Generator<T> {
2425

@@ -630,7 +631,7 @@ type IEnumerable<T> = Generator<T>;
630631
any() : boolean;
631632
any(predicate : (item : T) => boolean) : boolean;
632633
any(predicate? : (item : T) => boolean) : boolean {
633-
return this.firstOrDefault(predicate) !== null;
634+
return this.firstOrDefault(predicate) != null;
634635
}
635636

636637
* append(element : T) : IEnumerable<T> {
@@ -1161,7 +1162,13 @@ type IEnumerable<T> = Generator<T>;
11611162
}
11621163
}
11631164

1164-
class PartialArray<T> extends Enumerable<T> implements IEnumerableArray<T> {
1165+
class PartialArrayLike<T> extends Enumerable<T> {
1166+
* asEnumerable() : IEnumerable<T> {
1167+
for (const item of this) yield item;
1168+
}
1169+
}
1170+
1171+
class PartialArray<T> extends PartialArrayLike<T> implements IEnumerableArray<T> {
11651172
private length : number;
11661173

11671174
add(item : T) : void {
@@ -1310,6 +1317,15 @@ type IEnumerable<T> = Generator<T>;
13101317
}
13111318
}
13121319

1320+
// noinspection JSUnresolvedReference
1321+
const Generator = (function* () {
1322+
// @ts-ignore
1323+
})().__proto__.__proto__;
1324+
const excepts = Object.getOwnPropertyNames(Generator);
1325+
1326+
function propertyNames(prototype : any) {
1327+
return Object.getOwnPropertyNames(prototype).filter(x => excepts.find(y => x == y) == null)
1328+
}
13131329

13141330
function defineProperty(
13151331
prototype : any,
@@ -1323,17 +1339,39 @@ type IEnumerable<T> = Generator<T>;
13231339
})
13241340
}
13251341

1326-
Object.getOwnPropertyNames(PartialMap.prototype).forEach(name => {
1342+
const iterable = [
1343+
Map.prototype,
1344+
String.prototype,
1345+
Array.prototype,
1346+
Int8Array.prototype,
1347+
Uint8Array.prototype,
1348+
Uint8ClampedArray.prototype,
1349+
Int16Array.prototype,
1350+
Uint16Array.prototype,
1351+
Int32Array.prototype,
1352+
Uint32Array.prototype,
1353+
Float32Array.prototype,
1354+
Float64Array.prototype,
1355+
BigInt64Array.prototype,
1356+
BigUint64Array.prototype,
1357+
]
1358+
1359+
propertyNames(PartialMap.prototype).forEach(name => {
13271360
defineProperty(Map.prototype, name, PartialMap.prototype[name])
13281361
})
1329-
Object.getOwnPropertyNames(PartialArray.prototype).forEach(name => {
1362+
propertyNames(PartialArray.prototype).forEach(name => {
13301363
defineProperty(Array.prototype, name, PartialArray.prototype[name])
13311364
})
1332-
Object.getOwnPropertyNames(Enumerable.prototype).forEach(name => {
1333-
defineProperty((function* () {
1334-
// @ts-ignore
1335-
})().__proto__.__proto__, name, Enumerable.prototype[name])
1336-
defineProperty(Array.prototype, name, Enumerable.prototype[name])
1337-
defineProperty(Map.prototype, name, Enumerable.prototype[name])
1365+
propertyNames(PartialArrayLike.prototype).forEach(name => {
1366+
iterable.forEach(proto => {
1367+
defineProperty(proto, name, PartialArrayLike.prototype[name])
1368+
})
1369+
})
1370+
propertyNames(Enumerable.prototype).forEach(name => {
1371+
const method = Enumerable.prototype[name];
1372+
defineProperty(Generator, name, method)
1373+
iterable.forEach(proto => {
1374+
defineProperty(proto, name, method)
1375+
})
13381376
});
13391377
})()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "linqes",
3-
"version" : "1.0.2",
3+
"version" : "1.0.3",
44
"description" : "linq in es, automated extensions on prototype",
55
"main" : "index.js",
66
"types" : "index.d.ts",

0 commit comments

Comments
 (0)