-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add regex support to requiredLabels and blockingLabels (#818)
- Loading branch information
Showing
9 changed files
with
180 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { string, oneOf, object } from "@mojotech/json-type-validation" | ||
|
||
export interface RegexPattern { | ||
regex: RegExp | ||
} | ||
export type LiteralPattern = string | ||
export type Pattern = RegexPattern | LiteralPattern | ||
|
||
export function matchesPattern (pattern: Pattern, input: string) { | ||
if (typeof pattern === 'string') { | ||
return pattern === input | ||
} else if ('regex' in pattern) { | ||
return pattern.regex.test(input) | ||
} else { | ||
throw new Error(`Invalid pattern at runtime: ${typeof pattern}`) | ||
} | ||
} | ||
|
||
export function stringifyPattern (pattern: Pattern) { | ||
if (typeof pattern === 'string') { | ||
return pattern; | ||
} else if ('regex' in pattern) { | ||
return `{regex: ${pattern.regex}}` | ||
} else { | ||
return '[invalid pattern]' | ||
} | ||
} | ||
|
||
export const regexDecoder = string().map(value => new RegExp(value)) | ||
|
||
export const patternDecoder = oneOf<Pattern>( | ||
string(), | ||
object({ | ||
regex: regexDecoder | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { matchesPattern, stringifyPattern } from '../src/pattern' | ||
|
||
describe('matchesPattern', () => { | ||
describe('while using literal pattern', () => { | ||
it('should return true when pattern equals input', () => { | ||
expect(matchesPattern('word', 'word')).toBe(true) | ||
}) | ||
it('should return false when input matches only partially', () => { | ||
expect(matchesPattern('word', 'abc word abc')).toBe(false) | ||
}) | ||
it('should return false when input has different casing', () => { | ||
expect(matchesPattern('word', 'Word')).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('while using regex pattern', () => { | ||
it('should return true when pattern matches input', () => { | ||
expect(matchesPattern({ regex: /word/ }, 'word')).toBe(true) | ||
}) | ||
it('should return true when input has part of pattern', () => { | ||
expect(matchesPattern({ regex: /word/ }, 'abc word abc')).toBe(true) | ||
}) | ||
it('should return false when input has different casing', () => { | ||
expect(matchesPattern({ regex: /word/ }, 'Word')).toBe(false) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('stringifyPattern', () => { | ||
it('should stringify literal patterns', () => { | ||
expect(stringifyPattern('pattern')).toBe('pattern') | ||
}) | ||
it('should stringify regex patterns', () => { | ||
expect(stringifyPattern({ regex: /pattern/ })).toBe('{regex: /pattern/}') | ||
}) | ||
}) |