From 24a936454e3c565feab9ce3fc04c70a9bb2d7b5c Mon Sep 17 00:00:00 2001 From: mirumi Date: Mon, 23 Oct 2023 17:55:51 +0900 Subject: [PATCH 1/2] [test]: Add error handlings in data parsing --- src/app/services/parse-file.service.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/services/parse-file.service.spec.ts b/src/app/services/parse-file.service.spec.ts index b56482d..6f7129c 100644 --- a/src/app/services/parse-file.service.spec.ts +++ b/src/app/services/parse-file.service.spec.ts @@ -14,7 +14,7 @@ describe("ParseFileService", () => { } public override synonymMapping: { [key: string]: string } = {} - public override json(value: string): KLineSource { + public override json(value: string): Result { return super.json(value) } @@ -70,7 +70,7 @@ describe("ParseFileService", () => { volume: 233.359, }, ] - expect(service.json(_1)).toEqual(_1_e) + expect(service.json(_1).val).toEqual(_1_e) const _2 = `[ { @@ -108,7 +108,7 @@ describe("ParseFileService", () => { volume: 233.359, }, ] - expect(service.json(_2)).toEqual(_2_e) + expect(service.json(_2).val).toEqual(_2_e) }) test("raw()", () => {}) From b6d683f07aa6e8cfb59327f05c9eff04a5fa726f Mon Sep 17 00:00:00 2001 From: mirumi Date: Sun, 26 Nov 2023 15:40:53 +0900 Subject: [PATCH 2/2] Fix parsing directly bug --- src/app/services/parse-file.service.spec.ts | 3 +-- src/app/services/parse-file.service.ts | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/app/services/parse-file.service.spec.ts b/src/app/services/parse-file.service.spec.ts index 6f7129c..af54764 100644 --- a/src/app/services/parse-file.service.spec.ts +++ b/src/app/services/parse-file.service.spec.ts @@ -1,4 +1,5 @@ import { Result } from "ts-results" + import { KLineSource } from "../shared/types" import { ParseFileService } from "./parse-file.service" @@ -111,8 +112,6 @@ describe("ParseFileService", () => { expect(service.json(_2).val).toEqual(_2_e) }) - test("raw()", () => {}) - test("extractKLineFromParsed()", () => { const _1 = [ ["1696497780000", "27687.9", "27718.5", "27687.9", "27698.5", "1041.273"], diff --git a/src/app/services/parse-file.service.ts b/src/app/services/parse-file.service.ts index ee2f52f..2c0e608 100644 --- a/src/app/services/parse-file.service.ts +++ b/src/app/services/parse-file.service.ts @@ -24,7 +24,7 @@ export class ParseFileService { // csv, tsv, txt: As is (TOHLCV is assumed to be in order) if (file.ext !== "json") { - if (!/^[0-9,\s\[\]]+$/.test(file.value)) { + if (!/^[0-9,\.\s\[\]]+$/.test(file.value)) { return new Err(new ParseError(`${file.name}.${file.ext}`)) } } @@ -143,7 +143,7 @@ export class ParseFileService { } protected raw(value: string): Result { - const parsed = Papa.parse(value, { delimiter: "," }) + const parsed = Papa.parse(value) if (0 < parsed.errors.length) { return new Err(Error()) } @@ -163,6 +163,8 @@ export class ParseFileService { ) } else if (value.includes(",")) { result = parsed.data as Array> + } else if (value.includes("\t")) { + result = parsed.data as Array> } else { return new Err(Error()) }