Skip to content

Commit

Permalink
chore: move regex to composable #442
Browse files Browse the repository at this point in the history
  • Loading branch information
wazolab committed Dec 16, 2024
1 parent b280056 commit c9a1c8a
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
6 changes: 6 additions & 0 deletions composables/useIdsResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const regexForCategoryIds = /^(?:(?<cartocode>cartocode:\w{2})|(?<reference>ref:[\w-]+:\w+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
export const regexForPOIIds = /^(?:cartocode:\w{2}|ref:[\w-]+:\w+|osm:[nwr]\d+|\d+)$/

export function useIdsResolver() {

}
4 changes: 2 additions & 2 deletions pages/embedded.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Embedded from '~/components/Home/Embedded.vue'
import type { ApiPoi } from '~/lib/apiPois'
import { siteStore as useSiteStore } from '~/stores/site'
import { mapStore as useMapStore } from '~/stores/map'
import { regexForCategoryIds } from '~/composables/useIdsResolver'
//
// Composables
Expand All @@ -22,7 +23,6 @@ const { $trackingInit } = useNuxtApp()
const boundaryGeojson = ref<Polygon | MultiPolygon>()
const poiId = ref<string>()
const categoryIds = ref<number[]>()
const categoryIdsRegex = /^(?:(?<cartocode>cartocode:\w{2})|(?<reference>ref:[\w-]+:\w+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
//
// Hooks
Expand Down Expand Up @@ -53,7 +53,7 @@ if (boundary && typeof boundary === 'string' && settings.value!.polygons_extra)
// Get category IDs from URL
if (route.params.p1) {
const match = route.params.p1.toString().match(categoryIdsRegex)
const match = route.params.p1.toString().match(regexForCategoryIds)
if (!match || (!route.path.endsWith('/') && match.groups && (match.groups.cartocode || match.groups.reference || match.groups.osm)))
throw createError({ statusCode: 400, message: `No match for category ID: ${route.params.p1}` })
Expand Down
4 changes: 2 additions & 2 deletions pages/embedded/[p1]/[poiId].vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script setup lang="ts">
import { regexForCategoryIds, regexForPOIIds } from '~/composables/useIdsResolver'
//
// Validators
//
definePageMeta({
validate: ({ params }) => {
const regexForCategoryIds = /^(?:(?<cartocode>cartocode:\w{2})|(?<reference>ref:[\w-]+:\w+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
const regexForPOIIds = /^(?:cartocode:\w{2}|ref:[\w-]+:\w+|osm:[nwr]\d+|\d+)$/
const match = params.p1.toString().match(regexForCategoryIds)
if (match?.groups && (match.groups.cartocode || match.groups.reference || match.groups.osm)) {
Expand Down
6 changes: 3 additions & 3 deletions pages/embedded/[p1]/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script setup lang="ts">
import { regexForCategoryIds } from '~/composables/useIdsResolver'
//
// Validators
//
definePageMeta({
validate: ({ params }) => {
const regexForIds = /^(?:cartocode:\w{2}|ref:[\w-]+:\w+|osm:[nwr]\d+|\d+(?:,\d+)*)$/
return regexForIds.test(params.p1.toString())
return regexForCategoryIds.test(params.p1.toString())
},
})
</script>
Expand Down
4 changes: 2 additions & 2 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Home from '~/components/Home/Home.vue'
import type { ApiPoi } from '~/lib/apiPois'
import { siteStore as useSiteStore } from '~/stores/site'
import { mapStore as useMapStore } from '~/stores/map'
import { regexForCategoryIds } from '~/composables/useIdsResolver'
//
// Composables
Expand All @@ -22,7 +23,6 @@ const { $trackingInit } = useNuxtApp()
const boundaryGeojson = ref<Polygon | MultiPolygon>()
const poiId = ref<string>()
const categoryIds = ref<number[]>()
const categoryIdsRegex = /^(?:(?<cartocode>cartocode:\w{2})|(?<reference>ref:[\w-]+:\w+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
//
// Hooks
Expand Down Expand Up @@ -53,7 +53,7 @@ if (boundary && typeof boundary === 'string' && settings.value!.polygons_extra)
// Get category IDs from URL
if (route.params.p1) {
const match = route.params.p1.toString().match(categoryIdsRegex)
const match = route.params.p1.toString().match(regexForCategoryIds)
if (!match || (!route.path.endsWith('/') && match.groups && (match.groups.cartocode || match.groups.reference || match.groups.osm)))
throw createError({ statusCode: 400, message: `No match for category ID: ${route.params.p1}` })
Expand Down
4 changes: 2 additions & 2 deletions pages/index/[p1]/[poiId].vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script setup lang="ts">
import { regexForCategoryIds, regexForPOIIds } from '~/composables/useIdsResolver'
//
// Validators
//
definePageMeta({
validate: ({ params }) => {
const regexForCategoryIds = /^(?:(?<cartocode>cartocode:\w{2})|(?<reference>ref:[\w-]+:\w+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
const regexForPOIIds = /^(?:cartocode:\w{2}|ref:[\w-]+:\w+|osm:[nwr]\d+|\d+)$/
const match = params.p1.toString().match(regexForCategoryIds)
if (match?.groups && (match.groups.cartocode || match.groups.reference || match.groups.osm)) {
Expand Down
6 changes: 3 additions & 3 deletions pages/index/[p1]/index.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script setup lang="ts">
import { regexForCategoryIds } from '~/composables/useIdsResolver'
//
// Validators
//
definePageMeta({
validate: ({ params }) => {
const regexForIds = /^(?:cartocode:\w{2}|ref:[\w-]+:\w+|osm:[nwr]\d+|\d+(?:,\d+)*)$/
return regexForIds.test(params.p1.toString())
return regexForCategoryIds.test(params.p1.toString())
},
})
</script>
Expand Down

0 comments on commit c9a1c8a

Please sign in to comment.