From c37927e53c7fc2f9ba4fafcefcd67bde99949922 Mon Sep 17 00:00:00 2001 From: Malachi Soord Date: Fri, 29 Dec 2023 22:40:00 +0100 Subject: [PATCH] Add replaceSmartQuotes function (#324) --- src/functions.ts | 11 +++++++ .../functions/replace-smart-quotes.json | 32 +++++++++++++++++++ test/functions.spec.ts | 2 ++ 3 files changed, 45 insertions(+) create mode 100644 test/fixtures/functions/replace-smart-quotes.json diff --git a/src/functions.ts b/src/functions.ts index f12d5af1..bcd9f184 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -217,3 +217,14 @@ export function youtube(text: string): string { ...TRIM_SYMBOLS_FILTER_RULES, ]); } + +/** + * Replace smart quotes with regular quotes. + * + * @param text String to be filtered + * + * @return Filtered string + */ +export function replaceSmartQuotes(text: string): string { + return text.replace(/[\u2018\u2019]/g, "'").replace(/[\u201c\u201d]/g, '"'); +} diff --git a/test/fixtures/functions/replace-smart-quotes.json b/test/fixtures/functions/replace-smart-quotes.json new file mode 100644 index 00000000..7ed2f3e9 --- /dev/null +++ b/test/fixtures/functions/replace-smart-quotes.json @@ -0,0 +1,32 @@ +[ + { + "description": "should do nothing with clean string", + "funcParameter": "Track field", + "expectedValue": "Track field" + }, + { + "description": "should remove a double left single quotation space", + "funcParameter": "Track \u201cfield", + "expectedValue": "Track \"field" + }, + { + "description": "should remove a double left and double right quotation space", + "funcParameter": "Track \u201cfield\u201d", + "expectedValue": "Track \"field\"" + }, + { + "description": "should remove a single left single quotation space", + "funcParameter": "Track \u2018field", + "expectedValue": "Track 'field" + }, + { + "description": "should remove a single left and single right quotation space", + "funcParameter": "Track \u2018field\u2019", + "expectedValue": "Track 'field'" + }, + { + "description": "should remove a single left and double right quotation space", + "funcParameter": "Track \u2018field\u201d", + "expectedValue": "Track 'field\"" + } +] diff --git a/test/functions.spec.ts b/test/functions.spec.ts index b08a54b4..27b42cff 100644 --- a/test/functions.spec.ts +++ b/test/functions.spec.ts @@ -21,6 +21,7 @@ import { removeFeature, youtube, fixVariousArtists, + replaceSmartQuotes, } from '../src'; const functionsToTest = { @@ -38,6 +39,7 @@ const functionsToTest = { 'remove-version': removeVersion, 'remove-parody': removeParody, 'remove-feature': removeFeature, + 'replace-smart-quotes': replaceSmartQuotes, youtube: youtube, };