Skip to content

Commit

Permalink
feat: add calculations to current
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiaanviaene committed Jun 4, 2019
1 parent 8655ed4 commit 370cf46
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 60 deletions.
20 changes: 14 additions & 6 deletions src/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ export class Resolver {
*/
private resolveDependencies(parentReferenceName: string, propertyList: any): any[] {
const dependencies = [];
const currentRegExp = /\(\$current((\+|\-|\/|\*)\d+)?\)/g;

for (const [key, value] of Object.entries(propertyList)) {
if (typeof value === 'string' && value.indexOf('@') === 0) {
const reference = this.resolveReference(parentReferenceName, value.substr(1));

propertyList[key] = `@${reference}`;
dependencies.push(reference);
} else if (typeof value === 'string' && value.includes('($current)')) {
propertyList[key] = value.replace(/\(\$current\)/g, this.resolveCurrent(parentReferenceName));
} else if (typeof value === 'string' && currentRegExp.test(value)) {
propertyList[key] = value.replace(currentRegExp, this.resolveCurrent(parentReferenceName, value));
} else if (typeof value === 'object' && value !== null) {
dependencies.push(...this.resolveDependencies(parentReferenceName, value));
}
Expand All @@ -78,19 +79,19 @@ export class Resolver {
* @return {any}
*/
private resolveReference(fixtureIdentify: string, reference: string) {
const currentRegExp = /^([\w-_]+)\(\$current\)$/gm;
const currentRegExp = /^([\w-_]+)\(\$current((\+|\-|\/|\*)\d+)?\)$/gm;
const rangeRegExp = /^([\w-_]+)\{(\d+)\.\.(\d+)\}$/gm;

if (currentRegExp.test(reference)) {
const index = this.resolveCurrent(fixtureIdentify);
const index = this.resolveCurrent(fixtureIdentify, reference);
/* istanbul ignore else */
if (!index) {
throw new Error(
`Error parsed index in reference: "${reference}" and fixture identify: ${fixtureIdentify}`,
);
}

return reference.replace('($current)', index);
return reference.replace(/\(\$current((\+|\-|\/|\*)\d+)?\)/g, index);
} else if (rangeRegExp.test(reference)) {
const splitting = reference.split(rangeRegExp);
sample(range(+splitting[2], +(+splitting[3]) + 1));
Expand All @@ -103,10 +104,17 @@ export class Resolver {

/**
* @param {string} fixtureIdentify
* @param {string} value
*/
private resolveCurrent(fixtureIdentify: string) {
private resolveCurrent(fixtureIdentify: string, value: string): any {
const currentIndexRegExp = /^[a-z\_\-]+(\d+)$/gi;
const currentRegExp = /\(\$current((\+|\-|\/|\*)\d+)\)/gi;
const splitting = fixtureIdentify.split(currentIndexRegExp);
if (currentRegExp.test(value)) {
const calc = String(value.match(/(\+|\-|\/|\*)\d+/gi));

return eval(splitting[1].concat(calc));
}

return splitting[1];
}
Expand Down
65 changes: 11 additions & 54 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,34 @@
{
"rules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"comment-format": [true, "check-space"],
"curly": true,
"eofline": true,
"indent": [
true,
"spaces"
],
"indent": [true, "spaces"],
"interface-name": [true, "always-prefix"],
"jsdoc-format": true,
"label-position": true,
"member-access": [
"check-accessor"
],
"member-access": ["check-accessor"],
"no-angle-bracket-type-assertion": false,
"no-any": false,
"no-arg": true,
"no-bitwise": false,
"no-conditional-assignment": true,
"no-consecutive-blank-lines": false,
"no-console": [
true,
"log",
"debug",
"info",
"time",
"timeEnd",
"trace"
],
"no-console": [true, "log", "debug", "info", "time", "timeEnd", "trace"],
"no-construct": true,
"no-constructor-vars": false,
"no-debugger": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-eval": false,
"no-inferrable-types": true,
"no-internal-module": true,
"no-invalid-this": true,
"no-reference": false,
"no-require-imports": false,
"no-shadowed-variable": true,
"quotemark": [
true,
"single"
],
"quotemark": [true, "single"],
"prefer-const": true,
"no-string-literal": false,
"no-switch-case-fall-through": true,
Expand All @@ -56,24 +37,11 @@
"no-var-keyword": true,
"no-var-requires": false,
"object-literal-sort-keys": false,
"one-line": [
true,
"check-open-brace",
"check-catch",
"check-else",
"check-finally",
"check-whitespace"
],
"one-line": [true, "check-open-brace", "check-catch", "check-else", "check-finally", "check-whitespace"],
"radix": true,
"semicolon": [
true,
"always"
],
"semicolon": [true, "always"],
"switch-default": true,
"triple-equals": [
true,
"allow-null-check"
],
"triple-equals": [true, "allow-null-check"],
"typedef-whitespace": [
true,
{
Expand All @@ -92,19 +60,8 @@
}
],
"use-isnan": true,
"variable-name": [
true,
"check-format",
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
],
"variable-name": [true, "check-format", "ban-keywords"],
"whitespace": [true, "check-branch", "check-decl", "check-operator", "check-separator", "check-type"],
"newline-before-return": true
}
}

0 comments on commit 370cf46

Please sign in to comment.