@@ -190,6 +190,21 @@ namespace ts {
190
190
return array ;
191
191
}
192
192
193
+ export function removeWhere < T > ( array : T [ ] , f : ( x : T ) => boolean ) : boolean {
194
+ let outIndex = 0 ;
195
+ for ( const item of array ) {
196
+ if ( ! f ( item ) ) {
197
+ array [ outIndex ] = item ;
198
+ outIndex ++ ;
199
+ }
200
+ }
201
+ if ( outIndex !== array . length ) {
202
+ array . length = outIndex ;
203
+ return true ;
204
+ }
205
+ return false ;
206
+ }
207
+
193
208
export function filterMutate < T > ( array : T [ ] , f : ( x : T ) => boolean ) : void {
194
209
let outIndex = 0 ;
195
210
for ( const item of array ) {
@@ -381,6 +396,9 @@ namespace ts {
381
396
/**
382
397
* Gets the owned, enumerable property keys of a map-like.
383
398
*
399
+ * NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
400
+ * Object.keys instead as it offers better performance.
401
+ *
384
402
* @param map A map-like.
385
403
*/
386
404
export function getOwnKeys < T > ( map : MapLike < T > ) : string [ ] {
@@ -425,6 +443,36 @@ namespace ts {
425
443
return result ;
426
444
}
427
445
446
+ /**
447
+ * Maps key-value pairs of a map into a new map.
448
+ *
449
+ * NOTE: The key-value pair passed to the callback is *not* safe to cache between invocations
450
+ * of the callback.
451
+ *
452
+ * @param map A map.
453
+ * @param callback A callback that maps a key-value pair into a new key-value pair.
454
+ */
455
+ export function mapPairs < T , U > ( map : Map < T > , callback : ( entry : [ string , T ] ) => [ string , U ] ) : Map < U > {
456
+ let result : Map < U > ;
457
+ if ( map ) {
458
+ result = createMap < U > ( ) ;
459
+ let inPair : [ string , T ] ;
460
+ for ( const key in map ) {
461
+ if ( inPair ) {
462
+ inPair [ 0 ] = key ;
463
+ inPair [ 1 ] = map [ key ] ;
464
+ }
465
+ else {
466
+ inPair = [ key , map [ key ] ] ;
467
+ }
468
+
469
+ const outPair = callback ( inPair ) ;
470
+ result [ outPair [ 0 ] ] = outPair [ 1 ] ;
471
+ }
472
+ }
473
+ return result ;
474
+ }
475
+
428
476
/**
429
477
* Returns true if a Map<T> has some matching property.
430
478
*
@@ -504,9 +552,31 @@ namespace ts {
504
552
return result ;
505
553
}
506
554
555
+ /**
556
+ * Counts the properties of a map.
557
+ *
558
+ * NOTE: This is intended for use with Map<T> objects. For MapLike<T> objects, use
559
+ * countOwnProperties instead as it offers better runtime safety.
560
+ *
561
+ * @param map A map whose properties should be counted.
562
+ * @param predicate An optional callback used to limit which properties should be counted.
563
+ */
564
+ export function countProperties < T > ( map : Map < T > , predicate ?: ( value : T , key : string ) => boolean ) {
565
+ let count = 0 ;
566
+ for ( const key in map ) {
567
+ if ( ! predicate || predicate ( map [ key ] , key ) ) {
568
+ count ++ ;
569
+ }
570
+ }
571
+ return count ;
572
+ }
573
+
507
574
/**
508
575
* Counts the owned properties of a map-like.
509
576
*
577
+ * NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
578
+ * countProperties instead as it offers better performance.
579
+ *
510
580
* @param map A map-like whose properties should be counted.
511
581
* @param predicate An optional callback used to limit which properties should be counted.
512
582
*/
@@ -521,16 +591,42 @@ namespace ts {
521
591
}
522
592
523
593
/**
524
- * Performs a shallow equality comparison of the contents of two map-likes.
594
+ * Performs a shallow equality comparison of the contents of two maps.
595
+ *
596
+ * NOTE: This is intended for use with Map<T> objects. For MapLike<T> objects, use
597
+ * equalOwnProperties instead as it offers better runtime safety.
525
598
*
526
599
* @param left A map whose properties should be compared.
527
600
* @param right A map whose properties should be compared.
528
601
*/
529
- export function equalOwnProperties < T > ( left : MapLike < T > , right : MapLike < T > ) {
602
+ export function equalProperties < T > ( left : Map < T > , right : Map < T > , equalityComparer ?: ( left : T , right : T ) => boolean ) {
603
+ if ( left === right ) return true ;
604
+ if ( ! left || ! right ) return false ;
605
+ for ( const key in left ) {
606
+ if ( ! ( key in right ) ) return false ;
607
+ if ( equalityComparer ? ! equalityComparer ( left [ key ] , right [ key ] ) : left [ key ] !== right [ key ] ) return false ;
608
+ }
609
+ for ( const key in right ) {
610
+ if ( ! ( key in left ) ) return false ;
611
+ }
612
+ return true ;
613
+ }
614
+
615
+ /**
616
+ * Performs a shallow equality comparison of the contents of two map-likes.
617
+ *
618
+ * NOTE: This is intended for use with MapLike<T> objects. For Map<T> objects, use
619
+ * equalProperties instead as it offers better performance.
620
+ *
621
+ * @param left A map-like whose properties should be compared.
622
+ * @param right A map-like whose properties should be compared.
623
+ */
624
+ export function equalOwnProperties < T > ( left : MapLike < T > , right : MapLike < T > , equalityComparer ?: ( left : T , right : T ) => boolean ) {
530
625
if ( left === right ) return true ;
531
626
if ( ! left || ! right ) return false ;
532
627
for ( const key in left ) if ( hasOwnProperty . call ( left , key ) ) {
533
- if ( ! hasOwnProperty . call ( right , key ) === undefined || left [ key ] !== right [ key ] ) return false ;
628
+ if ( ! hasOwnProperty . call ( right , key ) === undefined ) return false ;
629
+ if ( equalityComparer ? ! equalityComparer ( left [ key ] , right [ key ] ) : left [ key ] !== right [ key ] ) return false ;
534
630
}
535
631
for ( const key in right ) if ( hasOwnProperty . call ( right , key ) ) {
536
632
if ( ! hasOwnProperty . call ( left , key ) ) return false ;
@@ -577,10 +673,12 @@ namespace ts {
577
673
export function extend < T1 extends MapLike < { } > , T2 extends MapLike < { } > > ( first : T1 , second : T2 ) : T1 & T2 {
578
674
const result : T1 & T2 = < any > { } ;
579
675
for ( const id in first ) {
580
- ( result as any ) [ id ] = first [ id ] ;
676
+ if ( hasOwnProperty . call ( first , id ) ) {
677
+ ( result as any ) [ id ] = first [ id ] ;
678
+ }
581
679
}
582
680
for ( const id in second ) {
583
- if ( ! hasProperty ( result , id ) ) {
681
+ if ( hasOwnProperty . call ( second , id ) && ! hasOwnProperty . call ( result , id ) ) {
584
682
( result as any ) [ id ] = second [ id ] ;
585
683
}
586
684
}
0 commit comments