From 075f104367a3b651761cf689fc413ce8d4a84ee7 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Fri, 28 Mar 2025 23:45:57 -0500 Subject: [PATCH] Offer abbreviated formatting for Pixel lenses --- __tests__/lens.test.ts | 18 +++++++++++++----- src/lens/index.ts | 5 ++++- src/platforms/apple.ts | 2 +- src/platforms/google.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 src/platforms/google.ts diff --git a/__tests__/lens.test.ts b/__tests__/lens.test.ts index d9a8a302..a159a92f 100644 --- a/__tests__/lens.test.ts +++ b/__tests__/lens.test.ts @@ -6,11 +6,19 @@ const IPHONE_15_PRO_BACK_WIDE: Lens = { make: 'Apple', model: 'iPhone 15 Pro bac const IPHONE_15_PRO_BACK_MAIN: Lens = { make: 'Apple', model: 'iPhone 15 Pro back triple camera 6.765mm f/1.78' }; const IPHONE_15_PRO_BACK_TELEPHOTO: Lens = { make: 'Apple', model: 'iPhone 15 Pro back triple camera 6.765mm f/2.8' }; +const PIXEL_8_PRO_BACK: Lens = { make: 'Google', model: 'Pixel 8 Pro back camera 6.9mm f/1.68' }; + describe('Lens', () => { - it('correctly formats iPhone lenses', () => { - expect(formatLensText(IPHONE_15_PRO_FRONT)).toBe('15 Pro front'); - 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)'); + describe('correctly formats', () => { + it('iPhone lenses', () => { + expect(formatLensText(IPHONE_15_PRO_FRONT)).toBe('15 Pro front'); + 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)'); + }); + it('Pixel lenses', () => { + expect(formatLensText(PIXEL_8_PRO_BACK, 'medium')).toBe('Pixel 8 Pro Back Camera (6.9mm)'); + expect(formatLensText(PIXEL_8_PRO_BACK, 'short')).toBe('Back Camera (6.9mm)'); + }); }); }); diff --git a/src/lens/index.ts b/src/lens/index.ts index 78a2fe17..a49f892b 100644 --- a/src/lens/index.ts +++ b/src/lens/index.ts @@ -2,6 +2,7 @@ import { Photo } from '@/photo'; import { parameterize } from '@/utility/string'; import { formatAppleLensText, isLensMakeApple } from '../platforms/apple'; import { MISSING_FIELD } from '@/app/paths'; +import { formatGoogleLensText, isLensMakeGoogle } from '../platforms/google'; const LENS_PLACEHOLDER: Lens = { make: 'Lens', model: 'Model' }; @@ -103,7 +104,9 @@ export const formatLensText = ( const model = isLensMakeApple(make) ? formatAppleLensText(modelRaw, length === 'medium') - : modelRaw; + : isLensMakeGoogle(make) + ? formatGoogleLensText(modelRaw, length === 'medium') + : modelRaw; switch (length) { case 'long': diff --git a/src/platforms/apple.ts b/src/platforms/apple.ts index 419b7bb6..f988b5f4 100644 --- a/src/platforms/apple.ts +++ b/src/platforms/apple.ts @@ -1,4 +1,3 @@ -/* eslint-disable max-len */ import { Camera } from '@/camera'; import { Lens } from '../lens'; @@ -26,6 +25,7 @@ export const formatAppleLensText = ( side, focalLength, aperture, + // eslint-disable-next-line max-len ] = (/iPhone ([0-9a-z]{1,3}(?: (?:Pro|Max|Plus))*).*?(back|front).*?([0-9\.]+)mm.*?f\/([0-9\.]+)/gi.exec(model) ?? []); const format = (lensName: string, includeFocalLength = true) => { diff --git a/src/platforms/google.ts b/src/platforms/google.ts new file mode 100644 index 00000000..e5047f63 --- /dev/null +++ b/src/platforms/google.ts @@ -0,0 +1,40 @@ +import { Camera } from '@/camera'; +import { Lens } from '../lens'; +import { capitalizeWords } from '@/utility/string'; + +const MAKE_GOOGLE = 'google'; + +export const isCameraMakeGoogle = (make?: string) => + make?.toLocaleLowerCase() === MAKE_GOOGLE; + +export const isCameraGoogle = ({ make }: Camera) => + isCameraMakeGoogle(make); + +export const isLensMakeGoogle = (make?: string) => + make?.toLocaleLowerCase() === MAKE_GOOGLE; + +export const isLensGoogle = ({ make }: Lens) => + isLensMakeGoogle(make); + +export const formatGoogleLensText = ( + model: string, + includePhoneName?: boolean, +) => { + const [ + _, + phoneName, + lensVariant, + focalLength, + _aperture, + // eslint-disable-next-line max-len + ] = (/^(Pixel (?:[0-9])+(?: Pro)*) (.+) ([0-9\.]+)mm.*?f\/([0-9\.]+)/gi.exec(model) ?? []); + + if (phoneName && lensVariant && focalLength) { + const lensName = `${capitalizeWords(lensVariant)} (${focalLength}mm)`; + return includePhoneName + ? `${phoneName} ${lensName}` + : lensName; + } + + return model; +};