fix bug in parser

This commit is contained in:
Jason Wall 2020-08-06 20:46:53 -04:00
parent cdb54947cb
commit ed30f45bde
2 changed files with 46 additions and 39 deletions

View File

@ -1,39 +1,41 @@
import { BibleReference } from './bible-reference'; import { BibleReference } from './bible-reference';
describe('Reference', () => { describe('Reference', () => {
// it('Should parse the reference: Gen 1:1', () => { it('Should parse the reference: Gen 1:1', () => {
// const ref = new BibleReference('Gen 1:1').toString(); const ref = new BibleReference('Gen 1:1').toString();
// expect(ref).toBe('Genesis 1:1'); expect(ref).toBe('Genesis 1:1');
// }); });
// it('Should parse the reference: Gen 1:1-11', () => { it('Should parse the reference: Gen 1:1-11', () => {
// const ref = new BibleReference('Gen 1:1-11').toString(); const ref = new BibleReference('Gen 1:1-11').toString();
// expect(ref).toBe('Genesis 1:1 - 11'); expect(ref).toBe('Genesis 1:1 - 11');
// }); });
// it('Should parse the reference: Gen 1-2', () => { it('Should parse the reference: Gen 1-2', () => {
// const ref = new BibleReference('Gen 1-2').toString(); const ref = new BibleReference('Gen 1-2').toString();
// expect(ref).toBe('Genesis 1:1 - 2:*'); expect(ref).toBe('Genesis 1:1 - 2:*');
// }); });
// it('Should parse the reference: John 3:16', () => { it('Should parse the reference: John 3:16', () => {
// const ref = new BibleReference('John 3:16').toString(); const ref = new BibleReference('John 3:16').toString();
// expect(ref).toBe('John 3:16'); expect(ref).toBe('John 3:16');
// }); });
// it('Should parse the reference: John 3-6', () => { it('Should parse the reference: John 3-6', () => {
// const ref = new BibleReference('Jn 3-6').toString(); const ref = new BibleReference('Jn 3-6').toString();
// expect(ref).toBe('John 3:1 - 6:*'); expect(ref).toBe('John 3:1 - 6:*');
// }); });
// it('Should parse the reference: 1 Corinthians 3:5-6:5', () => { it('Should parse the reference: 1 Corinthians 3:5-6:5', () => {
// const ref = new BibleReference('1 Corinthians 3:5-6:5').toString(); const ref = new BibleReference('1 Corinthians 3:5-6:5').toString();
// expect(ref).toBe('1 Corinthians 3:5 - 6:5'); expect(ref).toBe('1 Corinthians 3:5 - 6:5');
// }); });
// it('Should parse the reference: 2 Samuel 5:5-6:5', () => { it('Should parse the reference: 2 Samuel 5:5-6:5', () => {
// expect(new BibleReference('II sam 5:5-6:5').toString()).toBe('2 Samuel 5:5 - 6:5'); expect(new BibleReference('II sam 5:5-6:5').toString()).toBe(
// }); '2 Samuel 5:5 - 6:5'
);
});
const booknames = [ const booknames = [
{ abbr: 'gen', actual: 'Genesis' }, { abbr: 'gen', actual: 'Genesis' },
@ -274,6 +276,7 @@ describe('Reference', () => {
expect( expect(
new BibleReference(bk.abbr + ' ' + i + '-' + (i + 1)).toString() new BibleReference(bk.abbr + ' ' + i + '-' + (i + 1)).toString()
).toBe(bk.actual + ' ' + i + ':1 - ' + (i + 1) + ':*'); ).toBe(bk.actual + ' ' + i + ':1 - ' + (i + 1) + ':*');
expect( expect(
new BibleReference( new BibleReference(
bk.abbr + ' ' + i + ':3-' + (i + 1) + ':6' bk.abbr + ' ' + i + ':3-' + (i + 1) + ':6'
@ -281,17 +284,18 @@ describe('Reference', () => {
).toBe(bk.actual + ' ' + i + ':3 - ' + (i + 1) + ':6'); ).toBe(bk.actual + ' ' + i + ':3 - ' + (i + 1) + ':6');
} }
// expect(new BibleReference(bk.abbr + ' 1:4-2:5').toString()).toBe( expect(new BibleReference(bk.abbr + ' 1:4-2:5').toString()).toBe(
// bk.actual + ' 1:4 - 2:5' bk.actual + ' 1:4 - 2:5'
// ); );
// expect(new BibleReference(bk.abbr + ' 1:1 - 5').toString()).toBe( expect(new BibleReference(bk.abbr + ' 1:1 - 5').toString()).toBe(
// bk.actual + ' 1:1 - 5' bk.actual + ' 1:1 - 5'
// ); );
// expect(new BibleReference(bk.abbr + ' 1:4 - 5').toString()).toBe( expect(new BibleReference(bk.abbr + ' 1:4 - 5').toString()).toBe(
// bk.actual + ' 1:4 - 5' bk.actual + ' 1:4 - 5'
// ); );
}); });
} }
const refs = [ const refs = [
{ src: '2 sam 3:4-6:8', actual: '2 Samuel 3:4 - 6:8' }, { src: '2 sam 3:4-6:8', actual: '2 Samuel 3:4 - 6:8' },
// { "src": "gen 50 - ex 2", "actual": "Genesis 50:1 - Exodus 2:*" }, // { "src": "gen 50 - ex 2", "actual": "Genesis 50:1 - Exodus 2:*" },

View File

@ -1910,12 +1910,13 @@ export class BibleReference {
} }
private parseReference() { private parseReference() {
this.parseNumberReference(); this.parseKeyReference();
if (this.ref.length === 0) { if (this.ref.length === 0) {
return; return;
} }
this.parseBook(false); this.parseBook(false);
this.parseFirstNum(false); this.parseFirstNum(false);
const foundFirstVerse = this.ref.search(/:.*-/) !== -1; const foundFirstVerse = this.ref.search(/:.*-/) !== -1;
this.maybeParseSecondNum(false); this.maybeParseSecondNum(false);
this.maybeParseRangeSep(); this.maybeParseRangeSep();
@ -1926,10 +1927,12 @@ export class BibleReference {
this.maybeParseSecondNum(true); this.maybeParseSecondNum(true);
} }
private parseNumberReference() { // we're trying to parse references in the form of 41:2:3
private parseKeyReference() {
this.ref = this.ref.toLowerCase().trim(); this.ref = this.ref.toLowerCase().trim();
const parts = this.ref.split(':'); const parts = this.ref.split(':');
if (parts.length === 3 && parts.every((i) => parseInt(i, 10) > 0)) { if (this.ref.match(/\d+:\d+:\d+/) !== null) {
const fbook = BibleReference.bookName(parseInt(parts[0], 10)); const fbook = BibleReference.bookName(parseInt(parts[0], 10));
this.Section.book = fbook; this.Section.book = fbook;