Skip to content

Commit d4cae31

Browse files
committed
fix(places): Strip BOM character from code field during import
BOM (U+FEFF) can appear at start of CSV files and pollute the first field value. This caused issues when using the code in subsequent API requests.
1 parent df81680 commit d4cae31

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

api/app/models/place.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,9 @@ const seedData = async (directory: string) => {
471471

472472
const typeRegex = /^.*\//;
473473
const parseOsgb = (val: string) => parseInt(val, 10) || "";
474+
const stripBom = (val: string) => val.replace(/^\uFEFF/, "");
474475
const transformExceptions = {
476+
code: stripBom,
475477
northings: parseOsgb,
476478
eastings: parseOsgb,
477479
min_eastings: parseOsgb,

test/place.unit.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ describe("Place Model", () => {
5858
const result = await query(q);
5959
assert.equal(result.rows[0].count, placesEntriesCount);
6060
});
61+
62+
it("strips BOM characters from code field", async () => {
63+
// BOM (U+FEFF) can appear at start of CSV files and pollute first field
64+
const q = `SELECT code FROM ${Place.relation.relation}`;
65+
const result = await query(q);
66+
result.rows.forEach((row) => {
67+
assert.isFalse(
68+
row.code.startsWith("\uFEFF"),
69+
`code "${row.code}" should not start with BOM character`
70+
);
71+
});
72+
});
6173
});
6274

6375
describe("#populateLocation", () => {

0 commit comments

Comments
 (0)