merge: avoid using the /v modifier (#5)

Reviewed-on: https://git.joinsharkey.org/Sharkey/sfm.js/pulls/5
This commit is contained in:
Marie 2023-12-28 19:30:15 +01:00
commit 58dc6c3b2e
3 changed files with 43 additions and 1 deletions

View file

@ -181,6 +181,18 @@ export function notMatch(parser: Parser<any>): Parser<null> {
}); });
} }
export function difference(parserIncluded: Parser<any>, parserExcluded: Parser<any>): Parser<string> {
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 cr = str('\r');
export const lf = str('\n'); export const lf = str('\n');
export const crlf = str('\r\n'); export const crlf = str('\r\n');

View file

@ -12,7 +12,7 @@ import twemojiRegex from '@twemoji/parser/dist/lib/regex';
type ArgPair = { k: string, v: string | true }; type ArgPair = { k: string, v: string | true };
type Args = Record<string, string | true>; type Args = Record<string, string | true>;
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 alphaAndNum = P.regexp(/\p{Letter}|\p{Number}/iu);
const newLine = P.alt([P.crlf, P.cr, P.lf]); const newLine = P.alt([P.crlf, P.cr, P.lf]);

30
test/core.ts Normal file
View file

@ -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<any>;
assert.deepStrictEqual(result,P.failure());
result = parser.handler('a',0,{}) as P.Success<any>;
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<any>;
assert.deepStrictEqual(result,P.failure());
result = parser.handler(' ',0,{}) as P.Success<any>;
assert.deepStrictEqual(result,P.success(1,' '));
result = parser.handler('\t',0,{}) as P.Success<any>;
assert.deepStrictEqual(result,P.success(1,'\t'));
});
});
});