Skip to content

Commit 67947ba

Browse files
committed
Fix typing
1 parent b985f68 commit 67947ba

6 files changed

Lines changed: 17 additions & 40 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ jobs:
2828
restore-keys: |
2929
${{ runner.os }}-node-
3030
- run: yarn
31-
- run: yarn check-format
32-
- run: yarn check-typing
33-
- run: yarn lint
34-
- run: yarn test --coverage
31+
- run: yarn ci
3532
- run: yarn build
3633
env:
3734
NODE_ENV: production

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"check-typing": "tsc",
2626
"storybook": "start-storybook -p 6006",
2727
"build-storybook": "build-storybook",
28-
"build-typedoc": "typedoc"
28+
"build-typedoc": "typedoc",
29+
"ci": "yarn check-format && yarn check-typing && yarn lint && yarn test --coverage"
2930
},
3031
"peerDependencies": {
3132
"react": ">=16.8.0",

src/engine.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const MOCK_FORMULA_PARSER = {
1010
} as unknown as FormulaParser;
1111
const EXAMPLE_FORMULA_RESULT = 42;
1212
const EXAMPLE_FORMULA_ERROR = "EXAMPLE_ERROR";
13-
const EXAMPLE_FORMULA_CELL = { value: "=A1" };
13+
const EXAMPLE_FORMULA = "=A1";
1414

1515
describe("getFormulaComputedValue()", () => {
1616
beforeEach(() => {
@@ -33,11 +33,11 @@ describe("getFormulaComputedValue()", () => {
3333
test.each(cases)("%s", (name, expected, mockParseReturn) => {
3434
MOCK_PARSE.mockImplementationOnce(() => mockParseReturn);
3535
expect(
36-
getFormulaComputedValue(EXAMPLE_FORMULA_CELL, ORIGIN, MOCK_FORMULA_PARSER)
36+
getFormulaComputedValue(EXAMPLE_FORMULA, ORIGIN, MOCK_FORMULA_PARSER)
3737
).toBe(expected);
3838
expect(MOCK_FORMULA_PARSER.parse).toBeCalledTimes(1);
3939
expect(MOCK_FORMULA_PARSER.parse).toBeCalledWith(
40-
Formula.extractFormula(EXAMPLE_FORMULA_CELL.value),
40+
Formula.extractFormula(EXAMPLE_FORMULA),
4141
{ col: 1, row: 1, sheet: "Sheet1" }
4242
);
4343
});

src/engine.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Point } from "./point";
66
import * as pointGraph from "./point-graph";
77
import * as pointSet from "./point-set";
88
import { CellBase } from "./types";
9-
import { isFormulaCell } from "./util";
109

1110
export class Model<Cell extends CellBase> {
1211
readonly data!: matrix.Matrix<Cell>;
@@ -32,7 +31,7 @@ export function updateCellValue<Cell extends CellBase>(
3231
cell: Cell
3332
): Model<Cell> {
3433
const nextData = matrix.set(point, cell, model.data);
35-
const nextReferenceGraph = isFormulaCell(cell)
34+
const nextReferenceGraph = Formula.isFormulaValue(cell.value)
3635
? updateReferenceGraph(model.referenceGraph, point, cell, nextData)
3736
: model.referenceGraph;
3837

@@ -102,8 +101,8 @@ function evaluateCell<Cell extends CellBase>(
102101
() => nextEvaluatedData
103102
);
104103

105-
const evaluatedValue = isFormulaCell(cell)
106-
? getFormulaComputedValue(cell, point, formulaParser)
104+
const evaluatedValue = Formula.isFormulaValue(cell.value)
105+
? getFormulaComputedValue(cell.value, point, formulaParser)
107106
: cell.value;
108107

109108
const evaluatedCell = { ...cell, value: evaluatedValue };
@@ -119,8 +118,8 @@ function evaluateCell<Cell extends CellBase>(
119118
if (!referrerCell) {
120119
continue;
121120
}
122-
const evaluatedValue = isFormulaCell(referrerCell)
123-
? getFormulaComputedValue(referrerCell, point, formulaParser)
121+
const evaluatedValue = Formula.isFormulaValue(referrerCell.value)
122+
? getFormulaComputedValue(referrerCell.value, point, formulaParser)
124123
: referrerCell.value;
125124
const evaluatedCell = { ...referrerCell, value: evaluatedValue };
126125
nextEvaluatedData = matrix.set(referrer, evaluatedCell, nextEvaluatedData);
@@ -139,7 +138,7 @@ export function createReferenceGraph(
139138
): pointGraph.PointGraph {
140139
const entries: Array<[Point, pointSet.PointSet]> = [];
141140
for (const [point, cell] of matrix.entries(data)) {
142-
if (cell && isFormulaCell(cell)) {
141+
if (cell && Formula.isFormulaValue(cell.value)) {
143142
const references = getReferences(
144143
Formula.extractFormula(cell.value),
145144
point,
@@ -168,9 +167,9 @@ export function createEvaluatedData<Cell extends CellBase>(
168167
}
169168

170169
// If the cell is a formula cell, evaluate it
171-
if (isFormulaCell(cell)) {
170+
if (Formula.isFormulaValue(cell.value)) {
172171
const evaluatedValue = getFormulaComputedValue(
173-
cell,
172+
cell.value,
174173
point,
175174
formulaParser
176175
);
@@ -187,10 +186,10 @@ export function createEvaluatedData<Cell extends CellBase>(
187186

188187
/** Get the computed value of a formula cell */
189188
export function getFormulaComputedValue(
190-
cell: CellBase<string>,
189+
value: string,
191190
point: Point,
192191
formulaParser: FormulaParser
193192
): Value {
194-
const formula = Formula.extractFormula(cell.value);
193+
const formula = Formula.extractFormula(value);
195194
return Formula.evaluate(formula, point, formulaParser);
196195
}

src/util.test.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ const EXAMPLE_STRING = "EXAMPLE_STRING";
6767
const EXAMPLE_CELL: Types.CellBase = {
6868
value: "EXAMPLE_CELL_VALUE",
6969
};
70-
const EXAMPLE_FORMULA_CELL: Types.CellBase = {
71-
value: "=TRUE()",
72-
};
70+
7371
const EXAMPLE_EMPTY_COPIED = PointMap.from<Types.CellBase>([]);
7472
const EXAMPLE_COPIED = PointMap.from([[Point.ORIGIN, EXAMPLE_CELL]]);
7573

@@ -291,16 +289,6 @@ describe("writeTextToClipboard()", () => {
291289
);
292290
});
293291

294-
describe("isFormulaCell()", () => {
295-
const cases = [
296-
["Returns true for formula cell", EXAMPLE_FORMULA_CELL, true],
297-
["Returns true for formula cell", EXAMPLE_CELL, false],
298-
] as const;
299-
test.each(cases)("%s", (name, cell, expected) => {
300-
expect(util.isFormulaCell(cell)).toBe(expected);
301-
});
302-
});
303-
304292
describe("getCSV()", () => {
305293
test("Returns given data as CSV", () => {
306294
expect(util.getCSV(EXAMPLE_DATA)).toBe(

src/util.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as PointRange from "./point-range";
55
import * as Selection from "./selection";
66
import * as PointMap from "./point-map";
77
import * as PointSet from "./point-set";
8-
import * as Formula from "./formula";
98

109
export { createEmpty as createEmptyMatrix } from "./matrix";
1110

@@ -134,13 +133,6 @@ export function getSelectedDimensions(
134133
: undefined;
135134
}
136135

137-
/** Returns whether given cell contains a formula value */
138-
export function isFormulaCell(
139-
cell: Types.CellBase
140-
): cell is Types.CellBase<string> {
141-
return Formula.isFormulaValue(cell.value);
142-
}
143-
144136
/** Get given data as CSV */
145137
export function getCSV(data: Matrix.Matrix<Types.CellBase>): string {
146138
const valueMatrix = Matrix.map((cell) => cell?.value || "", data);

0 commit comments

Comments
 (0)