@@ -190,6 +190,21 @@ namespace ts {
190190 return array ;
191191 }
192192
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+
193208 export function filterMutate < T > ( array : T [ ] , f : ( x : T ) => boolean ) : void {
194209 let outIndex = 0 ;
195210 for ( const item of array ) {
@@ -381,6 +396,9 @@ namespace ts {
381396 /**
382397 * Gets the owned, enumerable property keys of a map-like.
383398 *
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+ *
384402 * @param map A map-like.
385403 */
386404 export function getOwnKeys < T > ( map : MapLike < T > ) : string [ ] {
@@ -425,6 +443,36 @@ namespace ts {
425443 return result ;
426444 }
427445
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+
428476 /**
429477 * Returns true if a Map<T> has some matching property.
430478 *
@@ -504,9 +552,31 @@ namespace ts {
504552 return result ;
505553 }
506554
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+
507574 /**
508575 * Counts the owned properties of a map-like.
509576 *
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+ *
510580 * @param map A map-like whose properties should be counted.
511581 * @param predicate An optional callback used to limit which properties should be counted.
512582 */
@@ -521,16 +591,42 @@ namespace ts {
521591 }
522592
523593 /**
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.
525598 *
526599 * @param left A map whose properties should be compared.
527600 * @param right A map whose properties should be compared.
528601 */
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 ) {
530625 if ( left === right ) return true ;
531626 if ( ! left || ! right ) return false ;
532627 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 ;
534630 }
535631 for ( const key in right ) if ( hasOwnProperty . call ( right , key ) ) {
536632 if ( ! hasOwnProperty . call ( left , key ) ) return false ;
@@ -577,10 +673,12 @@ namespace ts {
577673 export function extend < T1 extends MapLike < { } > , T2 extends MapLike < { } > > ( first : T1 , second : T2 ) : T1 & T2 {
578674 const result : T1 & T2 = < any > { } ;
579675 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+ }
581679 }
582680 for ( const id in second ) {
583- if ( ! hasProperty ( result , id ) ) {
681+ if ( hasOwnProperty . call ( second , id ) && ! hasOwnProperty . call ( result , id ) ) {
584682 ( result as any ) [ id ] = second [ id ] ;
585683 }
586684 }
0 commit comments