Skip to content

Commit

Permalink
Added url helper to listing service to make url slugs from listing
Browse files Browse the repository at this point in the history
  • Loading branch information
bencpeters committed Apr 28, 2020
1 parent 3343bd4 commit 033a34e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
28 changes: 28 additions & 0 deletions services/listings/__tests__/lib/url_helper.test.ts
@@ -0,0 +1,28 @@
import { formatUrlSlug, listingUrlSlug } from "../../src/lib/url_helper"
import { Listing } from "@bloom-housing/core"
import triton from "../../listings/triton.json"

describe("formatUrlSlug", () => {
test("reformats strings properly", () => {
expect(formatUrlSlug("snake_case")).toEqual("snake_case")
expect(formatUrlSlug("SnakeCase")).toEqual("snake_case")
expect(formatUrlSlug("Mix of spaces_and-hyphens")).toEqual("mix_of_spaces_and_hyphens")
expect(formatUrlSlug("Lots@of&weird spaces&^&!@^*&AND OTHER_CHARS")).toEqual(
"lots_of_weird_spaces_and_other_chars"
)
})

test("with an empty string", () => {
expect(formatUrlSlug("")).toEqual("")
})
})

describe("listingUrlSlug", () => {
// Force cast to listing - should we add a dependency to `listingsLoader` instead?
const listing = (triton as unknown) as Listing

test("Generates a URL slug for a Listing", () => {
const slug = listingUrlSlug(listing)
expect(slug).toEqual("the_triton_55_triton_park_lane_foster_city_ca")
})
})
2 changes: 2 additions & 0 deletions services/listings/src/index.ts
Expand Up @@ -5,6 +5,7 @@ import jp from "jsonpath"
import { Listing } from "@bloom-housing/core"
import listingsLoader from "./lib/listings_loader"
import { transformUnits } from "./lib/unit_transformations"
import { listingUrlSlug } from "./lib/url_helper"
import { amiCharts } from "./lib/ami_charts"

dotenv.config({ path: ".env" })
Expand All @@ -29,6 +30,7 @@ app.use(async ctx => {
// Transform all the listings
listings.forEach(listing => {
listing.unitsSummarized = transformUnits(listing.units, amiCharts)
listing.urlSlug = listingUrlSlug(listing)
})

const data = {
Expand Down
30 changes: 30 additions & 0 deletions services/listings/src/lib/url_helper.ts
@@ -0,0 +1,30 @@
/**
* Formats the input string as a URL slug.
* This includes the following transformations:
* - All lowercase
* - Remove special characters
* - snake_case
* @param input
*/
import { Listing } from "@bloom-housing/core"

export const formatUrlSlug = (input: string): string => {
return (
(
(input || "")
// Divide into words based on upper case letters followed by lower case letters
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]+|[0-9]+/g) || []
)

.join("_")
.toLowerCase()
)
}

export const listingUrlSlug = (listing: Listing): string => {
const {
name,
buildingAddress: { city, street, state }
} = listing
return formatUrlSlug([name, street, city, state].join(" "))
}
1 change: 1 addition & 0 deletions shared/core/src/listings.ts
Expand Up @@ -58,6 +58,7 @@ export interface Listing {
unitsAvailable: number
unitAmenities: string
unitsSummarized?: UnitsSummarized
urlSlug?: string
waitlistCurrentSize: number
waitlistMaxSize: number
yearBuilt: number
Expand Down

0 comments on commit 033a34e

Please sign in to comment.