Skip to content

Commit f4a6799

Browse files
committed
Extract transformCoordToPoint, getCellValue and getCellRangeValue to utils and test
Fix formatting Fix lint warnings
1 parent 4b5af55 commit f4a6799

File tree

4 files changed

+90
-29
lines changed

4 files changed

+90
-29
lines changed

src/Spreadsheet.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import React from "react";
6-
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
6+
import { fireEvent, render, screen } from "@testing-library/react";
77
import Spreadsheet, { Props } from "./Spreadsheet";
88
import * as Matrix from "./matrix";
99
import * as Types from "./types";

src/Spreadsheet.tsx

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ import {
3434
range,
3535
readTextFromClipboard,
3636
writeTextToClipboard,
37-
getComputedValue,
3837
getSelectedCSV,
3938
calculateSpreadsheetSize,
39+
transformCoordToPoint,
40+
getCellRangeValue,
41+
getCellValue,
4042
} from "./util";
4143
import "./Spreadsheet.css";
4244

@@ -388,40 +390,27 @@ const Spreadsheet = <CellType extends Types.CellBase>(
388390
formulaParser.on("callCellValue", (cellCoord, done) => {
389391
let value;
390392
try {
391-
const point = {
392-
row: cellCoord.row.index,
393-
column: cellCoord.column.index,
394-
};
395-
const cell = Matrix.get(point, store.getState().data);
396-
value = getComputedValue<CellType, CellType["value"]>({
397-
cell,
398-
formulaParser: formulaParser,
399-
});
393+
const point = transformCoordToPoint(cellCoord);
394+
const data = store.getState().data;
395+
value = getCellValue(formulaParser, data, point);
400396
} catch (error) {
401397
console.error(error);
402398
} finally {
403399
done(value);
404400
}
405401
});
406402
formulaParser.on("callRangeValue", (startCellCoord, endCellCoord, done) => {
407-
const startPoint = {
408-
row: startCellCoord.row.index,
409-
column: startCellCoord.column.index,
410-
};
411-
const endPoint = {
412-
row: endCellCoord.row.index,
413-
column: endCellCoord.column.index,
414-
};
415-
const values = Matrix.toArray(
416-
Matrix.slice(startPoint, endPoint, store.getState().data),
417-
(cell) =>
418-
getComputedValue<CellType, CellType["value"]>({
419-
cell,
420-
formulaParser: formulaParser,
421-
})
422-
);
423-
424-
done(values);
403+
const startPoint = transformCoordToPoint(startCellCoord);
404+
const endPoint = transformCoordToPoint(endCellCoord);
405+
const data = store.getState().data;
406+
let values;
407+
try {
408+
values = getCellRangeValue(formulaParser, data, startPoint, endPoint);
409+
} catch (error) {
410+
console.error(error);
411+
} finally {
412+
done(values);
413+
}
425414
});
426415
}, [formulaParser, store, handleCut, handleCopy, handlePaste]);
427416

src/util.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import {
2828
readTextFromClipboard,
2929
normalizeSelected,
3030
getCopiedRange,
31+
transformCoordToPoint,
32+
getCellValue,
33+
getCellRangeValue,
3134
} from "./util";
3235

3336
const EXAMPLE_INPUT_VALUE = "EXAMPLE_INPUT_VALUE";
@@ -468,3 +471,31 @@ describe("getCopiedRange()", () => {
468471
expect(getCopiedRange(copied, hasPasted)).toEqual(expected);
469472
});
470473
});
474+
475+
describe("transformCoordToPoint()", () => {
476+
test("transforms coord to point", () => {
477+
expect(
478+
transformCoordToPoint({
479+
row: { index: Point.ORIGIN.row },
480+
column: { index: Point.ORIGIN.column },
481+
})
482+
).toEqual(Point.ORIGIN);
483+
});
484+
});
485+
486+
describe("getCellValue()", () => {
487+
expect(getCellValue(MOCK_FORMULA_PARSER, EXAMPLE_DATA, Point.ORIGIN)).toEqual(
488+
null
489+
);
490+
});
491+
492+
describe("getCellRangeValue()", () => {
493+
expect(
494+
getCellRangeValue(
495+
MOCK_FORMULA_PARSER,
496+
EXAMPLE_DATA,
497+
Point.ORIGIN,
498+
Point.ORIGIN
499+
)
500+
).toEqual([null]);
501+
});

src/util.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,44 @@ export function getCopiedRange(
218218
const set: PointSet.PointSet = PointMap.map(() => true, copied);
219219
return PointSet.toRange(set);
220220
}
221+
222+
/** Tranform given hot-formula-parser coord to Point.Point */
223+
export function transformCoordToPoint(coord: {
224+
row: { index: number };
225+
column: { index: number };
226+
}): Point.Point {
227+
return { row: coord.row.index, column: coord.column.index };
228+
}
229+
230+
/**
231+
* Get cell value for given point from given spreadsheet data with evaluated
232+
* cells using given formulaParser
233+
*/
234+
export function getCellValue<CellType extends Types.CellBase>(
235+
formulaParser: hotFormulaParser.Parser,
236+
data: Matrix.Matrix<CellType>,
237+
point: Point.Point
238+
): FormulaParseResult | CellType["value"] | null {
239+
return getComputedValue({
240+
cell: Matrix.get(point, data),
241+
formulaParser,
242+
});
243+
}
244+
245+
/**
246+
* Get cell range value for given start and end points from given spreadsheet
247+
* data with evaluated cells using given formulaParser
248+
*/
249+
export function getCellRangeValue<CellType extends Types.CellBase>(
250+
formulaParser: hotFormulaParser.Parser,
251+
data: Matrix.Matrix<CellType>,
252+
start: Point.Point,
253+
end: Point.Point
254+
): Array<FormulaParseResult | CellType["value"] | null> {
255+
return Matrix.toArray(Matrix.slice(start, end, data), (cell) =>
256+
getComputedValue({
257+
cell,
258+
formulaParser,
259+
})
260+
);
261+
}

0 commit comments

Comments
 (0)