22 * Copyright (c) Microsoft Corporation. All rights reserved.
33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
5+
56'use strict' ;
67
7- import * as Types from 'vs/base/common/types' ;
8+ import { isObject , isUndefinedOrNull , isArray } from 'vs/base/common/types' ;
89
910export function clone < T > ( obj : T ) : T {
1011 if ( ! obj || typeof obj !== 'object' ) {
@@ -14,7 +15,7 @@ export function clone<T>(obj: T): T {
1415 // See https://github.com/Microsoft/TypeScript/issues/10990
1516 return obj as any ;
1617 }
17- var result = ( Array . isArray ( obj ) ) ? < any > [ ] : < any > { } ;
18+ const result = ( Array . isArray ( obj ) ) ? < any > [ ] : < any > { } ;
1819 Object . keys ( obj ) . forEach ( ( key ) => {
1920 if ( obj [ key ] && typeof obj [ key ] === 'object' ) {
2021 result [ key ] = clone ( obj [ key ] ) ;
@@ -29,7 +30,7 @@ export function deepClone<T>(obj: T): T {
2930 if ( ! obj || typeof obj !== 'object' ) {
3031 return obj ;
3132 }
32- var result = ( Array . isArray ( obj ) ) ? < any > [ ] : < any > { } ;
33+ const result = ( Array . isArray ( obj ) ) ? < any > [ ] : < any > { } ;
3334 Object . getOwnPropertyNames ( obj ) . forEach ( ( key ) => {
3435 if ( obj [ key ] && typeof obj [ key ] === 'object' ) {
3536 result [ key ] = deepClone ( obj [ key ] ) ;
@@ -40,37 +41,37 @@ export function deepClone<T>(obj: T): T {
4041 return result ;
4142}
4243
43- var hasOwnProperty = Object . prototype . hasOwnProperty ;
44+ const hasOwnProperty = Object . prototype . hasOwnProperty ;
4445
4546export function cloneAndChange ( obj : any , changer : ( orig : any ) => any ) : any {
4647 return _cloneAndChange ( obj , changer , [ ] ) ;
4748}
4849
4950function _cloneAndChange ( obj : any , changer : ( orig : any ) => any , encounteredObjects : any [ ] ) : any {
50- if ( Types . isUndefinedOrNull ( obj ) ) {
51+ if ( isUndefinedOrNull ( obj ) ) {
5152 return obj ;
5253 }
5354
54- var changed = changer ( obj ) ;
55+ const changed = changer ( obj ) ;
5556 if ( typeof changed !== 'undefined' ) {
5657 return changed ;
5758 }
5859
59- if ( Types . isArray ( obj ) ) {
60- var r1 : any [ ] = [ ] ;
61- for ( var i1 = 0 ; i1 < obj . length ; i1 ++ ) {
60+ if ( isArray ( obj ) ) {
61+ const r1 : any [ ] = [ ] ;
62+ for ( let i1 = 0 ; i1 < obj . length ; i1 ++ ) {
6263 r1 . push ( _cloneAndChange ( obj [ i1 ] , changer , encounteredObjects ) ) ;
6364 }
6465 return r1 ;
6566 }
6667
67- if ( Types . isObject ( obj ) ) {
68+ if ( isObject ( obj ) ) {
6869 if ( encounteredObjects . indexOf ( obj ) >= 0 ) {
6970 throw new Error ( 'Cannot clone recursive data-structure' ) ;
7071 }
7172 encounteredObjects . push ( obj ) ;
72- var r2 = { } ;
73- for ( var i2 in obj ) {
73+ const r2 = { } ;
74+ for ( let i2 in obj ) {
7475 if ( hasOwnProperty . call ( obj , i2 ) ) {
7576 r2 [ i2 ] = _cloneAndChange ( obj [ i2 ] , changer , encounteredObjects ) ;
7677 }
@@ -87,15 +88,15 @@ function _cloneAndChange(obj: any, changer: (orig: any) => any, encounteredObjec
8788 * if existing properties on the destination should be overwritten or not. Defaults to true (overwrite).
8889 */
8990export function mixin ( destination : any , source : any , overwrite : boolean = true ) : any {
90- if ( ! Types . isObject ( destination ) ) {
91+ if ( ! isObject ( destination ) ) {
9192 return source ;
9293 }
9394
94- if ( Types . isObject ( source ) ) {
95+ if ( isObject ( source ) ) {
9596 Object . keys ( source ) . forEach ( ( key ) => {
9697 if ( key in destination ) {
9798 if ( overwrite ) {
98- if ( Types . isObject ( destination [ key ] ) && Types . isObject ( source [ key ] ) ) {
99+ if ( isObject ( destination [ key ] ) && isObject ( source [ key ] ) ) {
99100 mixin ( destination [ key ] , source [ key ] , overwrite ) ;
100101 } else {
101102 destination [ key ] = source [ key ] ;
@@ -135,8 +136,8 @@ export function equals(one: any, other: any): boolean {
135136 return false ;
136137 }
137138
138- var i : number ,
139- key : string ;
139+ let i : number ;
140+ let key : string ;
140141
141142 if ( Array . isArray ( one ) ) {
142143 if ( one . length !== other . length ) {
@@ -148,13 +149,13 @@ export function equals(one: any, other: any): boolean {
148149 }
149150 }
150151 } else {
151- var oneKeys : string [ ] = [ ] ;
152+ const oneKeys : string [ ] = [ ] ;
152153
153154 for ( key in one ) {
154155 oneKeys . push ( key ) ;
155156 }
156157 oneKeys . sort ( ) ;
157- var otherKeys : string [ ] = [ ] ;
158+ const otherKeys : string [ ] = [ ] ;
158159 for ( key in other ) {
159160 otherKeys . push ( key ) ;
160161 }
@@ -178,8 +179,8 @@ export function ensureProperty(obj: any, property: string, defaultValue: any) {
178179}
179180
180181export function arrayToHash ( array : any [ ] ) {
181- var result : any = { } ;
182- for ( var i = 0 ; i < array . length ; ++ i ) {
182+ const result : any = { } ;
183+ for ( let i = 0 ; i < array . length ; ++ i ) {
183184 result [ array [ i ] ] = true ;
184185 }
185186 return result ;
@@ -193,7 +194,7 @@ export function createKeywordMatcher(arr: string[], caseInsensitive: boolean = f
193194 if ( caseInsensitive ) {
194195 arr = arr . map ( function ( x ) { return x . toLowerCase ( ) ; } ) ;
195196 }
196- var hash = arrayToHash ( arr ) ;
197+ const hash = arrayToHash ( arr ) ;
197198 if ( caseInsensitive ) {
198199 return function ( word ) {
199200 return hash [ word . toLowerCase ( ) ] !== undefined && hash . hasOwnProperty ( word . toLowerCase ( ) ) ;
@@ -211,19 +212,18 @@ export function createKeywordMatcher(arr: string[], caseInsensitive: boolean = f
211212 * to call this method before the constructor definition.
212213 */
213214export function derive ( baseClass : any , derivedClass : any ) : void {
214-
215- for ( var prop in baseClass ) {
215+ for ( let prop in baseClass ) {
216216 if ( baseClass . hasOwnProperty ( prop ) ) {
217217 derivedClass [ prop ] = baseClass [ prop ] ;
218218 }
219219 }
220220
221221 derivedClass = derivedClass || function ( ) { } ;
222- var basePrototype = baseClass . prototype ;
223- var derivedPrototype = derivedClass . prototype ;
222+ const basePrototype = baseClass . prototype ;
223+ const derivedPrototype = derivedClass . prototype ;
224224 derivedClass . prototype = Object . create ( basePrototype ) ;
225225
226- for ( var prop in derivedPrototype ) {
226+ for ( let prop in derivedPrototype ) {
227227 if ( derivedPrototype . hasOwnProperty ( prop ) ) {
228228 // handle getters and setters properly
229229 Object . defineProperty ( derivedClass . prototype , prop , Object . getOwnPropertyDescriptor ( derivedPrototype , prop ) ) ;
@@ -240,10 +240,9 @@ export function derive(baseClass: any, derivedClass: any): void {
240240 * "Uncaught TypeError: Converting circular structure to JSON"
241241 */
242242export function safeStringify ( obj : any ) : string {
243- var seen : any [ ] = [ ] ;
243+ const seen : any [ ] = [ ] ;
244244 return JSON . stringify ( obj , ( key , value ) => {
245-
246- if ( Types . isObject ( value ) || Array . isArray ( value ) ) {
245+ if ( isObject ( value ) || Array . isArray ( value ) ) {
247246 if ( seen . indexOf ( value ) !== - 1 ) {
248247 return '[Circular]' ;
249248 } else {
0 commit comments