Skip to content

Commit

Permalink
Use optional chaining to simplify sortByDate (#94).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Jan 10, 2021
1 parent 8c3e8a2 commit 5eb3197
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
16 changes: 4 additions & 12 deletions defaults/layout/scripts/sort_by_date.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
<script context="module">
export const sortByDate = (items, order) => {
items.sort((a, b) => {
// Must have a field specifically named "date" to work.
// Feel free to extend to other custom named date fields.
if (a.hasOwnProperty("fields") && b.hasOwnProperty("fields")) {
if (a.fields.hasOwnProperty("date") && b.fields.hasOwnProperty("date")) {
let aDate = new Date(a.fields.date);
let bDate = new Date(b.fields.date);
if (order == "oldest") {
return aDate - bDate;
}
return bDate - aDate;
}
}
// Must have field named "date" in content source to work.
let dateA = new Date(a?.fields?.date);
let dateB = new Date(b?.fields?.date);
return (order == "oldest") ? (dateA - dateB) : (dateB - dateA);
});
return items;
};
Expand Down
16 changes: 4 additions & 12 deletions generated/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,18 +487,10 @@ node_modules`),
"/layout/scripts/sort_by_date.svelte": []byte(`<script context="module">
export const sortByDate = (items, order) => {
items.sort((a, b) => {
// Must have a field specifically named "date" to work.
// Feel free to extend to other custom named date fields.
if (a.hasOwnProperty("fields") && b.hasOwnProperty("fields")) {
if (a.fields.hasOwnProperty("date") && b.fields.hasOwnProperty("date")) {
let aDate = new Date(a.fields.date);
let bDate = new Date(b.fields.date);
if (order == "oldest") {
return aDate - bDate;
}
return bDate - aDate;
}
}
// Must have field named "date" in content source to work.
let dateA = new Date(a?.fields?.date);
let dateB = new Date(b?.fields?.date);
return (order == "oldest") ? (dateA - dateB) : (dateB - dateA);
});
return items;
};
Expand Down

0 comments on commit 5eb3197

Please sign in to comment.