Skip to content

Commit

Permalink
#2 Ts lint fixation + some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Siemienik committed Feb 16, 2020
1 parent 5c601d3 commit 8c8347f
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 49 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"build": "tsc",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json",
"lint:fix": "tslint -p tsconfig.json --fix",
"test": "mocha -r ts-node/register tests/**/*.test.ts",
"coverageRaport": "nyc -r lcov -e .ts -x \"*.test.ts\" mocha -r ts-node/register tests/**/*.test.ts && nyc report",
"prepublishOnly": "npm test && npm run lint && npm run build",
Expand Down
2 changes: 1 addition & 1 deletion src/CellTemplateDebugPool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CellTemplatePool } from './CellTemplatePool';
import { Cell } from 'exceljs';
import { BaseCell } from './cell/BaseCell';

/* tslint:disable:no-console */
export class CellTemplateDebugPool extends CellTemplatePool {
/**
* do normal match and log in console result.
Expand Down
22 changes: 11 additions & 11 deletions src/cell/AverageCell.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { BaseCell } from './BaseCell';
import { Scope } from '../Scope';
import { Cell, CellFormulaValue, ValueType } from 'exceljs';

/* tslint:disable:variable-name */
export class AverageCell extends BaseCell {

public static match(cell: Cell): boolean {
return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 10) === '#! AVERAGE';
}

protected static getTargetParam(scope: Scope): string {
return scope.getCurrentTemplateValue()?.toString().split(' ')[2] || '';
}
public apply(scope: Scope): AverageCell {
super.apply(scope);

Expand All @@ -11,8 +19,8 @@ export class AverageCell extends BaseCell {
const __endOutput = scope.vm[target] && scope.vm[target].__endOutput;

if (__startOutput && __endOutput) {
const start = scope.output.worksheets[scope.outputCell.ws].getCell(__startOutput, scope.outputCell.c).address; //todo refactoring
const end = scope.output.worksheets[scope.outputCell.ws].getCell(__endOutput, scope.outputCell.c).address; //todo refactoring
const start = scope.output.worksheets[scope.outputCell.ws].getCell(__startOutput, scope.outputCell.c).address; // todo refactoring
const end = scope.output.worksheets[scope.outputCell.ws].getCell(__endOutput, scope.outputCell.c).address; // todo refactoring

scope.setCurrentOutputValue({ formula: `average(${start}:${end})` } as CellFormulaValue);
}
Expand All @@ -21,12 +29,4 @@ export class AverageCell extends BaseCell {

return this;
}

protected static getTargetParam(scope: Scope): string {
return scope.getCurrentTemplateValue()?.toString().split(' ')[2] || '';
}

public static match(cell: Cell): boolean {
return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 10) === '#! AVERAGE';
}
}
4 changes: 2 additions & 2 deletions src/cell/DeleteCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export class DeleteCell extends BaseCell {
public apply(scope: Scope): DeleteCell {
super.apply(scope);

const target = scope.getCurrentTemplateValue()?.toString().split(' ')[2]; //todo make some function for scope.getCurrentTemplateValue()?.toString().split(' ') ;
const target = scope.getCurrentTemplateValue()?.toString().split(' ')[2]; // todo make some function for scope.getCurrentTemplateValue()?.toString().split(' ') ;

if (target == undefined) return this; //it's ok here
if (target === undefined) { return this; } // it's ok here

scope.vm[target] = undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/cell/DumpColsCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Cell, ValueType } from 'exceljs';
import { Scope } from '../Scope';

export class DumpColsCell extends BaseCell {
static match(cell: Cell): boolean {
public static match(cell: Cell): boolean {
return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 12) === '#! DUMP_COLS';
}

Expand Down
1 change: 1 addition & 0 deletions src/cell/EndLoopCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseCell } from './BaseCell';
import { Cell, ValueType } from 'exceljs';
import { Scope } from '../Scope';

/* tslint:disable:variable-name */
export class EndLoopCell extends BaseCell {
public static match(cell: Cell): boolean {
return (
Expand Down
9 changes: 3 additions & 6 deletions src/cell/FinishCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseCell } from './BaseCell';
import { Cell, ValueType } from 'exceljs';
import { Scope } from '../Scope';

/* tslint:disable:variable-name */
export class FinishCell extends BaseCell {
public static match(cell: Cell): boolean {
return (
Expand All @@ -18,12 +19,8 @@ export class FinishCell extends BaseCell {
* * condition's path follow to undefined
* * condition is true
* In other way, the same template sheet should render next output sheet - as long as condition is false
*
* @param {Scope} scope
* @returns {boolean}
* @protected
*/
private static _getCondition(scope: Scope): boolean {
protected static getCondition(scope: Scope): boolean {
const args =
scope
.getCurrentTemplateValue()
Expand All @@ -44,7 +41,7 @@ export class FinishCell extends BaseCell {

let wst = scope.template.worksheets[scope.templateCell.ws];

if (FinishCell._getCondition(scope)) {
if (FinishCell.getCondition(scope)) {
// todo refactoring scope.iterateWorksheet

const wstNext = scope.templateCell.ws + 1;
Expand Down
13 changes: 7 additions & 6 deletions src/cell/ForEachCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseCell } from './BaseCell';
import { Scope } from '../Scope';
import { Cell, ValueType } from 'exceljs';

/* tslint:disable:variable-name */
/**
* Pattern: `#! FOR_EACH [TARGET] [SOURCE]`
* Iterate through `vm[SOURCE]` and store current item in readonly `vm[TARGET]`.
Expand All @@ -25,16 +26,12 @@ export class ForEachCell extends BaseCell {
return scope.getCurrentTemplateValue()?.toString().split(' ')[2] || '';
}

protected getSourceParam(scope: Scope): string {
return scope.getCurrentTemplateValue()?.toString().split(' ')[3] || '';
}


public apply(scope: Scope): ForEachCell {
const target = ForEachCell.getTargetParam(scope);
const __from = this.getSourceParam(scope);

//todo refactoring
// todo refactoring
const __index = (scope.vm[target] && scope.vm[target].__index || 0) + 1;
if (__index === 1) {
super.apply(scope);
Expand Down Expand Up @@ -65,7 +62,7 @@ export class ForEachCell extends BaseCell {
__insetRows = false;
if (!scope.isFrozen()) {
for (let i = __end.r; i > __start.r; i--) {
scope.output.worksheets[scope.outputCell.ws].spliceRows( //todo refactoring
scope.output.worksheets[scope.outputCell.ws].spliceRows( // todo refactoring
scope.outputCell.r + 1,
0,
[],
Expand Down Expand Up @@ -95,4 +92,8 @@ export class ForEachCell extends BaseCell {

return this;
}

protected getSourceParam(scope: Scope): string {
return scope.getCurrentTemplateValue()?.toString().split(' ')[3] || '';
}
}
32 changes: 13 additions & 19 deletions src/cell/FormulaCell.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { BaseCell } from './BaseCell';
import { Cell, CellFormulaValue, ValueType } from 'exceljs';
import { Scope } from '../Scope';
import {BaseCell} from './BaseCell';
import {Cell, CellFormulaValue, ValueType} from 'exceljs';
import {Scope} from '../Scope';

export class FormulaCell extends BaseCell {
/**
* @inheritDoc
* @param {Cell} cell
* @returns {boolean}
*/
public static match(cell: Cell): boolean {
return cell && cell.type === ValueType.Formula;
}

/**
* @inheritDoc
* @param {Scope} scope
* @returns {FormulaCell}
*/
public apply(scope: Scope): FormulaCell {
super.apply(scope);

Expand All @@ -26,16 +16,20 @@ export class FormulaCell extends BaseCell {
const value = scope.getCurrentTemplateValue() as CellFormulaValue;
let formula = value.formula;

//todo extract method match addresses
let matches;
let addresses = [];
while (matches = regex.exec(formula)) {
// todo extract method match addresses
const addresses = [];
while (true) {
const matches = regex.exec(formula);
if (matches === null) {
break;
}

addresses.push({index: matches.index, col: matches[1], row: +matches[2], len: matches[0].length})
}
addresses.reverse();

//todo extract method getShiftedFormula
let formulaChars = Array.from(formula);
// todo extract method getShiftedFormula
const formulaChars = Array.from(formula);
addresses.forEach(a => formulaChars.splice(a.index, a.len, `${a.col}${a.row + shift}`));
formula = formulaChars.join('');

Expand Down
1 change: 0 additions & 1 deletion src/cell/HyperlinkCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export class HyperlinkCell extends BaseCell {
const url = HyperlinkCell.getUrlParam(scope).split('.').reduce((p, c) => p[c] || {}, scope.vm);
if (typeof url === 'string') {
const label = HyperlinkCell.getLabelParam(scope).split('.').reduce((p, c) => p[c] || {}, scope.vm) || url;
console.log(label);
scope.setCurrentOutputValue({ text: label, hyperlink: url });
}

Expand Down
5 changes: 3 additions & 2 deletions src/cell/SumCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseCell } from './BaseCell';
import { Scope } from '../Scope';
import { Cell, CellFormulaValue, ValueType } from 'exceljs';

/* tslint:disable:variable-name */
export class SumCell extends BaseCell {
public static match(cell: Cell): boolean {
return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 6) === '#! SUM';
Expand All @@ -19,8 +20,8 @@ export class SumCell extends BaseCell {
const __endOutput = scope.vm[target] && scope.vm[target].__endOutput;

if (__startOutput && __endOutput) {
const start = scope.output.worksheets[scope.outputCell.ws].getCell(__startOutput, scope.outputCell.c).address; //todo refactoring
const end = scope.output.worksheets[scope.outputCell.ws].getCell(__endOutput, scope.outputCell.c).address; //todo refactoring
const start = scope.output.worksheets[scope.outputCell.ws].getCell(__startOutput, scope.outputCell.c).address; // todo refactoring
const end = scope.output.worksheets[scope.outputCell.ws].getCell(__endOutput, scope.outputCell.c).address; // todo refactoring

scope.setCurrentOutputValue({ formula: `sum(${start}:${end})` } as CellFormulaValue);
}
Expand Down
1 change: 1 addition & 0 deletions src/cell/VariableCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class VariableCell extends BaseCell {
const value = path.reduce((p, c) => (typeof p === 'object' ? p[c] : p), scope.vm);
if (value === undefined) {
// todo do it better (use logger or somethink like that)
// tslint:disable-next-line:no-console
console.warn(
`WARN: ${path} is undefined for output: ${scope.outputCell} when template is:${scope.templateCell}`,
);
Expand Down
1 change: 1 addition & 0 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CellTemplateDebugPool } from './CellTemplateDebugPool';
import { CellTemplatePool } from './CellTemplatePool';
import { Workbook } from 'exceljs';

// tslint:disable:comment-format
//*
const debug = true;
/*/
Expand Down

0 comments on commit 8c8347f

Please sign in to comment.