Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added url helper to listing service to make url slugs from listing
- Loading branch information
1 parent
3343bd4
commit 033a34e
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(" ")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters