File tree 2 files changed +41
-2
lines changed
packages/gatsby-plugin-sharp/src
2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change 1
- const { createArgsDigest } = require ( `../process-file` )
1
+ const { createArgsDigest, sortKeys } = require ( `../process-file` )
2
2
3
3
describe ( `createArgsDigest` , ( ) => {
4
4
const defaultArgsBaseline = {
@@ -94,5 +94,25 @@ describe(`createArgsDigest`, () => {
94
94
testHashEqual ( `maxWidth` , { maxWidth : 500 } )
95
95
testHashEqual ( `base64` , { base64 : true } )
96
96
} )
97
+
98
+ describe ( `argument sorting` , ( ) => {
99
+ it ( `sorts nested arguments` , ( ) => {
100
+ const args = {
101
+ duotone : {
102
+ shadow : `#10c5f8` ,
103
+ highlight : `#32CD32` ,
104
+ } ,
105
+ cropFocus : 17 ,
106
+ }
107
+ const actual = sortKeys ( args )
108
+ expect ( actual ) . toEqual ( {
109
+ cropFocus : 17 ,
110
+ duotone : {
111
+ highlight : `#32CD32` ,
112
+ shadow : `#10c5f8` ,
113
+ } ,
114
+ } )
115
+ } )
116
+ } )
97
117
} )
98
118
} )
Original file line number Diff line number Diff line change @@ -241,10 +241,29 @@ exports.createArgsDigest = args => {
241
241
242
242
const argsDigest = crypto
243
243
. createHash ( `md5` )
244
- . update ( JSON . stringify ( filtered , Object . keys ( filtered ) . sort ( ) ) )
244
+ . update ( JSON . stringify ( sortKeys ( filtered ) ) )
245
245
. digest ( `hex` )
246
246
247
247
const argsDigestShort = argsDigest . substr ( argsDigest . length - 5 )
248
248
249
249
return argsDigestShort
250
250
}
251
+
252
+ const sortKeys = object => {
253
+ var sortedObj = { } ,
254
+ keys = _ . keys ( object )
255
+
256
+ keys = _ . sortBy ( keys , key => key )
257
+
258
+ _ . each ( keys , key => {
259
+ if ( typeof object [ key ] == `object` && ! ( object [ key ] instanceof Array ) ) {
260
+ sortedObj [ key ] = sortKeys ( object [ key ] )
261
+ } else {
262
+ sortedObj [ key ] = object [ key ]
263
+ }
264
+ } )
265
+
266
+ return sortedObj
267
+ }
268
+
269
+ exports . sortKeys = sortKeys
You can’t perform that action at this time.
0 commit comments