@@ -162,6 +162,74 @@ export namespace Sketch {
162
162
const { mainFileUri, otherSketchFileUris, additionalFileUris } = sketch ;
163
163
return [ mainFileUri , ...otherSketchFileUris , ...additionalFileUris ] ;
164
164
}
165
+ const primitiveProps : Array < keyof Sketch > = [ 'name' , 'uri' , 'mainFileUri' ] ;
166
+ const arrayProps : Array < keyof Sketch > = [
167
+ 'additionalFileUris' ,
168
+ 'otherSketchFileUris' ,
169
+ 'rootFolderFileUris' ,
170
+ ] ;
171
+ export function sameAs ( left : Sketch , right : Sketch ) : boolean {
172
+ for ( const prop of primitiveProps ) {
173
+ const leftValue = left [ prop ] ;
174
+ const rightValue = right [ prop ] ;
175
+ assertIsNotArray ( leftValue , prop , left ) ;
176
+ assertIsNotArray ( rightValue , prop , right ) ;
177
+ if ( leftValue !== rightValue ) {
178
+ return false ;
179
+ }
180
+ }
181
+ for ( const prop of arrayProps ) {
182
+ const leftValue = left [ prop ] ;
183
+ const rightValue = right [ prop ] ;
184
+ assertIsArray ( leftValue , prop , left ) ;
185
+ assertIsArray ( rightValue , prop , right ) ;
186
+ if ( leftValue . length !== rightValue . length ) {
187
+ return false ;
188
+ }
189
+ }
190
+ for ( const prop of arrayProps ) {
191
+ const leftValue = left [ prop ] ;
192
+ const rightValue = right [ prop ] ;
193
+ assertIsArray ( leftValue , prop , left ) ;
194
+ assertIsArray ( rightValue , prop , right ) ;
195
+ if (
196
+ toSortedString ( leftValue as string [ ] ) !==
197
+ toSortedString ( rightValue as string [ ] )
198
+ ) {
199
+ return false ;
200
+ }
201
+ }
202
+ return true ;
203
+ }
204
+ function toSortedString ( array : string [ ] ) : string {
205
+ return array . slice ( ) . sort ( ) . join ( ',' ) ;
206
+ }
207
+ function assertIsNotArray (
208
+ toTest : unknown ,
209
+ prop : keyof Sketch ,
210
+ object : Sketch
211
+ ) : void {
212
+ if ( Array . isArray ( toTest ) ) {
213
+ throw new Error (
214
+ `Expected a non-array type. Got: ${ toTest } . Property was: ${ prop } . Object was: ${ JSON . stringify (
215
+ object
216
+ ) } `
217
+ ) ;
218
+ }
219
+ }
220
+ function assertIsArray (
221
+ toTest : unknown ,
222
+ prop : keyof Sketch ,
223
+ object : Sketch
224
+ ) : void {
225
+ if ( ! Array . isArray ( toTest ) ) {
226
+ throw new Error (
227
+ `Expected an array type. Got: ${ toTest } . Property was: ${ prop } . Object was: ${ JSON . stringify (
228
+ object
229
+ ) } `
230
+ ) ;
231
+ }
232
+ }
165
233
}
166
234
167
235
export interface SketchContainer {
0 commit comments