Skip to content

Commit

Permalink
fix(regex-actions): fix janus-idp#594: Cleanup some small code smells (
Browse files Browse the repository at this point in the history
…janus-idp#1039)

Signed-off-by: Christoph Jerolimov <[email protected]>
  • Loading branch information
christoph-jerolimov authored Jan 8, 2024
1 parent 49c7975 commit 215e49c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
14 changes: 7 additions & 7 deletions plugins/regex-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ export default async function createPlugin(

### Action : regex:replace

| Parameter Name | Type | Required | Description |
| -------------------------- | :----: | :------: | ------------------------------------------------------------------------------- |
| `regExps[].pattern` | string | Yes | The regex pattern to match the value like in `String.prototype.replace()` |
| `regExps[].flags` | string | No | The flags for the regex, possible values are: `g`, `m`, `i`, `y`, `u`, `s`, `d` |
| `regExps[].replacement` | string | Yes | The replacement value for the regex like in `String.prototype.replace()` |
| `regExps[].values[].key` | string | Yes | The key to access the regex value |
| `regExps[].values[].value` | string | Yes | The input value of the regex |
| Parameter Name | Type | Required | Description |
| -------------------------- | :------: | :------: | ------------------------------------------------------------------------------- |
| `regExps[].pattern` | string | Yes | The regex pattern to match the value like in `String.prototype.replace()` |
| `regExps[].flags` | string[] | No | The flags for the regex, possible values are: `g`, `m`, `i`, `y`, `u`, `s`, `d` |
| `regExps[].replacement` | string | Yes | The replacement value for the regex like in `String.prototype.replace()` |
| `regExps[].values[].key` | string | Yes | The key to access the regex value |
| `regExps[].values[].value` | string | Yes | The input value of the regex |

> **Warning**
>
Expand Down
12 changes: 4 additions & 8 deletions plugins/regex-actions/src/actions/regex/replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ import { createReplaceAction } from './replace';
describe('regex:replace', () => {
const action = createReplaceAction();

const logStream = {
write: jest.fn(),
} as jest.Mocked<Partial<Writable>> as jest.Mocked<Writable>;

const mockTmpDir = os.tmpdir();
const mockContext = {
input: {},
baseUrl: 'somebase',
workspacePath: mockTmpDir,
logger: getVoidLogger(),
logStream,
logStream: {} as Writable,
output: jest.fn(),
createTemporaryDirectory: jest.fn().mockResolvedValue(mockTmpDir),
};
Expand Down Expand Up @@ -87,7 +83,7 @@ describe('regex:replace', () => {
{
pattern: 'Dog',
replacement: 'ferret',
flags: ['i'] as any,
flags: ['i'] as 'i'[],
values: [
{
key: 'eg2',
Expand Down Expand Up @@ -120,7 +116,7 @@ describe('regex:replace', () => {
{
pattern: 'dog',
replacement: 'monkey',
flags: ['g'] as any,
flags: ['g'] as ('g' | 'i')[],
values: [
{
key: 'eg1',
Expand All @@ -131,7 +127,7 @@ describe('regex:replace', () => {
{
pattern: 'Dog',
replacement: 'ferret',
flags: ['g', 'i'] as any,
flags: ['g', 'i'] as ('g' | 'i')[],
values: [
{
key: 'eg2',
Expand Down
11 changes: 4 additions & 7 deletions plugins/regex-actions/src/actions/regex/replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ const schemaInput = z.object({
// You should not parse a regex (regular language) with a regex (regular language),
// you actually need a context free grammar to parse a regex (regular language).
// Hence, we are using a string comparison here.
value =>
!(
value.charAt(0) === '/' && value.charAt(value.length - 1) === '/'
),
value => !value.startsWith('/') && !value.endsWith('/'),
{
message:
'The RegExp constructor cannot take a string pattern with a leading and trailing forward slash.',
Expand All @@ -26,7 +23,7 @@ const schemaInput = z.object({
),
flags: z
.array(
// FIXME: changed from z.set() because that breaks zod-to-json-schema parser in unknown way.
// Prefer array over set here because input values are normally defined in YAML which doesn't support Sets.
z.enum(['g', 'm', 'i', 'y', 'u', 's', 'd'], {
invalid_type_error:
'Invalid flag, possible values are: g, m, i, y, u, s, d',
Expand Down Expand Up @@ -169,15 +166,15 @@ export const createReplaceAction = () => {
async handler(ctx) {
const input = ctx.input;

const values = {} as Record<string, string>;
const values: Record<string, string> = {};

for (const {
pattern,
flags: flagsInput,
replacement,
values: valuesInput,
} of input.regExps) {
// FIXME: remove `new Set()` when the `z.set()` issue is fixed
// remove duplicates from the flags input array
const flags = flagsInput
? Array.from(new Set(flagsInput)).join('')
: '';
Expand Down

0 comments on commit 215e49c

Please sign in to comment.