1+ // Simple JavaScript version for testing
2+ const DEFAULT_IMAGE_TRANSFORM_OPTIONS = {
3+ enableLossy : true ,
4+ lossyQuality : 30 ,
5+ } ;
6+
7+ function isBigCommerceCdnUrl ( url ) {
8+ if ( ! url ) return false ;
9+
10+ const cdnPatterns = [
11+ / c d n \d * \. b i g c o m m e r c e \. c o m / ,
12+ / b i g c o m m e r c e - .* \. s 3 \. a m a z o n a w s \. c o m / ,
13+ / s t o r e - .* \. m y b i g c o m m e r c e \. c o m / ,
14+ ] ;
15+
16+ return cdnPatterns . some ( pattern => pattern . test ( url ) ) ;
17+ }
18+
19+ function transformImageUrl ( url , options = { } ) {
20+ if ( ! url || typeof url !== 'string' ) {
21+ return url ;
22+ }
23+
24+ const config = { ...DEFAULT_IMAGE_TRANSFORM_OPTIONS , ...options } ;
25+
26+ if ( ! config . enableLossy ) {
27+ return url ;
28+ }
29+
30+ if ( ! isBigCommerceCdnUrl ( url ) ) {
31+ return url ;
32+ }
33+
34+ if ( url . includes ( 'lossy=' ) || url . includes ( 'quality=' ) ) {
35+ return url ;
36+ }
37+
38+ const separator = url . includes ( '?' ) ? '&' : '?' ;
39+ return `${ url } ${ separator } lossy=true&quality=${ config . lossyQuality } ` ;
40+ }
41+
42+ function transformImageUrls ( data , options = { } ) {
43+ if ( ! data || typeof data !== 'object' ) {
44+ return data ;
45+ }
46+
47+ if ( Array . isArray ( data ) ) {
48+ return data . map ( item => transformImageUrls ( item , options ) ) ;
49+ }
50+
51+ const result = { ...data } ;
52+ const imageUrlFields = [ 'url' , 'src' , 'urlOriginal' , 'urlStandard' , 'urlThumbnail' ] ;
53+
54+ for ( const field of imageUrlFields ) {
55+ if ( result [ field ] && typeof result [ field ] === 'string' ) {
56+ result [ field ] = transformImageUrl ( result [ field ] , options ) ;
57+ }
58+ }
59+
60+ for ( const [ key , value ] of Object . entries ( result ) ) {
61+ if ( value && typeof value === 'object' ) {
62+ result [ key ] = transformImageUrls ( value , options ) ;
63+ }
64+ }
65+
66+ return result ;
67+ }
68+
69+ // Test the functionality
70+ console . log ( 'Testing image transforms...' ) ;
71+
72+ // Test 1: Basic URL transformation
73+ const testUrl = 'https://cdn11.bigcommerce.com/s-123abc/products/123/images/456/image.jpg' ;
74+ const transformed = transformImageUrl ( testUrl ) ;
75+ const expected = `${ testUrl } ?lossy=true&quality=${ DEFAULT_IMAGE_TRANSFORM_OPTIONS . lossyQuality } ` ;
76+
77+ console . log ( 'Test 1 - Basic URL transformation:' ) ;
78+ console . log ( ' Input:' , testUrl ) ;
79+ console . log ( ' Output:' , transformed ) ;
80+ console . log ( ' Expected:' , expected ) ;
81+ console . log ( ' ✓ Pass:' , transformed === expected ) ;
82+
83+ // Test 2: Non-BigCommerce URL (should not transform)
84+ const externalUrl = 'https://example.com/image.jpg' ;
85+ const untransformed = transformImageUrl ( externalUrl ) ;
86+
87+ console . log ( '\nTest 2 - External URL (should not transform):' ) ;
88+ console . log ( ' Input:' , externalUrl ) ;
89+ console . log ( ' Output:' , untransformed ) ;
90+ console . log ( ' ✓ Pass:' , untransformed === externalUrl ) ;
91+
92+ // Test 3: URL with existing query params
93+ const urlWithParams = 'https://cdn11.bigcommerce.com/s-123abc/products/123/images/456/image.jpg?width=100' ;
94+ const transformedWithParams = transformImageUrl ( urlWithParams ) ;
95+ const expectedWithParams = `${ urlWithParams } &lossy=true&quality=${ DEFAULT_IMAGE_TRANSFORM_OPTIONS . lossyQuality } ` ;
96+
97+ console . log ( '\nTest 3 - URL with existing query params:' ) ;
98+ console . log ( ' Input:' , urlWithParams ) ;
99+ console . log ( ' Output:' , transformedWithParams ) ;
100+ console . log ( ' Expected:' , expectedWithParams ) ;
101+ console . log ( ' ✓ Pass:' , transformedWithParams === expectedWithParams ) ;
102+
103+ // Test 4: Nested object transformation
104+ const testData = {
105+ product : {
106+ name : 'Test Product' ,
107+ image : {
108+ url : 'https://cdn11.bigcommerce.com/s-123abc/products/123/images/456/image.jpg'
109+ }
110+ }
111+ } ;
112+
113+ const transformedData = transformImageUrls ( testData ) ;
114+
115+ console . log ( '\nTest 4 - Nested object transformation:' ) ;
116+ console . log ( ' Original URL:' , testData . product . image . url ) ;
117+ console . log ( ' Transformed URL:' , transformedData . product . image . url ) ;
118+ console . log ( ' ✓ Pass:' , transformedData . product . image . url . includes ( 'lossy=true' ) ) ;
119+
120+ // Test 5: Disabled lossy
121+ const disabledTransform = transformImageUrl ( testUrl , { enableLossy : false } ) ;
122+
123+ console . log ( '\nTest 5 - Disabled lossy:' ) ;
124+ console . log ( ' Input:' , testUrl ) ;
125+ console . log ( ' Output:' , disabledTransform ) ;
126+ console . log ( ' ✓ Pass:' , disabledTransform === testUrl ) ;
127+
128+ console . log ( '\nAll tests completed!' ) ;
0 commit comments