diff --git a/src/Resolver.ts b/src/Resolver.ts index 76b25af..14d0bac 100644 --- a/src/Resolver.ts +++ b/src/Resolver.ts @@ -55,6 +55,7 @@ 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) { @@ -62,8 +63,8 @@ export class Resolver { 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)); } @@ -78,11 +79,11 @@ 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( @@ -90,7 +91,7 @@ export class Resolver { ); } - 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)); @@ -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]; } diff --git a/tslint.json b/tslint.json index 21bd6a1..eb490af 100644 --- a/tslint.json +++ b/tslint.json @@ -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, @@ -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, { @@ -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 } }