87 string splitting by separator, not whitespace

This commit is contained in:
survellow
2023-12-03 14:41:30 +01:00
parent a991e56115
commit ed99568fb1
7 changed files with 82 additions and 33 deletions

View File

@@ -27,7 +27,9 @@ func GetRooms(app *pocketbase.PocketBase) []string {
var roomArray []string var roomArray []string
for _, event := range events { for _, event := range events {
var room = strings.Split(event.Rooms, " ") var room = strings.FieldsFunc(event.Rooms, functions.IsSeparator(
[]rune{',', ' ', '\t', '\n', '\r', '\u00A0'},
))
//split functions room by space and add each room to array if it is not already in there //split functions room by space and add each room to array if it is not already in there
for _, r := range room { for _, r := range room {
var text = strings.TrimSpace(r) var text = strings.TrimSpace(r)

View File

@@ -8,10 +8,19 @@ import (
// check if string is empty or contains only whitespaces // check if string is empty or contains only whitespaces
func OnlyWhitespace(word string) bool { func OnlyWhitespace(word string) bool {
if len(strings.TrimSpace(word)) == 0 { return len(strings.TrimSpace(word)) == 0
return true }
// return function to check if rune is a separator
func IsSeparator(separator []rune) func(rune) bool {
return func(character rune) bool {
for _, sep := range separator {
if sep == character {
return true
}
}
return false
} }
return false
} }
func Contains(s []string, e string) bool { func Contains(s []string, e string) bool {

View File

@@ -1,6 +1,8 @@
package functions package functions
import "testing" import (
"testing"
)
func TestOnlyWhitespace(t *testing.T) { func TestOnlyWhitespace(t *testing.T) {
type args struct { type args struct {
@@ -52,3 +54,29 @@ func TestHashString(t *testing.T) {
}) })
} }
} }
func TestIsSeparator(t *testing.T) {
type args struct {
separator []rune
character rune
}
tests := []struct {
name string
args args
want bool
}{
{"empty separator", args{[]rune{}, 'a'}, false},
{"separator with one rune equal", args{[]rune{'a'}, 'a'}, true},
{"separator with one rune different", args{[]rune{'a'}, 'b'}, false},
{"separator with two runes equal", args{[]rune{'a', 'b'}, 'a'}, true},
{"separator with two runes equal second", args{[]rune{'a', 'b'}, 'b'}, true},
{"separator with two runes different", args{[]rune{'a', 'b'}, 'c'}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsSeparator(tt.args.separator)(tt.args.character); got != tt.want {
t.Errorf("IsSeparator()() = %v, want %v", got, tt.want)
}
})
}
}

View File

@@ -31,7 +31,7 @@ export async function fetchEventsByRoomAndDuration(
}) })
.catch((error) => { .catch((error) => {
console.log("Error fetching events: ", error); console.log("Error fetching events: ", error);
return null; return Promise.reject(error);
}); });
console.log("occupations: ", events); console.log("occupations: ", events);
return events; return events;

View File

@@ -20,6 +20,7 @@ type CalenderEvent = {
id: number; id: number;
start: string; start: string;
end: string; end: string;
showFree: boolean;
}; };
const currentDateFrom: Ref<string> = ref(""); const currentDateFrom: Ref<string> = ref("");
@@ -38,21 +39,27 @@ async function getOccupation() {
if (selectedRoom.value === "") { if (selectedRoom.value === "") {
return; return;
} }
const events = await fetchEventsByRoomAndDuration( fetchEventsByRoomAndDuration(
selectedRoom.value, selectedRoom.value,
currentDateFrom.value, currentDateFrom.value,
currentDateTo.value, currentDateTo.value,
); )
occupations.value = events.map((event, index) => { .then((events) => {
return { occupations.value = events.map((event, index) => {
id: index, return {
start: event.start.replace(/\s\+\d{4}\s\w+$/, "").replace(" ", "T"), id: index,
end: event.end.replace(/\s\+\d{4}\s\w+$/, "").replace(" ", "T"), start: event.start.replace(/\s\+\d{4}\s\w+$/, "").replace(" ", "T"),
}; end: event.end.replace(/\s\+\d{4}\s\w+$/, "").replace(" ", "T"),
}); showFree: event.name.toLowerCase().includes("zur freien verfügung"),
};
});
const calendar = fullCalendar.value?.getApi(); const calendar = fullCalendar.value?.getApi();
calendar?.refetchEvents(); calendar?.refetchEvents();
})
.catch((error) => {
console.log(error);
});
} }
import allLocales from "@fullcalendar/core/locales-all"; import allLocales from "@fullcalendar/core/locales-all";
@@ -124,20 +131,19 @@ const calendarOptions: ComputedRef<CalendarOptions> = computed(() => {
currentDateTo.value = endDate.toISOString().split("T")[0]; currentDateTo.value = endDate.toISOString().split("T")[0];
getOccupation(); getOccupation();
}, },
events: function (_info: any, successCallback: any, failureCallback: any) { events: function (_info: any, successCallback: any, _: any) {
if (occupations.value.length === 0) { successCallback(
failureCallback(new Error("no events")); occupations.value.map((event) => {
} else { return {
successCallback( id: event.id.toString(),
occupations.value.map((event) => { start: event.start,
return { end: event.end,
id: event.id.toString(), color: event.showFree ? "var(--green-800)" : "var(--primary-color)",
start: event.start, textColor: event.showFree ? "var(--green-50)" : "var(--primary-text-color)",
end: event.end, title: event.showFree ? t("roomFinderPage.available") : t("roomFinderPage.occupied"),
} as EventInput; } as EventInput;
}), }),
); );
}
}, },
}; };
}); });

View File

@@ -21,7 +21,9 @@
"headline": "Raumfinder", "headline": "Raumfinder",
"detail": "Bitte wähle einen Raum aus, um die Belegung einzusehen", "detail": "Bitte wähle einen Raum aus, um die Belegung einzusehen",
"dropDownSelect": "Bitte wähle einen Raum aus", "dropDownSelect": "Bitte wähle einen Raum aus",
"noRoomsAvailable": "Keine Räume verfügbar" "noRoomsAvailable": "Keine Räume verfügbar",
"available": "verfügbar",
"occupied": "belegt"
}, },
"moduleSelection": { "moduleSelection": {
"nextStep": "Weiter", "nextStep": "Weiter",

View File

@@ -21,7 +21,9 @@
"headline": "room finder", "headline": "room finder",
"detail": "Please select a room to view the occupancy", "detail": "Please select a room to view the occupancy",
"dropDownSelect": "Please select a room", "dropDownSelect": "Please select a room",
"noRoomsAvailable": "No rooms listed" "noRoomsAvailable": "No rooms listed",
"available": "available",
"occupied": "occupied"
}, },
"moduleSelection": { "moduleSelection": {
"nextStep": "next step", "nextStep": "next step",