Skip to content

Commit

Permalink
Avoid using accumulator destructuring in reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
bperel committed Sep 23, 2024
1 parent 581ceb4 commit 9e6cd5d
Show file tree
Hide file tree
Showing 27 changed files with 273 additions and 396 deletions.
5 changes: 1 addition & 4 deletions apps/edgecreator/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { createI18n } from "vue-i18n";
import fr from "../locales/fr-FR.json";

const messages = {
"en-US": Object.keys(fr).reduce(
(acc, value) => ({ ...acc, [value]: value }),
{},
),
"en-US": Object.fromEntries(Object.keys(fr).map(key => [key, key])),
fr,
};

Expand Down
8 changes: 1 addition & 7 deletions apps/edgecreator/src/pages/[...all].vue
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,7 @@ watch(
const publicationcodes = [
...mostWantedEdges.value!.map(({ issuecode }) => issuecode!),
...Object.values(currentEdges.value).map(({ issuecode }) => issuecode),
].reduce<string[]>(
(acc, issuecode) => [
...acc,
issuecodeDetails.value[issuecode].publicationcode,
],
[]
);
].map((issuecode) => issuecodeDetails.value[issuecode].publicationcode);
await coaStore.fetchPublicationNames(publicationcodes);
isUploadableEdgesCarouselReady.value = true;
Expand Down
12 changes: 2 additions & 10 deletions apps/web/src/components/ContextMenuOnSaleByOthers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,11 @@ const selectedIssues = $computed(() =>
Object.keys(selectedIssueIdsByIssuecode),
);
const issueIds = $computed(() =>
Object.values(selectedIssueIdsByIssuecode).reduce<number[]>(
(acc, issues) => [...acc, ...issues.map(({ id }) => id!)],
[],
),
Object.values(selectedIssueIdsByIssuecode).map(({ id }) => id),
);
const selectedIssuesBuyerIds = $computed(() => [
...new Set(
issueIds.reduce<number[]>(
(acc, issueId) => [...acc, issuesOnSaleById.value[issueId].userId],
[],
),
),
...new Set(issueIds.map((issueId) => issuesOnSaleById.value[issueId].userId)),
]);
const issuesWithMultipleCopiesSelected = $computed(() =>
Expand Down
51 changes: 24 additions & 27 deletions apps/web/src/components/IssueList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,14 @@ let userIssuecodesNotFoundForPublication = $shallowRef<string[] | null>([]);
let selected = $shallowRef<string[]>([]);
const filteredUserCopies = $computed(() =>
filteredIssues.reduce<(issue & { issuecode: string })[]>(
(acc, { userCopies }) => [...acc, ...userCopies],
(acc, { userCopies }) => {
acc.push(...userCopies);
return acc;
},
[],
),
);
const copiesBySelectedIssuecode = $computed(() =>
selected.reduce<{ [issuecode: string]: issue[] }>((acc, issueKey) => {
const [issuecode, maybeIssueId] = issueKey.split("-id-");
Expand Down Expand Up @@ -460,10 +464,7 @@ const showFilter = $computed(
);
const issueIds = $computed(() =>
Object.values(copiesBySelectedIssuecode).reduce<number[]>(
(acc, issues) => [...acc, ...issues.map(({ id }) => id)],
[],
),
Object.values(copiesBySelectedIssuecode).map(({ id }) => id),
);
let contextMenuKey = $ref<string>("context-menu");
Expand Down Expand Up @@ -605,31 +606,27 @@ const loadIssues = async () => {
];
issues = coaIssuecodes
.filter((issuecode) => userIssuecodes.includes(issuecode))
.reduce<issueWithCopies[]>(
(acc, issuecode) => [
...acc,
...userIssuesForPublication!
.filter(
({ issuecode: userIssuecode }) => userIssuecode === issuecode,
)
.map((issue) => ({
...issue,
key: `${issue.issuecode.replaceAll(" ", "_")}-id-${issue.id}`,
userCopies: [{ ...issue, copyIndex: 0 }],
})),
],
[],
);
.reduce<issueWithCopies[]>((acc, issuecode) => {
const filteredIssues = userIssuesForPublication!
.filter(
({ issuecode: userIssuecode }) => userIssuecode === issuecode,
)
.map((issue) => ({
...issue,
key: `${issue.issuecode.replaceAll(" ", "_")}-id-${issue.id}`,
userCopies: [{ ...issue, copyIndex: 0 }],
}));
acc.push(...filteredIssues);
return acc;
}, []);
}
if (duplicatesOnly) {
const countPerIssuecode = issues!.reduce<{
[issuecode: string]: number;
}>(
(acc, { userCopies }) => ({
...acc,
[userCopies[0].issuecode]: (acc[userCopies[0].issuecode] || 0) + 1,
}),
const countPerIssuecode = issues!.reduce<{ [issuecode: string]: number }>(
(acc, { userCopies: [{ issuecode }] }) => {
acc[issuecode] = (acc[issuecode] || 0) + 1;
return acc;
},
{},
);
issues = issues!.filter(
Expand Down
25 changes: 12 additions & 13 deletions apps/web/src/components/PublicationList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,18 @@ const sortedCountries = $computed(
) || 0,
),
);
const publicationsPerCountry = $computed(
() =>
totalPerCountry.value &&
hasPublicationNames &&
Object.keys(totalPerCountry.value).reduce<{ [key: string]: string[] }>(
(acc, country) => ({
...acc,
[country]: Object.keys(totalPerPublication.value!).filter(
(publicationcode) => publicationcode.split("/")[0] === country,
),
}),
{},
),
const publicationsPerCountry = $computed(() =>
!totalPerCountry.value || !hasPublicationNames
? {}
: Object.keys(totalPerCountry.value).reduce<{ [key: string]: string[] }>(
(acc, country) => {
acc[country] = Object.keys(totalPerPublication.value!).filter(
(publicationcode) => publicationcode.split("/")[0] === country,
);
return acc;
},
{},
),
);
const getSortedPublications = (country: string) =>
publicationsPerCountry?.[country]?.sort((a, b) =>
Expand Down
37 changes: 16 additions & 21 deletions apps/web/src/composables/useCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,18 @@ export default (issues: ShallowRef<(issue & { issuecode: string })[]>) => {
issuesByIssuecode = computed(() =>
issues.value?.groupBy("issuecode", "[]"),
),
duplicateIssues = computed(
(): {
[issuecode: string]: issue[];
} =>
(issuesByIssuecode.value &&
Object.keys(issuesByIssuecode.value).reduce(
(acc, issuecode) =>
issuesByIssuecode.value![issuecode].length > 1
? {
...acc,
[issuecode]: issuesByIssuecode.value![issuecode],
}
: acc,
{},
)) ||
duplicateIssues = computed(() => {
const issues = issuesByIssuecode.value || {};
return Object.keys(issues).reduce<{ [issuecode: string]: issue[] }>(
(acc, issuecode) => {
if (issues[issuecode].length > 1) {
acc[issuecode] = issues[issuecode];
}
return acc;
},
{},
),
);
}),
issuesInToReadStack = computed(() =>
issues.value?.filter(({ isToRead }) => isToRead),
),
Expand All @@ -69,11 +64,11 @@ export default (issues: ShallowRef<(issue & { issuecode: string })[]>) => {
),
totalPerCountry = computed(() =>
issues.value?.reduce<{ [countrycode: string]: number }>(
(acc, issue) => ({
...acc,
[issue.publicationcode.split("/")[0]]:
(acc[issue.publicationcode.split("/")[0]] || 0) + 1,
}),
(acc, { publicationcode }) => {
const countrycode = publicationcode.split("/")[0];
acc[countrycode] = (acc[countrycode] || 0) + 1;
return acc;
},
{},
),
),
Expand Down
9 changes: 6 additions & 3 deletions apps/web/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ export default (
if (!instance) {
const messages = {
...translations,
[defaultLocale]: Object.keys(
translations[Object.keys(translations)[0]],
).reduce((acc, value) => ({ ...acc, [value]: value }), {}),
[defaultLocale]: Object.fromEntries(
Object.keys(translations[Object.keys(translations)[0]]).map((key) => [
key,
key,
]),
),
};

const fallbackLocale = defaultLocale;
Expand Down
62 changes: 27 additions & 35 deletions apps/web/src/pages/admin/edges/progress.vue
Original file line number Diff line number Diff line change
Expand Up @@ -160,46 +160,38 @@ const open = (inducksIssuecode: string) => {
const inducksIssuenumbers = $computed(() =>
Object.keys(issuecodesByPublicationcode.value).reduce<
Record<string, string[]>
>(
(acc, publicationcode) => ({
...acc,
[publicationcode]: Object.values(
issuecodesByPublicationcode.value[publicationcode],
).map((issuecode) =>
issuecodeDetails.value[issuecode].issuenumber.replaceAll(" ", ""),
),
}),
{},
),
>((acc, publicationcode) => {
acc[publicationcode] = Object.values(
issuecodesByPublicationcode.value[publicationcode],
).map((issuecode) =>
issuecodeDetails.value[issuecode].issuenumber.replaceAll(" ", ""),
);
return acc;
}, {}),
);
const sortedBookcase = computed(() =>
Object.values(showEdgesForPublication).reduce<
Record<string, BookcaseEdgeWithPopularity[]>
>(
(acc, publicationcode) => ({
...acc,
[publicationcode]:
issuecodesByPublicationcode.value[publicationcode]?.map(
(issuecode) => ({
id: 0,
edgeId: publishedEdgesByPublicationcode?.[publicationcode]
.map(({ issuecode }) => issuecode)
.includes(issuecode)
? 1
: 0,
publicationcode,
issuecode,
creationDate: new Date(),
sprites: [],
points: 0,
slug: "",
timestamp: new Date().getTime(),
}),
) || [],
}),
{},
),
>((acc, publicationcode) => {
acc[publicationcode] =
issuecodesByPublicationcode.value[publicationcode]?.map((issuecode) => ({
id: 0,
edgeId: publishedEdgesByPublicationcode?.[publicationcode]
.map(({ issuecode }) => issuecode)
.includes(issuecode)
? 1
: 0,
publicationcode,
issuecode,
creationDate: new Date(),
sprites: [],
points: 0,
slug: "",
timestamp: new Date().getTime(),
})) || [];
return acc;
}, {}),
);
(async () => {
Expand Down
12 changes: 6 additions & 6 deletions apps/web/src/pages/print/classic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ watch(
);
ownedIssuecodes = Object.entries(
issuecodesByPublicationcode.value,
).reduce(
(acc, [publicationcode, indexedIssuecodes]) => ({
...acc,
[publicationcode]: indexedIssuecodes
).reduce<Record<string, string>>(
(acc, [publicationcode, indexedIssuecodes]) => {
acc[publicationcode] = indexedIssuecodes
.filter((indexedIssuecode) =>
collectionWithPublicationcodes[publicationcode].some(
({ issuecode }) => issuecode === indexedIssuecode,
),
)
.map((issuecode) => issuecodeDetails.value[issuecode].issuenumber)
.join(", "),
}),
.join(", ");
return acc;
},
{},
);
}
Expand Down
43 changes: 20 additions & 23 deletions apps/web/src/pages/stats/publications.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ const totalPerPublicationGroupSmallCounts: {
(publicationcode) =>
!smallCountPublications.includes(publicationcode),
)
.reduce(
(acc, publicationcode) => ({
...acc,
[publicationcode]: totalPerPublication.value![publicationcode],
}),
{},
),
.reduce<Record<string, number>>((acc, publicationcode) => {
acc[publicationcode] = totalPerPublication.value![publicationcode];
return acc;
}, {}),
...(!smallCountPublications.length
? {}
: {
Expand All @@ -66,22 +63,22 @@ const totalPerPublicationGroupSmallCounts: {
}) ||
{},
);
const labels = $computed(
() =>
hasPublicationNames &&
Object.entries(totalPerPublicationGroupSmallCounts)
.sort(sortByCount)
.reduce<string[]>(
(acc, [publicationcode]) => [
...acc,
publicationNames.value[publicationcode] ||
`${$t("Autres")} (${smallCountPublications!.length} ${$t(
"Publications",
).toLowerCase()})`,
],
[],
),
);
const labels = $computed(() => {
if (!hasPublicationNames) return false;
return Object.entries(totalPerPublicationGroupSmallCounts)
.sort(sortByCount)
.reduce<string[]>((acc, [publicationcode]) => {
acc.push(
publicationNames.value[publicationcode] ||
`${$t("Autres")} (${smallCountPublications!.length} ${$t(
"Publications",
).toLowerCase()})`,
);
return acc;
}, []);
});
const values = $computed(() =>
Object.values(totalPerPublicationGroupSmallCounts).sort((count1, count2) =>
Math.sign(count1 - count2),
Expand Down
Loading

0 comments on commit 9e6cd5d

Please sign in to comment.