-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update
formatRawProjectTypes
to reference schema instead of …
…static database table (#445)
- Loading branch information
1 parent
7666a78
commit eb3b635
Showing
8 changed files
with
113 additions
and
85 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./digitalPlanningSchema"; | ||
export * from "./encryption"; | ||
export * from "./govPayMetadata"; | ||
export * from "./projectTypes"; |
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,47 @@ | ||
import { | ||
formatHumanReadableProjectTypes, | ||
formatRawProjectTypes, | ||
} from "./projectTypes"; | ||
|
||
describe("formatRawProjectTypes()", () => { | ||
it("correctly displays the schema descriptions for a list of valid raw project types", () => { | ||
const result = formatRawProjectTypes([ | ||
"alter.boundary.add.gate", | ||
"alter.equipment.charging", | ||
"extend.outbuilding.swimmingPool", | ||
]); | ||
expect(result).toEqual( | ||
"Add a new gate, install a car charging point, and add an outbuilding - swimming pool", | ||
); | ||
}); | ||
|
||
it("returns 'Unknown' if none of the raw project types are supported by the schema", () => { | ||
const result = formatRawProjectTypes(["alter.typo", "new.typo"]); | ||
expect(result).toEqual("Unknown"); | ||
}); | ||
|
||
it("skips invalid raw project types and correctly shows descriptions of valid ones", () => { | ||
const result = formatRawProjectTypes([ | ||
"alter.typo", | ||
"alter.boundary.add.gate", | ||
"alter.equipment.charging", | ||
]); | ||
expect(result).toEqual("Add a new gate and install a car charging point"); | ||
}); | ||
}); | ||
|
||
describe("formatHumanReadableProjectTypes()", () => { | ||
it("returns a correctly formatted string", () => { | ||
const result = formatHumanReadableProjectTypes([ | ||
"first", | ||
"second", | ||
"third", | ||
]); | ||
expect(result).toEqual("First, second, and third"); | ||
}); | ||
|
||
it("returns an empty string if no raw project types are provided", () => { | ||
const result = formatHumanReadableProjectTypes([]); | ||
expect(result).toEqual(""); | ||
}); | ||
}); |
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,31 @@ | ||
import "@formatjs/intl-listformat/locale-data/en"; | ||
import "@formatjs/intl-listformat/polyfill"; | ||
|
||
import { getValidSchemaDictionary } from "./digitalPlanningSchema"; | ||
|
||
export function formatRawProjectTypes(rawProjectTypes: string[]): string { | ||
const schemaProjectTypes = getValidSchemaDictionary("ProjectType"); | ||
if (schemaProjectTypes) { | ||
const matchingDescriptions = Object.entries(schemaProjectTypes) | ||
.filter(([key, _description]) => rawProjectTypes.includes(key)) | ||
.map(([_key, description]) => description.toLocaleLowerCase()); | ||
if (matchingDescriptions.length === 0) { | ||
return "Unknown"; | ||
} | ||
return formatHumanReadableProjectTypes(matchingDescriptions); | ||
} else { | ||
return "Unknown"; | ||
} | ||
} | ||
|
||
export function formatHumanReadableProjectTypes( | ||
projectTypes: string[], | ||
): string { | ||
// Join in readable format - en-US ensures we use Oxford commas | ||
const formatter = new Intl.ListFormat("en-US", { type: "conjunction" }); | ||
const joinedList = formatter.format(projectTypes); | ||
// Convert first character to uppercase | ||
const formattedProjectTypes = | ||
joinedList.charAt(0).toUpperCase() + joinedList.slice(1); | ||
return formattedProjectTypes; | ||
} |