Skip to content

Commit

Permalink
Clean up variables.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-yu committed Sep 9, 2020
1 parent 9cc918e commit 0a5ad11
Showing 1 changed file with 69 additions and 54 deletions.
123 changes: 69 additions & 54 deletions src/variables.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const VARIABLE_REGEX = /\$\{(.*?)\}/g;
const FILE_REGEX_STR = '.*';

const VARIABLE_HANDLERS = new Map<string, (context: Context, value: string, argument: string | undefined) => string>([
['env', evaluateEnv],
['match', evaluateMatch],
['editor', evaluateEditor],
['regex', evaluateRegex],
]);

const REGEX_TEMPLATES = new Map<string, string>([
['file', '.*'],
]);

// TODO (ayu): docstrings

Expand All @@ -9,69 +19,74 @@ export interface Context {
match: RegExpExecArray | null,
}

function evaluate(value: string, variable: string, context: Context): string {
let argument: string | undefined;
const parts = variable.split(':');
function evaluateEnv(context: Context, value: string, variable: string | undefined): string {
if (!variable) {
throw new Error(`${value} cannot be resolved because no environment variable name is given.`);
}
const env = context.env[variable];
return (typeof env === 'string') ? env : '';
}

if (parts.length > 1) {
variable = parts[0];
argument = parts[1];
function evaluateMatch(context: Context, value: string, group: string | undefined): string {
// TODO (ayu): validation against the argument; is the argument validly formed?
// Does the argument exist in the match?
// Handle group numbers too
if (!context.match) {
throw new Error(`${value} cannot be resolved because there is no regex match`);
} else if (!context.match.groups) {
throw new Error(`${value} cannot be resolved because there are no regex capture groups to match`);
} else if (!group) {
throw new Error(`${value} cannot be resolved because no regex capture group is given`);
}
return context.match.groups[group];
}

function evaluateEditor(context: Context, value: string, variable: string | undefined): string {
if (!variable) {
throw new Error(`${value} cannot be resolved because no editor variable name is given.`);
}

switch (variable) {
case 'env':
if (!argument) {
throw new Error(`${value} cannot be resolved because no environment variable name is given.`);
}
const env = context.env[argument];
return (typeof env === 'string') ? env : '';
case 'match':
// TODO (ayu): validation against the argument; is the argument validly formed?
// Does the argument exist in the match?
// Handle group numbers too
if (!context.match) {
throw new Error(`${value} cannot be resolved because there is no regex match`);
} else if (!context.match.groups) {
throw new Error(`${value} cannot be resolved because there are no regex capture groups to match`);
} else if (!argument) {
throw new Error(`${value} cannot be resolved because no regex capture group is given`);
}
return context.match.groups[argument];
case 'editor':
if (!argument) {
throw new Error(`${value} cannot be resolved because no editor variable name is given.`);
case 'lines':
if (!context.lines) {
return '';
} else if (context.lines[0] === context.lines[1]) {
return `#${context.lines[0].toString()}`;
}
return `#${context.lines[0].toString()}-${context.lines[1].toString()}`;
default:
throw new Error(`${variable} is not a valid editor variable.`);
}
}

switch (argument) {
case 'lines':
if (!context.lines) {
return '';
} else if (context.lines[0] === context.lines[1]) {
return `#${context.lines[0].toString()}`;
}
return `#${context.lines[0].toString()}-${context.lines[1].toString()}`;
default:
throw new Error(`${argument} is not a valid editor variable.`);
}
case 'regex':
if (!argument) {
throw new Error(`${value} cannot be resolved because no regex template name is given.`);
}
function evaluateRegex(_: Context, value: string, template: string | undefined): string {
if (!template) {
throw new Error(`${value} cannot be resolved because no regex template name is given.`);
}

let regex = null;
const regex = REGEX_TEMPLATES.get(template);
if (!regex) {
throw new Error(`${template} is not a valid regex template.`);
}

switch (argument) {
case 'file':
regex = FILE_REGEX_STR;
break;
default:
throw new Error(`${argument} is not a valid regex template.`);
}
return `(?<${template}>${regex})`;
}

return `(?<${argument}>${regex})`;
default:
return value;
function evaluate(value: string, variable: string, context: Context): string {
let argument: string | undefined;
const parts = variable.split(':');

if (parts.length > 1) {
variable = parts[0];
argument = parts[1];
}

const handler = VARIABLE_HANDLERS.get(variable);
if (handler) {
return handler(context, value, argument);
}

return value;
}

export function resolve(value: string, context: Context): string {
Expand Down

0 comments on commit 0a5ad11

Please sign in to comment.