Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CellValue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CellError, ErrorType, InternalCellValue, NoErrorCellValue, simpleCellAddress, SimpleCellAddress} from './Cell'
import {CellValueChange} from './ContentChanges'
import {Config} from './Config'
import {CellValueChange} from './ContentChanges'
import {NamedExpressions} from './NamedExpressions'

export type CellValue = NoErrorCellValue | DetailedCellError
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyGraph/DependencyGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {ColumnsSpan} from '../ColumnsSpan'
import {Config} from '../Config'
import {findSmallerRange} from '../interpreter/plugin/SumprodPlugin'
import {LazilyTransformingAstService} from '../LazilyTransformingAstService'
import {Maybe} from '../Maybe'
import {Ast} from '../parser'
import {RowsSpan} from '../RowsSpan'
import {Statistics, StatType} from '../statistics/Statistics'
Expand Down Expand Up @@ -523,7 +524,7 @@ export class DependencyGraph {
return this.addressMapping.getWidth(sheet)
}

public getMatrix(range: AbsoluteCellRange): MatrixVertex | undefined {
public getMatrix(range: AbsoluteCellRange): Maybe<MatrixVertex> {
return this.matrixMapping.getMatrix(range)
}

Expand Down
3 changes: 2 additions & 1 deletion src/DependencyGraph/MatrixMapping.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {AbsoluteCellRange} from '../AbsoluteCellRange'
import {SimpleCellAddress} from '../Cell'
import {ColumnsSpan} from '../ColumnsSpan'
import {Maybe} from '../Maybe'
import {RowsSpan} from '../RowsSpan'
import {MatrixVertex} from './'

export class MatrixMapping {
public readonly matrixMapping: Map<string, MatrixVertex> = new Map()

public getMatrix(range: AbsoluteCellRange): MatrixVertex | undefined {
public getMatrix(range: AbsoluteCellRange): Maybe<MatrixVertex> {
return this.matrixMapping.get(range.toString())
}

Expand Down
7 changes: 4 additions & 3 deletions src/DependencyGraph/SheetMapping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {TranslationPackage} from '../i18n'
import {Maybe} from '../Maybe'

function canonicalize(sheetDisplayName: string): string {
return sheetDisplayName.toLowerCase()
Expand Down Expand Up @@ -57,7 +58,7 @@ export class SheetMapping {
return sheet.id
}

public get = (sheetName: string): number | undefined => {
public get = (sheetName: string): Maybe<number> => {
const sheet = this.mappingFromCanonicalName.get(canonicalize(sheetName))
if (sheet) {
return sheet.id
Expand All @@ -70,7 +71,7 @@ export class SheetMapping {
return this.fetchSheetById(sheetId).displayName
}

public getDisplayName(sheetId: number): string | undefined {
public getDisplayName(sheetId: number): Maybe<string> {
const sheet = this.mappingFromId.get(sheetId)
if (sheet) {
return sheet.displayName
Expand All @@ -79,7 +80,7 @@ export class SheetMapping {
}
}

public getDisplayNameByName(sheetName: string): string | undefined {
public getDisplayNameByName(sheetName: string): Maybe<string> {
const sheet = this.mappingFromCanonicalName.get(canonicalize(sheetName))
if (sheet) {
return sheet.displayName
Expand Down
75 changes: 54 additions & 21 deletions src/HyperFormula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {Evaluator} from './Evaluator'
import {Sheet, Sheets} from './GraphBuilder'
import {IBatchExecutor} from './IBatchExecutor'
import {LazilyTransformingAstService} from './LazilyTransformingAstService'
import {Maybe} from './Maybe'
import {NamedExpressions} from './NamedExpressions'
import {AstNodeType, ParserWithCaching, simpleCellAddressFromString, simpleCellAddressToString, Unparser, Ast} from './parser'
import {Statistics, StatType} from './statistics/Statistics'
Expand Down Expand Up @@ -131,7 +132,7 @@ export class HyperFormula {
*
* @param address - cell coordinates
*/
public getCellValue(address: SimpleCellAddress): CellValue {
public getCellValue = (address: SimpleCellAddress): CellValue => {
return this.exporter.exportValue(this.dependencyGraph.getCellValue(address))
}

Expand All @@ -140,7 +141,7 @@ export class HyperFormula {
*
* @param address - cell coordinates
*/
public getCellFormula(address: SimpleCellAddress): string | undefined {
public getCellFormula = (address: SimpleCellAddress): Maybe<string> => {
Comment thread
izulin marked this conversation as resolved.
Resolved
const formulaVertex = this.dependencyGraph.getCell(address)
if (formulaVertex instanceof FormulaCellVertex) {
const formula = formulaVertex.getFormula(this.dependencyGraph.lazilyTransformingAstService)
Expand All @@ -156,56 +157,88 @@ export class HyperFormula {
return undefined
}

public getCellSerialized = (address: SimpleCellAddress): CellValue => {
Comment thread
izulin marked this conversation as resolved.
Resolved
return this.getCellFormula(address) || this.getCellValue(address)
Comment thread
wojciechczerniak marked this conversation as resolved.
Resolved
}

/**
* Returns array with values of all cells.
* Returns array with values of all cells from Sheet
*
* @param sheet - sheet id number
*/
public getValues(sheet: number): CellValue[][] {
public getSheetValues = (sheet: number): CellValue[][] => {
return this.genericSheetGetter(sheet, this.getCellValue)
}

public getSheetFormulas = (sheet: number): Maybe<string>[][] => {
return this.genericSheetGetter(sheet, this.getCellFormula)
}

public getSheetSerialized = (sheet: number): CellValue[][] => {
return this.genericSheetGetter(sheet, this.getCellSerialized)
}

private genericSheetGetter<T>(sheet: number, getter: (address: SimpleCellAddress) => T): T[][] {
const sheetHeight = this.dependencyGraph.getSheetHeight(sheet)
const sheetWidth = this.dependencyGraph.getSheetWidth(sheet)

const arr: CellValue[][] = new Array(sheetHeight)
const arr: T[][] = new Array(sheetHeight)
for (let i = 0; i < sheetHeight; i++) {
arr[i] = new Array(sheetWidth)

for (let j = 0; j < sheetWidth; j++) {
const address = simpleCellAddress(sheet, j, i)
arr[i][j] = this.exporter.exportValue(this.dependencyGraph.getCellValue(address))
arr[i][j] = getter(address)
Comment thread
izulin marked this conversation as resolved.
Resolved
}
}

return arr
}

/**
* Returns map containing dimensions of all sheets.
*
*/
public getSheetsDimensions(): Map<string, { width: number, height: number }> {
const sheetDimensions = new Map<string, { width: number, height: number }>()
for (const sheetName of this.sheetMapping.displayNames()) {
const sheetId = this.sheetMapping.fetch(sheetName)
sheetDimensions.set(sheetName, {
width: this.dependencyGraph.getSheetWidth(sheetId),
height: this.dependencyGraph.getSheetHeight(sheetId),
})
}
return sheetDimensions
public getSheetsDimensions(): Record<string, { width: number, height: number }> {
return this.genericAllGetter(this.getSheetDimensions)
}

/**
* Returns dimensions of specific sheet.
*
* @param sheet - sheet id number
*/
public getSheetDimensions(sheet: number): { width: number, height: number } {
public getSheetDimensions = (sheet: number): { width: number, height: number } => {
return {
width: this.dependencyGraph.getSheetWidth(sheet),
height: this.dependencyGraph.getSheetHeight(sheet),
}
}

/**
* Returns map containing values of all sheets.
*
*/
public getAllValues(): Record<string, CellValue[][]> {
return this.genericAllGetter(this.getSheetValues)
}

public getAllFormulas(): Record<string, Maybe<string>[][]> {
Comment thread
izulin marked this conversation as resolved.
Resolved
return this.genericAllGetter(this.getSheetFormulas)
}

public getAllSerialized(): Record<string, CellValue[][]> {
Comment thread
izulin marked this conversation as resolved.
Resolved
return this.genericAllGetter(this.getSheetSerialized)
}
Comment thread
izulin marked this conversation as resolved.
Resolved

private genericAllGetter<T>( sheetGetter: (sheet: number) => T): Record<string, T> {
const ret: Record<string, T> = {}
for (const sheetName of this.sheetMapping.displayNames()) {
const sheetId = this.sheetMapping.fetch(sheetName)
ret[sheetName] = sheetGetter(sheetId)
}
return ret
Comment thread
izulin marked this conversation as resolved.
Resolved
}

/**
* Returns snapshot of a computation time statistics.
*/
Expand Down Expand Up @@ -709,7 +742,7 @@ export class HyperFormula {
* @param address - object representation of absolute address
* @param sheet - if is not equal with address sheet index, string representation will contain sheet name
* */
public simpleCellAddressToString(address: SimpleCellAddress, sheet: number): string | undefined {
public simpleCellAddressToString(address: SimpleCellAddress, sheet: number): Maybe<string> {
return simpleCellAddressToString(this.sheetMapping.fetchDisplayName, address, sheet)
}

Expand All @@ -721,7 +754,7 @@ export class HyperFormula {
* @param sheetId - ID of the sheet, for which we want to retrieve name
* @returns name of the sheet
*/
public getSheetName(sheetId: number): string | undefined {
public getSheetName(sheetId: number): Maybe<string> {
return this.sheetMapping.getDisplayName(sheetId)
}

Expand All @@ -733,7 +766,7 @@ export class HyperFormula {
* @param sheetName - name of the sheet, for which we want to retrieve ID
* @returns ID of the sheet
*/
public getSheetId(sheetName: string): number | undefined {
public getSheetId(sheetName: string): Maybe<number> {
return this.sheetMapping.get(sheetName)
}

Expand Down
1 change: 1 addition & 0 deletions src/Maybe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Maybe<T> = T | undefined
7 changes: 4 additions & 3 deletions src/NamedExpressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {absolutizeDependencies} from './absolutizeDependencies'
import {SimpleCellAddress, simpleCellAddress, InternalCellValue} from './Cell'
import {CellContent, CellContentParser, RawCellContent} from './CellContentParser'
import {DependencyGraph, AddressMapping, SparseStrategy} from './DependencyGraph'
import {Maybe} from './Maybe'
import {ParserWithCaching} from './parser'
import {CrudOperations} from './CrudOperations'

Expand Down Expand Up @@ -30,11 +31,11 @@ class NamedExpressionsStore {
this.rowMapping.set(namedExpression.row, namedExpression)
}

public get(expressionName: string): NamedExpression | undefined {
public get(expressionName: string): Maybe<NamedExpression> {
return this.mapping.get(this.normalizeExpressionName(expressionName))
}

public getByRow(row: number): NamedExpression | undefined {
public getByRow(row: number): Maybe<NamedExpression> {
return this.rowMapping.get(row)
}

Expand Down Expand Up @@ -87,7 +88,7 @@ export class NamedExpressions {
return namedExpression.name
}

public getDisplayNameByName(expressionName: string): string | undefined {
public getDisplayNameByName(expressionName: string): Maybe<string> {
const namedExpression = this.workbookStore.get(expressionName)
if (namedExpression) {
return namedExpression.name
Expand Down
4 changes: 3 additions & 1 deletion src/generatorUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Maybe} from './Maybe'

export function * empty<T>(): IterableIterator<T> { }

export function split<T>(iterable: IterableIterator<T>): { value?: T, rest: IterableIterator<T> } {
Expand All @@ -11,7 +13,7 @@ export function split<T>(iterable: IterableIterator<T>): { value?: T, rest: Iter
}
}

export function first<T>(iterable: IterableIterator<T>): T | undefined {
export function first<T>(iterable: IterableIterator<T>): Maybe<T> {
const iterator: Iterator<T> = iterable[Symbol.iterator]()
const { done, value } = iterator.next()

Expand Down
3 changes: 2 additions & 1 deletion src/interpreter/Criterion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {InternalCellValue} from '../Cell'
import {Maybe} from '../Maybe'

export enum CriterionType {
GREATER_THAN = 'GREATER_THAN',
Expand All @@ -16,7 +17,7 @@ export const buildCriterion = (operator: CriterionType, value: number | string)

export class CriterionPackage {

public static fromCellValue(raw: InternalCellValue): CriterionPackage | undefined {
public static fromCellValue(raw: InternalCellValue): Maybe<CriterionPackage> {
if (typeof raw !== 'string') {
return undefined
}
Expand Down
3 changes: 2 additions & 1 deletion src/interpreter/CriterionFunctionCompute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {AbsoluteCellRange} from '../AbsoluteCellRange'
import {CellError, ErrorType, InternalCellValue, simpleCellAddress} from '../Cell'
import {CriterionCache, DependencyGraph, RangeVertex} from '../DependencyGraph'
import {split} from '../generatorUtils'
import {Maybe} from '../Maybe'
import { CriterionLambda, CriterionPackage} from './Criterion'
import {Interpreter} from './Interpreter'
import { SimpleRangeValue} from './InterpreterValue'
Expand Down Expand Up @@ -78,7 +79,7 @@ export class CriterionFunctionCompute<T> {
}
}

private tryToGetRangeVertexForRangeValue(rangeValue: SimpleRangeValue): RangeVertex | undefined {
private tryToGetRangeVertexForRangeValue(rangeValue: SimpleRangeValue): Maybe<RangeVertex> {
const maybeRange = rangeValue.range()
if (maybeRange === undefined) {
return undefined
Expand Down
5 changes: 3 additions & 2 deletions src/interpreter/InterpreterValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {AbsoluteCellRange} from '../AbsoluteCellRange'
import {InternalCellValue} from '../Cell'
import {DependencyGraph} from '../DependencyGraph'
import {MatrixSize} from '../Matrix'
import {Maybe} from '../Maybe'

export class ArrayData {
constructor(
Expand Down Expand Up @@ -41,7 +42,7 @@ export class ArrayData {
}

export class OnlyRangeData {
public data: InternalCellValue[][] | undefined
public data: Maybe<InternalCellValue[][]>
public _hasOnlyNumbers?: boolean

constructor(
Expand Down Expand Up @@ -184,7 +185,7 @@ export class SimpleRangeValue {
return this.data.rawNumbers()
}

public range(): AbsoluteCellRange | undefined {
public range(): Maybe<AbsoluteCellRange> {
return this.data.range()
}

Expand Down
3 changes: 2 additions & 1 deletion src/interpreter/plugin/NumericAggregationPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'assert'
import {AbsoluteCellRange, DIFFERENT_SHEETS_ERROR} from '../../AbsoluteCellRange'
import {CellError, EmptyValue, ErrorType, InternalCellValue, SimpleCellAddress} from '../../Cell'
import {Maybe} from '../../Maybe'
import {AstNodeType, CellRangeAst, ProcedureAst} from '../../parser'
import {coerceNonDateScalarToMaybeNumber, coerceToRange} from '../coerce'
import { SimpleRangeValue} from '../InterpreterValue'
Expand Down Expand Up @@ -50,7 +51,7 @@ class AverageResult {
return new AverageResult(this.sum + other.sum, this.count + other.count)
}

public averageValue(): number | undefined {
public averageValue(): Maybe<number> {
if (this.count > 0) {
return this.sum / this.count
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/interpreter/plugin/SumifPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import {CellError, ErrorType, InternalCellValue, SimpleCellAddress} from '../../Cell'
import {Maybe} from '../../Maybe'
import { ProcedureAst} from '../../parser'
import {coerceToRange} from '../coerce'
import { CriterionPackage} from '../Criterion'
Expand All @@ -24,7 +25,7 @@ class AverageResult {
return new AverageResult(this.sum + other.sum, this.count + other.count)
}

public averageValue(): number | undefined {
public averageValue(): Maybe<number> {
if (this.count > 0) {
return this.sum / this.count
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/parser/Ast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {IToken} from 'chevrotain'
import {CellError, ErrorType} from '../Cell'
import {Maybe} from '../Maybe'
import {CellAddress} from './CellAddress'
import {IExtendedToken} from './FormulaParser'

Expand Down Expand Up @@ -346,7 +347,7 @@ export const buildParsingErrorAst = (): ErrorAst => ({
error: CellError.parsingError()
})

function extractImage(token: IToken | undefined): string | undefined {
function extractImage(token: Maybe<IToken>): Maybe<string> {
return token !== undefined ? token.image : undefined
}

Expand Down
Loading