diff --git a/README.md b/README.md index a17e6eb..3bde866 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## Installation ```bash -npm install bible-ref-parser +npm install @idrw/bible-ref-parser ``` ## Usage diff --git a/package.json b/package.json index 5cc6382..02ea20f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@idrw/bible-ref-parser", - "version": "1.0.0", + "version": "1.0.1", "main": "./build/index.js", "type": "module", "scripts": { diff --git a/src/index.ts b/src/index.ts index ec03ebc..617c57b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -72,24 +72,42 @@ function parseBook(query: string): BookData | null { function replaceRomanNumbers(query: string) { - let romanNumber = 0 - let idx + const edgeCases = [ + { words: ["isamuel", "isam", "ism"], sliceAt: 1 }, + { words: ["isaiah", "isa", "is"], sliceAt: 0 } + ] - for (let i = 0; i < query.length; i++) { - if (query[i] === "I") { - romanNumber += 1 - continue + for (const edgeCase of edgeCases) { + for (const word of edgeCase.words) { + if (query.toLowerCase().startsWith(word)) { + const ordinal = edgeCase.sliceAt === 0 ? "" : "1 " + return `${ordinal}${query.slice(edgeCase.sliceAt)}` + } } + } + + let romanNumber = "" + let idx - idx = (query[i] === " ") ? (i + 1) : i - break + const ordinals = [ + { text: "iii", properOrd: "3", endPosition: 3 }, + { text: "ii", properOrd: "2", endPosition: 2 }, + { text: "i", properOrd: "1", endPosition: 1 }, + ] + + for (const ord of ordinals) { + if (query.toLowerCase().startsWith(ord.text)) { + idx = ord.endPosition + romanNumber += ord.properOrd + break + } } - if (romanNumber === 0) { + if (romanNumber === "") { return query } - return `${romanNumber} ${query.slice(idx)}` + return `${romanNumber} ${query.slice(idx).trim()}` } diff --git a/test/app.test.ts b/test/app.test.ts index 7c7ae3e..2dd5a88 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -57,7 +57,10 @@ test('replaceRomanNumbers() should replace "I" with 1, "II" with 2 and "III" wit { input: "II Kings", expected: "2 Kings" }, { input: "III John", expected: "3 John" }, - { input: "IIJohn", expected: "2 John" } + { input: "IIJohn", expected: "2 John" }, + { input: "Isaiah", expected: "Isaiah" }, + { input: "Isamuel", expected: "1 samuel" }, + { input: "isAmUeL", expected: "1 sAmUeL" } ] for (const { input, expected } of cases) { @@ -72,6 +75,9 @@ test('replaceRomanNumbers() should return the original string if no book number { input: "Timothy", expected: "Timothy" }, { input: "John", expected: "John" }, { input: "Thessalonians", expected: "Thessalonians" }, + { input: "isaiah", expected: "isaiah" }, + { input: "isa", expected: "isa" }, + { input: "is", expected: "is" }, ] for (const { input, expected } of cases) { @@ -276,6 +282,11 @@ test('parseBook() should return the expected bookData', async (t) => { { input: "1st John", expected: { name: "1 John", references: [] } }, { input: "first JOHN", expected: { name: "1 John", references: [] } }, { input: "2ndtim", expected: { name: "2 Timothy", references: [] } }, + { input: "isa", expected: { name: "Isaiah", references: [] } }, + { input: "isam", expected: { name: "1 Samuel", references: [] } }, + { input: "Ism", expected: { name: "1 Samuel", references: [] } }, + { input: "IIsm", expected: { name: "2 Samuel", references: [] } }, + { input: "iSaIAH", expected: { name: "Isaiah", references: [] } }, ] for (const { input, expected } of cases) {