From dac760c0eaf833725121816891dd46af48906fc3 Mon Sep 17 00:00:00 2001 From: tim-hub Date: Sat, 14 Sep 2024 16:55:27 +1200 Subject: [PATCH] update reg to be international --- src/utils/reg.test.ts | 77 +++++++++++++++++++++++++++++++++++++++++++ src/utils/regs.ts | 10 ++++-- 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/utils/reg.test.ts diff --git a/src/utils/reg.test.ts b/src/utils/reg.test.ts new file mode 100644 index 0000000..55a8efb --- /dev/null +++ b/src/utils/reg.test.ts @@ -0,0 +1,77 @@ +import { BOOK_REG, MODAL_REG } from './regs' + +describe('test book name reg matching in different languages', () => { + test('should match book name in English', () => { + const bookName = 'John' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(true) + }) + + test('should match book name in Italian', () => { + const bookName = 'Giovanni' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(true) + }) + + test('should match book name in Japanese', () => { + const bookName = 'ヨハネ' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(true) + }) + + test('should match book name in Spanish', () => { + const bookName = 'Juan' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(true) + }) + + test('should match book name in Chinese', () => { + const bookName = '约翰福音' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(true) + }) + + test('should match book name in French', () => { + const bookName = 'Josué' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(true) + }) + + test('should not cover just one letter', () => { + const bookName = 'J' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(false) + }) +}) + +describe('test modal reg matching in different languages', () => { + test('should match modal in English', () => { + const modal = 'John 1:1' + const reg = new RegExp(MODAL_REG) + expect(reg.test(modal)).toBe(true) + }) + + test('should match modal in English Withou Space', () => { + const modal = 'John1:1' + const reg = new RegExp(MODAL_REG) + expect(reg.test(modal)).toBe(true) + }) + + test('should match book name in French', () => { + const bookName = 'Josué1:1' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(true) + }) + + test('should match book name in Chinese', () => { + const bookName = '约翰福音2:1' + const reg = new RegExp(BOOK_REG) + expect(reg.test(bookName)).toBe(true) + }) + + test('should not match if use letter for number', () => { + const modal = 'John a:1' + const reg = new RegExp(MODAL_REG) + expect(reg.test(modal)).toBe(false) + }) +}) diff --git a/src/utils/regs.ts b/src/utils/regs.ts index be1f2e5..b279f21 100644 --- a/src/utils/regs.ts +++ b/src/utils/regs.ts @@ -4,10 +4,14 @@ * John1:1 * 1John1:1 */ -export const MODAL_REG = - /([123])*\s*[A-Z\[\\\]^_`a-z]{2,}\s*\d{1,3}:\d{1,3}(-\d{1,3})*/ +// export const MODAL_REG = +// /([123])*\s*[A-Z\[\\\]^_`a-z]{2,}\s*\d{1,3}:\d{1,3}(-\d{1,3})*/ -export const BOOK_REG = /[123]*\s*[A-Z\[\\\]^_`a-z]{2,}/ +export const MODAL_REG = /([123])*\s*([\p{L}[\\\]^_`a-zA-Z]{2,100}|\p{Script=Han}{1,})\s*\d{1,3}:\d{1,3}(-\d{1,3})*/isu + +export const BOOK_REG = /([123])*\s*([\p{L}[\\\]^_`a-zA-Z]{2,100}|\p{Script=Han}{1,})/isu + +// export const BOOK_REG = /[123]*\s*[A-Z\[\\\]^_`a-z]{2,}/ /** * prefix of the trigger