Skip to content

Commit

Permalink
replace sorting and filtering logic with lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaaargZombies committed Aug 31, 2022
1 parent 921820f commit 4998b67
Show file tree
Hide file tree
Showing 3 changed files with 1,243 additions and 1,350 deletions.
26 changes: 9 additions & 17 deletions app/javascript/controllers/opening_times_controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Controller } from "@hotwired/stimulus";
import _ from "lodash";

const dayOrder = [
"Monday",
Expand All @@ -25,17 +26,10 @@ const openingHoursObj = (openSpec) => ({
});

const sortedOpeningHours = (openSpecArray) =>
[...openSpecArray]
.sort(
(a, b) =>
parseFloat(openingHoursObj(a).open.replace(":", ".")) -
parseFloat(openingHoursObj(b).open.replace(":", ".")),
)
.sort(
(a, b) =>
dayOrder.indexOf(openingHoursObj(a).day) -
dayOrder.indexOf(openingHoursObj(b).day),
);
_.orderBy(openSpecArray, [
(el) => dayOrder.indexOf(openingHoursObj(el).day),
(el) => parseFloat(openingHoursObj(el).open.replace(":", ".")),
]);

const nextDay = (day) => {
const index =
Expand All @@ -50,11 +44,6 @@ const openingHoursEnglish = (openSpec) => {
: `${day} from ${open} to ${close}`;
};

const removeTime = (openSpecArray, openSpec) =>
[...openSpecArray].filter(
(el) => JSON.stringify(el) !== JSON.stringify(openSpec),
);

const element = (type, content = "", classes = []) => {
const el = document.createElement(type);
el.innerHTML = content;
Expand Down Expand Up @@ -115,7 +104,10 @@ export default class extends Controller {
"btn-sm",
]);
btn.onclick = () => {
this.dataValue = removeTime(this.dataValue, openSpec);
// remove opening time
this.dataValue = [...this.dataValue].filter(
(el) => !_.isEqual(el, openSpec),
);
};
li.appendChild(btn);
return li;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"esbuild-vue": "^1.2.1",
"jquery": "^3.6.0",
"leaflet": "^1.6.0",
"lodash": "^4.17.21",
"minimist": "1.2.6",
"popper.js": "1.16.1",
"sass": "^1.50.1",
Expand Down
Loading

0 comments on commit 4998b67

Please sign in to comment.