diff --git a/__tests__/lens.test.ts b/__tests__/lens.test.ts index d1ad1d97..e00a93f5 100644 --- a/__tests__/lens.test.ts +++ b/__tests__/lens.test.ts @@ -17,6 +17,7 @@ describe('Lens', () => { describe('correctly formats', () => { it('iPhone lenses', () => { expect(formatLensText(IPHONE_15_PRO_FRONT)).toBe('15 Pro front'); + expect(formatLensText(IPHONE_15_PRO_FRONT, 'long')).toBe('Apple iPhone 15 Pro front TrueDepth camera 2.69mm f/1.9'); expect(formatLensText(IPHONE_15_PRO_BACK_WIDE)).toBe('15 Pro Wide (6.765mm)'); expect(formatLensText(IPHONE_15_PRO_BACK_MAIN)).toBe('15 Pro Main (6.765mm)'); expect(formatLensText(IPHONE_15_PRO_BACK_TELEPHOTO)).toBe('15 Pro Telephoto (6.765mm)'); @@ -25,10 +26,10 @@ describe('Lens', () => { expect(formatLensText(IPHONE_12_PRO_MAX_BACK_TELEPHOTO_NO_MAKE)).toBe('12 Pro Max Main (5.1mm)'); }); it('Pixel lenses', () => { - expect(formatLensText(PIXEL_8_PRO_BACK, 'medium')).toBe('Pixel 8 Pro Back Camera (6.9mm)'); - expect(formatLensText(PIXEL_8_PRO_BACK_NO_MAKE, 'medium')).toBe('Pixel 8 Pro Back Camera (6.9mm)'); + expect(formatLensText(PIXEL_8_PRO_BACK, 'medium')).toBe('Pixel 8 Pro Back (6.9mm)'); + expect(formatLensText(PIXEL_8_PRO_BACK_NO_MAKE, 'medium')).toBe('Pixel 8 Pro Back (6.9mm)'); expect(formatLensText(PIXEL_8_PRO_BACK, 'short')).toBe('Back Camera (6.9mm)'); - expect(formatLensText(PIXEL_6A_BACK, 'medium')).toBe('Pixel 6a Back Camera (2.35mm)'); + expect(formatLensText(PIXEL_6A_BACK, 'medium')).toBe('Pixel 6a Back (2.35mm)'); expect(formatLensText(PIXEL_6A_BACK, 'short')).toBe('Back Camera (2.35mm)'); }); }); diff --git a/src/lens/index.ts b/src/lens/index.ts index 21393026..d91e0c53 100644 --- a/src/lens/index.ts +++ b/src/lens/index.ts @@ -105,21 +105,18 @@ export const formatLensText = ( ); const model = isLensApple(lens) - ? formatAppleLensText(modelRaw, length === 'medium') + ? formatAppleLensText(modelRaw, length !== 'short') : isLensGoogle(lens) - ? formatGoogleLensText(modelRaw, length === 'medium') - : modelRaw; + ? formatGoogleLensText(modelRaw, length !== 'short') + : doesModelStartWithMake + ? modelRaw.replace(makeSimple, '').trim() + : modelRaw; switch (length) { case 'long': - return make ? `${make} ${model}` : model; + return make ? `${make} ${modelRaw}` : modelRaw; case 'medium': - return doesModelStartWithMake - ? model.replace(makeSimple, '').trim() - : model; case 'short': - return doesModelStartWithMake - ? model.replace(makeSimple, '').trim() - : model; + return model; } }; diff --git a/src/platforms/apple.ts b/src/platforms/apple.ts index fcc84d63..eca96a76 100644 --- a/src/platforms/apple.ts +++ b/src/platforms/apple.ts @@ -18,7 +18,7 @@ export const isLensApple = ({ make, model }: Lens) => export const formatAppleLensText = ( model: string, - includePhoneName?: boolean, + includeDeviceName?: boolean, ) => { const [ _, @@ -31,11 +31,11 @@ export const formatAppleLensText = ( const format = (lensName: string, includeFocalLength = true) => { let result = ''; - if (includePhoneName) { + if (includeDeviceName) { result += `${phoneName} `; } result += lensName; - if (!includePhoneName) { + if (!includeDeviceName) { result += ' Camera'; } if (includeFocalLength && focalLength) { diff --git a/src/platforms/google.ts b/src/platforms/google.ts index 80d43c63..0f03c39a 100644 --- a/src/platforms/google.ts +++ b/src/platforms/google.ts @@ -19,22 +19,28 @@ export const isLensGoogle = ({ make, model }: Lens) => export const formatGoogleLensText = ( model: string, - includePhoneName?: boolean, + includeDeviceName?: boolean, ) => { const [ - _, + _match, phoneName, - lensVariant, + lensVariant, // Expected: 'Back' or 'Front' + lensVariantRemainder, // Expected: 'Camera' focalLength, _aperture, // eslint-disable-next-line max-len - ] = (/^(Pixel (?:[0-9a-z])+(?: Pro)*) (.+) ([0-9\.]+)mm.*?f\/([0-9\.]+)/gi.exec(model) ?? []); + ] = (/^(Pixel (?:[0-9a-z])+(?: Pro)*)(?: (back|front))* (.+) ([0-9\.]+)mm.*?f\/([0-9\.]+)/gi.exec(model) ?? []); if (phoneName && lensVariant && focalLength) { - const lensName = `${capitalizeWords(lensVariant)} (${focalLength}mm)`; - return includePhoneName - ? `${phoneName} ${lensName}` - : lensName; + const lensName = capitalizeWords(lensVariant); + const focalText = `(${focalLength}mm)`; + if (includeDeviceName) { + return `${phoneName} ${lensName} ${focalText}`; + } else { + return lensVariantRemainder + ? `${lensName} ${capitalizeWords(lensVariantRemainder)} ${focalText}` + : `${lensName} ${focalText}`; + } } return model;