From cb611b278be33a98d6d9ee3bc545f0e6d9162b7e Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 28 Dec 2023 14:12:55 +0000 Subject: [PATCH] avoid using the `/v` modifier it's not supported by many browsers, also the vue/vite compiler refuses to allow it --- src/internal/core/index.ts | 12 ++++++++++++ src/internal/parser.ts | 2 +- test/core.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/core.ts diff --git a/src/internal/core/index.ts b/src/internal/core/index.ts index 2241d50..0ee19f4 100644 --- a/src/internal/core/index.ts +++ b/src/internal/core/index.ts @@ -181,6 +181,18 @@ export function notMatch(parser: Parser): Parser { }); } +export function difference(parserIncluded: Parser, parserExcluded: Parser): Parser { + return new Parser((input, index, state) => { + const exclude = parserExcluded.handler(input, index, state); + + if (exclude.success) { + return failure(); + } + + return parserIncluded.handler(input, index, state); + }); +} + export const cr = str('\r'); export const lf = str('\n'); export const crlf = str('\r\n'); diff --git a/src/internal/parser.ts b/src/internal/parser.ts index 36f87d6..f50978d 100644 --- a/src/internal/parser.ts +++ b/src/internal/parser.ts @@ -12,7 +12,7 @@ import twemojiRegex from '@twemoji/parser/dist/lib/regex'; type ArgPair = { k: string, v: string | true }; type Args = Record; -const space = P.regexp(/[\s--[\n\r]]/v); +const space = P.difference(P.regexp(/\s/), P.newline); const alphaAndNum = P.regexp(/\p{Letter}|\p{Number}/iu); const newLine = P.alt([P.crlf, P.cr, P.lf]); diff --git a/test/core.ts b/test/core.ts new file mode 100644 index 0000000..a4ad393 --- /dev/null +++ b/test/core.ts @@ -0,0 +1,30 @@ +import assert from 'assert'; +import * as P from '../src/internal/core'; + +describe('core', () => { + describe('difference', () => { + test('basic', () => { + const parser = P.difference(P.regexp(/\p{Letter}/u), P.str('x')); + + let result = parser.handler('x',0,{}) as P.Success; + assert.deepStrictEqual(result,P.failure()); + + result = parser.handler('a',0,{}) as P.Success; + assert.deepStrictEqual(result,P.success(1,'a')); + }); + + test('horizontal whitespace', () => { + const parser = P.difference(P.regexp(/\s/u), P.newline); + + let result = parser.handler('\n',0,{}) as P.Success; + assert.deepStrictEqual(result,P.failure()); + + result = parser.handler(' ',0,{}) as P.Success; + assert.deepStrictEqual(result,P.success(1,' ')); + + result = parser.handler('\t',0,{}) as P.Success; + assert.deepStrictEqual(result,P.success(1,'\t')); + }); + + }); +});