From 3bee6de208618fcea9cf07ead22e3d5ca1cfd200 Mon Sep 17 00:00:00 2001 From: Jimmy Cao Date: Thu, 25 Apr 2019 15:47:18 -0700 Subject: [PATCH] toml: remove parseFile (denoland/deno_std#361) Original: https://github.com/denoland/deno_std/commit/0431b2f92fba69582f4412bfe9ad138838f46275 --- toml/README.md | 5 +++-- toml/parser.ts | 20 +++++--------------- toml/parser_test.ts | 12 +++++++++++- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/toml/README.md b/toml/README.md index f4ae258578..e30d972f3f 100644 --- a/toml/README.md +++ b/toml/README.md @@ -94,9 +94,10 @@ will output: ### Parse ```ts -import { parseFile, parse } from "./parser.ts"; +import { parse } from "./parser.ts"; +import { readFileStrSync } from "../fs/read_file_str.ts"; -const tomlObject = parseFile("file.toml"); +const tomlObject = parse(readFileStrSync("file.toml")); const tomlString = 'foo.bar = "Deno"'; const tomlObject22 = parse(tomlString); diff --git a/toml/parser.ts b/toml/parser.ts index c615c3c1e8..cc96322fb2 100644 --- a/toml/parser.ts +++ b/toml/parser.ts @@ -1,6 +1,4 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { existsSync } from "../fs/exists.ts"; -import { readFileStrSync } from "../fs/read_file_str.ts"; import { deepAssign } from "../util/deep_assign.ts"; import { pad } from "../strings/pad.ts"; @@ -275,12 +273,12 @@ class Parser { _parseDeclarationName(declaration: string): string[] { const out = []; let acc = []; - let inLitteral = false; + let inLiteral = false; for (let i = 0; i < declaration.length; i++) { const c = declaration[i]; switch (c) { case ".": - if (!inLitteral) { + if (!inLiteral) { out.push(acc.join("")); acc = []; } else { @@ -288,10 +286,10 @@ class Parser { } break; case `"`: - if (inLitteral) { - inLitteral = false; + if (inLiteral) { + inLiteral = false; } else { - inLitteral = true; + inLiteral = true; } break; default: @@ -538,11 +536,3 @@ export function parse(tomlString: string): object { tomlString = tomlString.replace(/\r\n/g, "\n").replace(/\\\n/g, "\n"); return new Parser(tomlString).parse(); } - -export function parseFile(filePath: string): object { - if (!existsSync(filePath)) { - throw new Error("File not found"); - } - const strFile = readFileStrSync(filePath); - return parse(strFile); -} diff --git a/toml/parser_test.ts b/toml/parser_test.ts index 03c7337141..4b53945f4c 100644 --- a/toml/parser_test.ts +++ b/toml/parser_test.ts @@ -1,10 +1,20 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { test } from "../testing/mod.ts"; import { assertEquals } from "../testing/asserts.ts"; -import { parseFile, stringify } from "./parser.ts"; +import { existsSync } from "../fs/exists.ts"; +import { readFileStrSync } from "../fs/read_file_str.ts"; +import { parse, stringify } from "./parser.ts"; import * as path from "../fs/path/mod.ts"; const testFilesDir = path.resolve("toml", "testdata"); +function parseFile(filePath: string): object { + if (!existsSync(filePath)) { + throw new Error(`File not found: ${filePath}`); + } + const strFile = readFileStrSync(filePath); + return parse(strFile); +} + test({ name: "[TOML] Strings", fn(): void {