Skip to content

Commit

Permalink
RawCellType extended (#168)
Browse files Browse the repository at this point in the history
* RawCellType extended

* Update test/CellContentParser.spec.ts

Co-Authored-By: Wojciech Czerniak <wojciech.czerniak@gmail.com>

Co-authored-by: Wojciech Czerniak <wojciech.czerniak@gmail.com>
  • Loading branch information
izulin and wojciechczerniak committed Feb 17, 2020
1 parent 2ebe535 commit be57f95
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/CellContentParser.ts
@@ -1,8 +1,8 @@
import {CellError, ErrorType} from './Cell'
import {CellError, EmptyValue, EmptyValueType, ErrorType} from './Cell'
import {Config} from './Config'
import {DateHelper} from './DateHelper'

export type RawCellContent = string | null | undefined
export type RawCellContent = string | number | boolean | EmptyValueType | null | undefined

export namespace CellContent {
export class Number {
Expand All @@ -13,6 +13,10 @@ export namespace CellContent {
constructor(public readonly value: string) { }
}

export class Boolean {
constructor(public readonly value: boolean) { }
}

export class Empty {

public static getSingletonInstance() {
Expand All @@ -39,7 +43,7 @@ export namespace CellContent {
}
}

export type Type = Number | String | Empty | Formula | MatrixFormula | Error
export type Type = Number | String | Boolean | Empty | Formula | MatrixFormula | Error
}

/**
Expand Down Expand Up @@ -68,10 +72,13 @@ export class CellContentParser {
constructor(private readonly config: Config, private readonly dateHelper: DateHelper) {}

public parse(content: RawCellContent): CellContent.Type {
if (content === undefined || content === null) {
if (content === undefined || content === null || content === EmptyValue) {
return CellContent.Empty.getSingletonInstance()
}
if (isMatrix(content)) {
} else if (typeof content === 'number') {
return new CellContent.Number(content)
} else if (typeof content === 'boolean') {
return new CellContent.Boolean(content)
} else if (isMatrix(content)) {
return new CellContent.MatrixFormula(content.substr(1, content.length - 2))
} else if (isFormula(content)) {
return new CellContent.Formula(content)
Expand Down
8 changes: 7 additions & 1 deletion test/CellContentParser.spec.ts
@@ -1,4 +1,4 @@
import {Config} from '../src'
import {Config, EmptyValue} from '../src'
import {ErrorType} from '../src/Cell'
import {CellContent, CellContentParser} from '../src/CellContentParser'
import {DateHelper} from '../src/DateHelper'
Expand Down Expand Up @@ -37,6 +37,12 @@ describe('CellContentParser', () => {
expect(cellContentParser.parse('+42.13')).toStrictEqual(new CellContent.Number(42.13))
})

it( 'non-string', () => {
expect(cellContentParser.parse(42)).toStrictEqual(new CellContent.Number(42))
expect(cellContentParser.parse(true)).toStrictEqual(new CellContent.Boolean(true))
expect(cellContentParser.parse(EmptyValue)).toStrictEqual(new CellContent.Empty())
})

it('string', () => {
expect(cellContentParser.parse('f42')).toStrictEqual(new CellContent.String('f42'))
expect(cellContentParser.parse('42f')).toStrictEqual(new CellContent.String('42f'))
Expand Down
10 changes: 10 additions & 0 deletions test/engine.spec.ts
Expand Up @@ -389,4 +389,14 @@ describe('Integration', () => {
expect(error.type).toEqual(ErrorType.VALUE)
expect(error.value).toEqual('#ARG!')
})

it('should correctly parse all JS types', () => {
const engine = HyperFormula.buildFromArray([
[1, true, EmptyValue],
]);

expect(engine.getCellValue(adr('A1'))).toBe(1)
expect(engine.getCellValue(adr('B1'))).toBe(true)
expect(engine.getCellValue(adr('C1'))).toBe(EmptyValue)
})
})

0 comments on commit be57f95

Please sign in to comment.