Skip to content

Commit

Permalink
Done: #2 all classes has been migrated to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Siemienik committed Feb 15, 2020
1 parent f1f5bb4 commit 5c601d3
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 222 deletions.
24 changes: 12 additions & 12 deletions src/CellTemplatePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { BaseCell, CellType } from './cell/BaseCell';
import { NormalCell } from './cell/NormalCell';
import { VariableCell } from './cell/VariableCell';
import { FinishCell } from './cell/FinishCell';
// import ForEachCell from "./cell/ForEachCell";
// import ContinueCell from "./cell/ContinueCell";
// import EndLoopCell from "./cell/EndLoopCell";
import { ForEachCell } from './cell/ForEachCell';
import { ContinueCell } from './cell/ContinueCell';
import { EndLoopCell } from './cell/EndLoopCell';
import { EndRowCell } from './cell/EndRowCell';
// import SumCell from "./cell/SumCell";
// import AverageCell from "./cell/AverageCell";
import { SumCell } from './cell/SumCell';
import { AverageCell } from './cell/AverageCell';
import { DeleteCell } from './cell/DeleteCell';
// import DumpColsCell from "./cell/DumpColsCell";
import { DumpColsCell } from './cell/DumpColsCell';
import { WsNameCell } from './cell/WsNameCell';
import { HyperlinkCell } from './cell/HyperlinkCell';
import { FormulaCell } from './cell/FormulaCell';
Expand All @@ -23,13 +23,13 @@ export class CellTemplatePool {
VariableCell,
FormulaCell,
HyperlinkCell,
// ForEachCell,
ForEachCell,
FinishCell,
// EndLoopCell,
// ContinueCell,
// DumpColsCell,
// SumCell,
// AverageCell,
EndLoopCell,
ContinueCell,
DumpColsCell,
SumCell,
AverageCell,
WsNameCell,
DeleteCell,
];
Expand Down
35 changes: 11 additions & 24 deletions src/cell/AverageCell.js → src/cell/AverageCell.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
import BaseCell from "./BaseCell";
import Scope from "../Scope";
import {ValueType} from "exceljs";

export default class AverageCell extends BaseCell {
/**
* @param {Scope} scope
* @returns {AverageCell}
*/
apply(scope) {
import { BaseCell } from './BaseCell';
import { Scope } from '../Scope';
import { Cell, CellFormulaValue, ValueType } from 'exceljs';

export class AverageCell extends BaseCell {
public apply(scope: Scope): AverageCell {
super.apply(scope);

const target = AverageCell._getTargetParam(scope);
const target = AverageCell.getTargetParam(scope);
const __startOutput = scope.vm[target] && scope.vm[target].__startOutput;
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

scope.setCurrentOutputValue({formula: `average(${start}:${end})`});
scope.setCurrentOutputValue({ formula: `average(${start}:${end})` } as CellFormulaValue);
}

scope.incrementCol();

return this;
}

/**
* @param {Scope} scope
* @returns {string}
* @protected
*/
static _getTargetParam(scope) {
return scope.getCurrentTemplateValue().split(' ')[2];
protected static getTargetParam(scope: Scope): string {
return scope.getCurrentTemplateValue()?.toString().split(' ')[2] || '';
}

/**
* @param {Cell} cell
* @returns {boolean}
*/
static match(cell) {
public static match(cell: Cell): boolean {
return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 10) === '#! AVERAGE';
}
}
28 changes: 0 additions & 28 deletions src/cell/ContinueCell.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/cell/ContinueCell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ForEachCell } from './ForEachCell';
import { Cell, ValueType } from 'exceljs';
import { Scope } from '../Scope';

export class ContinueCell extends ForEachCell {
public static match(cell: Cell): boolean {
return cell && cell.type === ValueType.String && typeof cell.value === 'string' && cell.value.substring(0, 11) === '#! CONTINUE';
}

public getSourceParam(scope: Scope): string {
const target = ForEachCell.getTargetParam(scope);

return scope.vm[target] && scope.vm[target].__from;
}

}
38 changes: 0 additions & 38 deletions src/cell/DumpColsCell.js

This file was deleted.

29 changes: 29 additions & 0 deletions src/cell/DumpColsCell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { BaseCell } from './BaseCell';
import { Cell, ValueType } from 'exceljs';
import { Scope } from '../Scope';

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

public apply(scope: Scope): DumpColsCell {
super.apply(scope);

const path = scope.getCurrentTemplateValue()?.toString().substring(13).split('.') || '';
const cols = Array.from(path).reduce((p, c) => p[c] || [], scope.vm);

scope.setCurrentOutputValue(null);

cols.forEach((x: any) => {
scope.setCurrentOutputValue(x);
scope.applyStyles();
scope.outputCell = Object.freeze({ ...scope.outputCell, c: scope.outputCell.c + 1 });
});

scope.incrementCol();

return this;
}

}
45 changes: 0 additions & 45 deletions src/cell/EndLoopCell.js

This file was deleted.

42 changes: 42 additions & 0 deletions src/cell/EndLoopCell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { BaseCell } from './BaseCell';
import { Cell, ValueType } from 'exceljs';
import { Scope } from '../Scope';

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

public apply(scope: Scope): EndLoopCell {
super.apply(scope);

const target =
scope
.getCurrentTemplateValue()
?.toString()
.split(' ')[2] || '';
const __start = scope.vm[target] && scope.vm[target].__start;
const __iterated = scope.vm[target] && scope.vm[target].__iterated;

scope.unfreezeOutput();

scope.vm[target] = Object.freeze({
...scope.vm[target],
__end: scope.templateCell,
__insetRows: true,
});

if (__start && !__iterated) {
scope.templateCell = __start;
} else {
scope.incrementRow();
}

return this;
}
}
Loading

0 comments on commit 5c601d3

Please sign in to comment.