-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3091 from ONSdigital/EAR-2064-importing-double-space
EAR 2064 importing double space
- Loading branch information
Showing
19 changed files
with
1,668 additions
and
726 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
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,36 @@ | ||
const removeExtraSpaces = (inputData) => { | ||
// Does not remove extra spaces from inputData if inputData is null or undefined | ||
if (inputData !== null && inputData !== undefined) { | ||
// If inputData is a string, remove extra spaces | ||
if (typeof inputData === "string") { | ||
// If inputData starts and end with HTML tags, remove extra spaces from the content inside the tags, including leading and trailing spaces | ||
if (/^<[^>]+>.*<\/[^>]+>$/.test(inputData)) { | ||
const startTags = inputData.substring(0, inputData.indexOf(">") + 1); | ||
const endTags = inputData.substring(inputData.lastIndexOf("<")); | ||
const inputDataContent = inputData.substring( | ||
startTags.length, | ||
inputData.lastIndexOf("<") | ||
); | ||
inputData = `${startTags}${inputDataContent | ||
.replace(/\s+/g, " ") | ||
.trim()}${endTags}`; | ||
} else { | ||
inputData = inputData.replace(/\s+/g, " ").trim(); | ||
} | ||
} | ||
// If inputData is an array, recursively call removeExtraSpaces to remove extra spaces from each of its items | ||
else if (Array.isArray(inputData)) { | ||
inputData = inputData.map((item) => removeExtraSpaces(item)); | ||
} | ||
// If inputData is an object, loop through each of its keys and recursively call removeExtraSpaces to remove extra spaces from each of its values | ||
else if (typeof inputData === "object") { | ||
Object.keys(inputData).forEach((key) => { | ||
inputData[key] = removeExtraSpaces(inputData[key]); | ||
}); | ||
} | ||
} | ||
|
||
return inputData; | ||
}; | ||
|
||
module.exports = removeExtraSpaces; |
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,107 @@ | ||
const removeExtraSpaces = require("./removeExtraSpaces"); | ||
|
||
describe("removeExtraSpaces", () => { | ||
it("should remove extra spaces from string values", () => { | ||
const sectionTitleWithExtraSpaces = " Section 1 "; | ||
|
||
const sectionTitleWithoutExtraSpaces = removeExtraSpaces( | ||
sectionTitleWithExtraSpaces | ||
); | ||
|
||
expect(sectionTitleWithoutExtraSpaces).toEqual("Section 1"); | ||
}); | ||
|
||
it("should remove extra spaces from string values with HTML tags", () => { | ||
const sectionTitleWithExtraSpaces = "<p> Section 1 </p>"; | ||
|
||
const sectionTitleWithoutExtraSpaces = removeExtraSpaces( | ||
sectionTitleWithExtraSpaces | ||
); | ||
|
||
expect(sectionTitleWithoutExtraSpaces).toEqual("<p>Section 1</p>"); | ||
}); | ||
|
||
it("should remove extra spaces from arrays of strings", () => { | ||
const answerLabelsWithExtraSpaces = [ | ||
"Answer 1 ", | ||
" Answer 2", | ||
" Answer 3 ", | ||
]; | ||
|
||
const answerLabelsWithoutExtraSpaces = removeExtraSpaces( | ||
answerLabelsWithExtraSpaces | ||
); | ||
|
||
expect(answerLabelsWithoutExtraSpaces).toEqual([ | ||
"Answer 1", | ||
"Answer 2", | ||
"Answer 3", | ||
]); | ||
}); | ||
|
||
it("should remove extra spaces from all string values in object", () => { | ||
const sectionWithExtraSpaces = { | ||
id: "section-with-extra-spaces-1", | ||
title: " Section with extra spaces 1 ", | ||
folders: [ | ||
{ | ||
id: "folder-with-extra-spaces-1", | ||
alias: " Folder with extra spaces 1", | ||
pages: [ | ||
{ | ||
id: "page-with-extra-spaces-1", | ||
title: "Page with extra spaces 1 ", | ||
answers: [ | ||
{ | ||
id: "answer-with-extra-spaces-1", | ||
label: " Answer with extra spaces 1", | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const sectionWithoutExtraSpaces = removeExtraSpaces(sectionWithExtraSpaces); | ||
|
||
expect(sectionWithoutExtraSpaces).toMatchObject({ | ||
id: "section-with-extra-spaces-1", | ||
title: "Section with extra spaces 1", | ||
folders: [ | ||
{ | ||
id: "folder-with-extra-spaces-1", | ||
alias: "Folder with extra spaces 1", | ||
pages: [ | ||
{ | ||
id: "page-with-extra-spaces-1", | ||
title: "Page with extra spaces 1", | ||
answers: [ | ||
{ | ||
id: "answer-with-extra-spaces-1", | ||
label: "Answer with extra spaces 1", | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it("should remove extra spaces from arrays of strings in objects", () => { | ||
const page = { | ||
id: "page-1", | ||
title: " Page 1", | ||
answerLabels: ["Answer 1", "Answer 2 ", "Answer 3"], // Mock value to test array of strings in an object - `answerLabels` is not an attribute of `page` in Author | ||
}; | ||
|
||
const pageWithoutExtraSpaces = removeExtraSpaces(page); | ||
|
||
expect(pageWithoutExtraSpaces).toMatchObject({ | ||
id: "page-1", | ||
title: "Page 1", | ||
answerLabels: ["Answer 1", "Answer 2", "Answer 3"], | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.