You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.7 KiB
54 lines
1.7 KiB
import type { NormalizedInternalData } from '../../../types';
|
|
import { createTargetHelpers, identity } from '../../../utils/objectHelpers';
|
|
import type { StoredData } from './types';
|
|
|
|
const { createRequiredField } = createTargetHelpers<NormalizedInternalData>([
|
|
null,
|
|
'',
|
|
]);
|
|
|
|
const internalRatingToRatings = (internalRating: StoredData['userrating']) => {
|
|
let totalStars = 0;
|
|
for (let i = 1; i <= 5; i++) {
|
|
totalStars += internalRating[`countStar${i as 1 | 2 | 3 | 4 | 5}`] * i;
|
|
}
|
|
|
|
const rating = totalStars / internalRating.countTotal;
|
|
|
|
return [
|
|
{
|
|
source: 'Internal',
|
|
value: `${rating.toFixed(1)}/5`,
|
|
},
|
|
];
|
|
};
|
|
|
|
export const normalizeRawInternalData = (
|
|
rawInternalData: StoredData,
|
|
): NormalizedInternalData => {
|
|
const {
|
|
description,
|
|
duration,
|
|
id,
|
|
imdbId,
|
|
languages,
|
|
originalLanguage,
|
|
productionYear,
|
|
studios,
|
|
title,
|
|
userrating,
|
|
} = rawInternalData;
|
|
|
|
return {
|
|
...createRequiredField('localDescription', description, identity),
|
|
...createRequiredField('duration', duration, identity),
|
|
...createRequiredField('internalId', id, identity<number>),
|
|
...createRequiredField('imdbId', imdbId, identity),
|
|
...createRequiredField('availableLanguages', languages, identity),
|
|
...createRequiredField('originalLanguage', originalLanguage, identity),
|
|
...createRequiredField('productionYear', productionYear, identity),
|
|
...createRequiredField('studios', studios, identity),
|
|
...createRequiredField('localTitle', title, identity),
|
|
...createRequiredField('ratings', userrating, internalRatingToRatings),
|
|
};
|
|
};
|
|
|