Generalize camera make/model text formatting

This commit is contained in:
Sam Becker 2025-02-03 23:22:09 -06:00
parent 2673c0dbb0
commit 1a154e71bd
2 changed files with 16 additions and 10 deletions

View File

@ -5,9 +5,10 @@ const APPLE_01 : Camera = { make: 'Apple', model: 'iPhone 11' };
const APPLE_02 : Camera = { make: 'Apple', model: 'iPhone 15 Pro Max' };
const FUJIFILM : Camera = { make: 'Fujifilm', model: 'X-T5' };
const CANON : Camera = { make: 'Canon', model: 'Canon EOS 800D' };
const NIKON : Camera = {
make: 'Nikon Corporation',
model: 'Nikon D7000',
const NIKON : Camera = { make: 'Nikon Corporation', model: 'Nikon D7000' };
const RICOH : Camera = {
make: 'RICOH IMAGING COMPANY, LTD.',
model: 'RICOH GR III',
};
describe('Camera', () => {
@ -19,6 +20,7 @@ describe('Camera', () => {
expect(formatCameraText(FUJIFILM)).toBe('Fujifilm X-T5');
expect(formatCameraText(CANON)).toBe('Canon EOS 800D');
expect(formatCameraText(NIKON)).toBe('Nikon D7000');
expect(formatCameraText(RICOH)).toBe('RICOH GR III');
});
it('labels models correctly', () => {
expect(formatCameraText(APPLE, 'never')).toBe('iPhone 11 Pro');

View File

@ -56,14 +56,17 @@ export const cameraFromPhoto = (
: fallback ?? CAMERA_PLACEHOLDER;
export const formatCameraText = (
{ make: makeRaw, model: modelRaw }: Camera,
{ make, model: modelRaw }: Camera,
includeMake: 'always' | 'never' | 'if-not-apple' = 'if-not-apple',
removeIPhoneOnLongModels?: boolean,
) => {
// Remove 'Corporation' from makes like 'Nikon Corporation'
const make = makeRaw.replace(/ Corporation/i, '');
// Remove potential duplicate make from model
let model = modelRaw.replace(`${make} `, '');
// Capture simple make without modifiers like 'Corporation' or 'Company'
const makeSimple = make.match(/^(\S+)/)?.[1];
const doesModelStartWithMake = (
makeSimple &&
modelRaw.toLocaleLowerCase().startsWith(makeSimple.toLocaleLowerCase())
);
let model = modelRaw;
if (
removeIPhoneOnLongModels &&
model.includes('iPhone') &&
@ -73,7 +76,8 @@ export const formatCameraText = (
}
return (
includeMake === 'never' ||
includeMake === 'if-not-apple' && make === 'Apple'
doesModelStartWithMake ||
(includeMake === 'if-not-apple' && make === 'Apple')
) ? model
: `${make} ${model}`;
};