Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
## Installation

```bash
npm install bible-ref-parser
npm install @idrw/bible-ref-parser
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@idrw/bible-ref-parser",
"version": "1.0.0",
"version": "1.0.1",
"main": "./build/index.js",
"type": "module",
"scripts": {
Expand Down
38 changes: 28 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`
}


Expand Down
13 changes: 12 additions & 1 deletion test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down