Skip to content

Commit

Permalink
refactor(entityRoutes): extract getDefaultAliasIndex and areaToOption
Browse files Browse the repository at this point in the history
creator.js and publisher.js both had these, put them in entity.js
  • Loading branch information
naiveai committed Dec 28, 2017
1 parent a5e051d commit 2d5c8fd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
26 changes: 3 additions & 23 deletions src/server/routes/entity/creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,14 @@ router.get(
}
);

function getDefaultAliasIndex(aliases) {
const index = aliases.findIndex((alias) => alias.default);
return index > 0 ? index : 0;
}

function areaToOption(area) {
if (!area) {
return null;
}

const {id} = area;

return {
disambiguation: area.comment,
id,
text: area.name,
type: 'area'
};
}

function creatorToFormState(creator) {
const aliases = creator.aliasSet ?
creator.aliasSet.aliases.map(({language, ...rest}) => ({
language: language.id,
...rest
})) : [];

const defaultAliasIndex = getDefaultAliasIndex(aliases);
const defaultAliasIndex = entityRoutes.getDefaultAliasIndex(aliases);
const defaultAliasList = aliases.splice(defaultAliasIndex, 1);

const aliasEditor = {};
Expand Down Expand Up @@ -170,9 +150,9 @@ function creatorToFormState(creator) {
);

const creatorSection = {
beginArea: areaToOption(creator.beginArea),
beginArea: entityRoutes.areaToOption(creator.beginArea),
beginDate: creator.beginDate,
endArea: areaToOption(creator.endArea),
endArea: entityRoutes.areaToOption(creator.endArea),
endDate: creator.endDate,
ended: creator.ended,
gender: creator.gender && creator.gender.id,
Expand Down
20 changes: 20 additions & 0 deletions src/server/routes/entity/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,3 +944,23 @@ export function constructIdentifiers(identifierEditor) {
value
}));
}

export function getDefaultAliasIndex(aliases) {
const index = aliases.findIndex((alias) => alias.default);
return index > 0 ? index : 0;
}

export function areaToOption(area) {
if (!area) {
return null;
}

const {id} = area;

return {
disambiguation: area.comment,
id,
text: area.name,
type: 'area'
};
}
25 changes: 2 additions & 23 deletions src/server/routes/entity/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,35 +126,14 @@ router.get(
}
);


function getDefaultAliasIndex(aliases) {
const index = aliases.findIndex((alias) => alias.default);
return index > 0 ? index : 0;
}

function areaToOption(area) {
if (!area) {
return null;
}

const {id} = area;

return {
disambiguation: area.comment,
id,
text: area.name,
type: 'area'
};
}

function publisherToFormState(publisher) {
const aliases = publisher.aliasSet ?
publisher.aliasSet.aliases.map(({language, ...rest}) => ({
language: language.id,
...rest
})) : [];

const defaultAliasIndex = getDefaultAliasIndex(aliases);
const defaultAliasIndex = entityRoutes.getDefaultAliasIndex(aliases);
const defaultAliasList = aliases.splice(defaultAliasIndex, 1);

const aliasEditor = {};
Expand Down Expand Up @@ -186,7 +165,7 @@ function publisherToFormState(publisher) {
);

const publisherSection = {
area: areaToOption(publisher.area),
area: entityRoutes.areaToOption(publisher.area),
beginDate: publisher.beginDate,
endDate: publisher.endDate,
ended: publisher.ended,
Expand Down

0 comments on commit 2d5c8fd

Please sign in to comment.