Improve pixel lens formatting

This commit is contained in:
Sam Becker 2025-04-04 22:13:10 -05:00
parent c564621e91
commit b6a5a0c5da
4 changed files with 28 additions and 24 deletions

View File

@ -17,6 +17,7 @@ describe('Lens', () => {
describe('correctly formats', () => { describe('correctly formats', () => {
it('iPhone lenses', () => { it('iPhone lenses', () => {
expect(formatLensText(IPHONE_15_PRO_FRONT)).toBe('15 Pro front'); 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_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_MAIN)).toBe('15 Pro Main (6.765mm)');
expect(formatLensText(IPHONE_15_PRO_BACK_TELEPHOTO)).toBe('15 Pro Telephoto (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)'); expect(formatLensText(IPHONE_12_PRO_MAX_BACK_TELEPHOTO_NO_MAKE)).toBe('12 Pro Max Main (5.1mm)');
}); });
it('Pixel lenses', () => { it('Pixel lenses', () => {
expect(formatLensText(PIXEL_8_PRO_BACK, '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 Camera (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_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)'); expect(formatLensText(PIXEL_6A_BACK, 'short')).toBe('Back Camera (2.35mm)');
}); });
}); });

View File

@ -105,21 +105,18 @@ export const formatLensText = (
); );
const model = isLensApple(lens) const model = isLensApple(lens)
? formatAppleLensText(modelRaw, length === 'medium') ? formatAppleLensText(modelRaw, length !== 'short')
: isLensGoogle(lens) : isLensGoogle(lens)
? formatGoogleLensText(modelRaw, length === 'medium') ? formatGoogleLensText(modelRaw, length !== 'short')
: doesModelStartWithMake
? modelRaw.replace(makeSimple, '').trim()
: modelRaw; : modelRaw;
switch (length) { switch (length) {
case 'long': case 'long':
return make ? `${make} ${model}` : model; return make ? `${make} ${modelRaw}` : modelRaw;
case 'medium': case 'medium':
return doesModelStartWithMake
? model.replace(makeSimple, '').trim()
: model;
case 'short': case 'short':
return doesModelStartWithMake return model;
? model.replace(makeSimple, '').trim()
: model;
} }
}; };

View File

@ -18,7 +18,7 @@ export const isLensApple = ({ make, model }: Lens) =>
export const formatAppleLensText = ( export const formatAppleLensText = (
model: string, model: string,
includePhoneName?: boolean, includeDeviceName?: boolean,
) => { ) => {
const [ const [
_, _,
@ -31,11 +31,11 @@ export const formatAppleLensText = (
const format = (lensName: string, includeFocalLength = true) => { const format = (lensName: string, includeFocalLength = true) => {
let result = ''; let result = '';
if (includePhoneName) { if (includeDeviceName) {
result += `${phoneName} `; result += `${phoneName} `;
} }
result += lensName; result += lensName;
if (!includePhoneName) { if (!includeDeviceName) {
result += ' Camera'; result += ' Camera';
} }
if (includeFocalLength && focalLength) { if (includeFocalLength && focalLength) {

View File

@ -19,22 +19,28 @@ export const isLensGoogle = ({ make, model }: Lens) =>
export const formatGoogleLensText = ( export const formatGoogleLensText = (
model: string, model: string,
includePhoneName?: boolean, includeDeviceName?: boolean,
) => { ) => {
const [ const [
_, _match,
phoneName, phoneName,
lensVariant, lensVariant, // Expected: 'Back' or 'Front'
lensVariantRemainder, // Expected: 'Camera'
focalLength, focalLength,
_aperture, _aperture,
// eslint-disable-next-line max-len // 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) { if (phoneName && lensVariant && focalLength) {
const lensName = `${capitalizeWords(lensVariant)} (${focalLength}mm)`; const lensName = capitalizeWords(lensVariant);
return includePhoneName const focalText = `(${focalLength}mm)`;
? `${phoneName} ${lensName}` if (includeDeviceName) {
: lensName; return `${phoneName} ${lensName} ${focalText}`;
} else {
return lensVariantRemainder
? `${lensName} ${capitalizeWords(lensVariantRemainder)} ${focalText}`
: `${lensName} ${focalText}`;
}
} }
return model; return model;