( src: string | null | undefined, resize: ResizeImageResizeOptions, defaultImage?: string, )
| 29 | defaultImage: string, |
| 30 | ): string; |
| 31 | export function resizeImage( |
| 32 | src: string | null | undefined, |
| 33 | resize: ResizeImageResizeOptions, |
| 34 | defaultImage?: string, |
| 35 | ): string { |
| 36 | if (!src) { |
| 37 | return defaultImage as string; |
| 38 | } |
| 39 | |
| 40 | let newSrc = src.replace('hashnode.imgix.net', 'cdn.hashnode.com'); |
| 41 | if (src.indexOf('//res.cloudinary.com/hashnode') !== -1 && src.indexOf('/upload/') !== -1) { |
| 42 | const parts = src.split('/upload/'); |
| 43 | const format = parts[1].substring(parts[1].lastIndexOf('.') + 1); |
| 44 | if (parts[1].indexOf('ama_banners') !== -1) { |
| 45 | const version = parts[1].substring(1, parts[1].indexOf('/')); |
| 46 | const path = parts[1].substring(parts[1].indexOf('/') + 1, parts[1].lastIndexOf('.')); |
| 47 | newSrc = `${parts[0]}/upload/${path}/${version}.${format}?auto=compress,format&format=webp`; |
| 48 | } else { |
| 49 | const nextParts = parts[1].split('/'); |
| 50 | if (nextParts[0].indexOf('v') === 0) { |
| 51 | nextParts[0] = nextParts[0].substring(1); |
| 52 | } |
| 53 | newSrc = `${parts[0]}/upload/${nextParts[1].substring(0, nextParts[1].lastIndexOf('.'))}/${ |
| 54 | nextParts[0] |
| 55 | }.${format}?auto=compress,format&format=webp`; |
| 56 | } |
| 57 | newSrc = newSrc |
| 58 | .replace('//res.cloudinary.com', '//cdn.hashnode.com/res') |
| 59 | .replace('http://', 'https://'); |
| 60 | } else if (src.indexOf('//cdn.hashnode.com') !== -1 && src.indexOf('/upload/') !== -1) { |
| 61 | const parts = src.split('/upload/'); |
| 62 | if (parts[1].indexOf('v') !== 0) { |
| 63 | newSrc = `${parts[0]}/upload/${parts[1].substring(parts[1].indexOf('/') + 1)}`; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (newSrc.indexOf('//cdn.hashnode.com') === -1) { |
| 68 | return newSrc; |
| 69 | } |
| 70 | |
| 71 | let opts = ''; |
| 72 | Object.keys(resize).forEach((prop) => { |
| 73 | if (prop === 'w' || prop === 'h' || prop === 'mask' || prop === 'corner-radius') { |
| 74 | opts += `${prop}=${resize[prop]}&`; |
| 75 | } else if (prop === 'fill') { |
| 76 | opts += `fit=fill&fill=${resize[prop]}&`; |
| 77 | } else if (prop === 'c') { |
| 78 | opts += `fit=crop&crop=${resize[prop] === 'face' ? 'faces' : 'entropy'}&`; |
| 79 | } |
| 80 | }); |
| 81 | |
| 82 | if (resize.q === 'none') { |
| 83 | return `${newSrc}?${opts}`; |
| 84 | } |
| 85 | |
| 86 | if (newSrc.indexOf('?') !== -1) { |
| 87 | let newSrcSplit = newSrc.split('?'); |
| 88 | newSrc = newSrcSplit[0]; |
no outgoing calls
no test coverage detected