Skip to content

Commit e57b322

Browse files
committed
feat: add orChar/from chain for charIn/charNotIn
1 parent b06c4e9 commit e57b322

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/core/inputs.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,27 @@ export type { Input }
1010

1111
const ESCAPE_REPLACE_RE = /[.*+?^${}()|[\]\\/]/g
1212

13-
/** This matches any character in the string provided */
14-
export function charIn<T extends string>(chars: T) {
15-
return createInput(`[${chars.replace(/[-\\^\]]/g, '\\$&')}]`) as Input<`[${EscapeChar<T>}]`>
13+
function createCharInput <T extends string>(raw: T) {
14+
const input = createInput(`[${raw}]`)
15+
const from = <From extends string, To extends string>(charFrom: From, charTo: To) => createCharInput(`${raw}${escapeCharInput(charFrom)}-${escapeCharInput(charTo)}`)
16+
const orChar = Object.assign((<T extends string>(chars: T) => createCharInput(`${raw}${escapeCharInput(chars)}`)), { from })
17+
return Object.assign(input, { orChar, from })
1618
}
1719

18-
/** This matches any character that is not in the string provided */
19-
export function charNotIn<T extends string>(chars: T) {
20-
return createInput(`[^${chars.replace(/[-\\^\]]/g, '\\$&')}]`) as Input<`[^${EscapeChar<T>}]`>
20+
function escapeCharInput <T extends string>(raw: T) {
21+
return raw.replace(/[-\\^\]]/g, '\\$&') as EscapeChar<T>
2122
}
2223

24+
/** This matches any character in the string provided */
25+
export const charIn = Object.assign(<T extends string>(chars: T) => {
26+
return createCharInput(escapeCharInput(chars))
27+
}, createCharInput(''))
28+
29+
/** This matches any character that is not in the string provided */
30+
export const charNotIn = Object.assign(<T extends string>(chars: T) => {
31+
return createCharInput(`^${escapeCharInput(chars)}`)
32+
}, createCharInput('^'))
33+
2334
/**
2435
* This takes a variable number of inputs and matches any of them
2536
* @example

test/inputs.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,41 @@ describe('inputs', () => {
3030
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[fo\\\\\\]\\\\\\^\\]/')
3131
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[fo\\]\\^]'>()
3232
})
33+
it('charIn.orChar', () => {
34+
const input = charIn('a').orChar('b')
35+
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[ab\\]/')
36+
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[ab]'>()
37+
})
38+
it('charIn.orChar.from', () => {
39+
const input = charIn('a').orChar.from('a', 'b')
40+
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[aa-b\\]/')
41+
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[aa-b]'>()
42+
})
43+
it('charIn.from', () => {
44+
const input = charIn.from('a', 'b')
45+
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[a-b\\]/')
46+
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[a-b]'>()
47+
})
3348
it('charNotIn', () => {
3449
const input = charNotIn('fo^-')
3550
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[\\^fo\\\\\\^\\\\-\\]/')
3651
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[^fo\\^\\-]'>()
3752
})
53+
it('charNotIn.orChar', () => {
54+
const input = charNotIn('a').orChar('b')
55+
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[\\^ab\\]/')
56+
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[^ab]'>()
57+
})
58+
it('charNotIn.orChar.from', () => {
59+
const input = charNotIn('a').orChar.from('a', 'b')
60+
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[\\^aa-b\\]/')
61+
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[^aa-b]'>()
62+
})
63+
it('charNotIn.from', () => {
64+
const input = charNotIn.from('a', 'b')
65+
expect(new RegExp(input as any)).toMatchInlineSnapshot('/\\[\\^a-b\\]/')
66+
expectTypeOf(extractRegExp(input)).toEqualTypeOf<'[^a-b]'>()
67+
})
3868
it('anyOf', () => {
3969
const values = ['fo/o', 'bar', 'baz', oneOrMore('this')] as const
4070
const input = anyOf(...values)

0 commit comments

Comments
 (0)