summaryrefslogtreecommitdiff
path: root/std/encoding
diff options
context:
space:
mode:
authorYusuke Sakurai <kerokerokerop@gmail.com>2020-02-09 05:15:59 +0900
committerGitHub <noreply@github.com>2020-02-08 14:15:59 -0600
commita4bb8bab44cc778e6e455f19fdae9f3e4acc809b (patch)
treedcd78cd45f493588b04d3949aeb7c57b8d53d92e /std/encoding
parentcdba5ab6fc633606aaa6f95d0825832c3ac6fe5c (diff)
remove non-null assertion operator from std (part2) (#3927)
Diffstat (limited to 'std/encoding')
-rw-r--r--std/encoding/csv.ts25
-rw-r--r--std/encoding/csv_test.ts4
-rw-r--r--std/encoding/toml.ts20
3 files changed, 32 insertions, 17 deletions
diff --git a/std/encoding/csv.ts b/std/encoding/csv.ts
index cc486641b..1314afcaa 100644
--- a/std/encoding/csv.ts
+++ b/std/encoding/csv.ts
@@ -5,6 +5,7 @@
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { StringReader } from "../io/readers.ts";
+import { assert } from "../testing/asserts.ts";
const INVALID_RUNE = ["\r", "\n", '"'];
@@ -37,11 +38,15 @@ export interface ReadOptions {
}
function chkOptions(opt: ReadOptions): void {
- if (!opt.comma) opt.comma = ",";
- if (!opt.trimLeadingSpace) opt.trimLeadingSpace = false;
+ if (!opt.comma) {
+ opt.comma = ",";
+ }
+ if (!opt.trimLeadingSpace) {
+ opt.trimLeadingSpace = false;
+ }
if (
- INVALID_RUNE.includes(opt.comma!) ||
- INVALID_RUNE.includes(opt.comment!) ||
+ INVALID_RUNE.includes(opt.comma) ||
+ INVALID_RUNE.includes(opt.comment) ||
opt.comma === opt.comment
) {
throw new Error("Invalid Delimiter");
@@ -81,7 +86,8 @@ async function read(
return [];
}
- result = line.split(opt.comma!);
+ assert(opt.comma != null);
+ result = line.split(opt.comma);
let quoteError = false;
result = result.map((r): string => {
@@ -141,7 +147,7 @@ export async function readMatrix(
}
if (lineResult.length > 0) {
- if (_nbFields! && _nbFields! !== lineResult.length) {
+ if (_nbFields && _nbFields !== lineResult.length) {
throw new ParseError(lineIndex, lineIndex, "wrong number of fields");
}
result.push(lineResult);
@@ -215,7 +221,9 @@ export async function parse(
);
}
} else {
- headers = r.shift()!.map(
+ const head = r.shift();
+ assert(head != null);
+ headers = head.map(
(e): HeaderOptions => {
return {
name: e
@@ -245,7 +253,8 @@ export async function parse(
});
}
if (opt.parse) {
- return r.map((e: string[]): unknown => opt.parse!(e));
+ assert(opt.parse != null, "opt.parse must be set");
+ return r.map((e: string[]): unknown => opt.parse(e));
}
return r;
}
diff --git a/std/encoding/csv_test.ts b/std/encoding/csv_test.ts
index daa30d0fb..6c726835e 100644
--- a/std/encoding/csv_test.ts
+++ b/std/encoding/csv_test.ts
@@ -477,7 +477,7 @@ for (const t of testCases) {
if (t.Error) {
let err;
try {
- actual = await readMatrix(new BufReader(new StringReader(t.Input!)), {
+ actual = await readMatrix(new BufReader(new StringReader(t.Input)), {
comma: comma,
comment: comment,
trimLeadingSpace: trim,
@@ -490,7 +490,7 @@ for (const t of testCases) {
assert(err);
assertEquals(err.message, t.Error);
} else {
- actual = await readMatrix(new BufReader(new StringReader(t.Input!)), {
+ actual = await readMatrix(new BufReader(new StringReader(t.Input)), {
comma: comma,
comment: comment,
trimLeadingSpace: trim,
diff --git a/std/encoding/toml.ts b/std/encoding/toml.ts
index 6af31a1a1..53b907fbf 100644
--- a/std/encoding/toml.ts
+++ b/std/encoding/toml.ts
@@ -1,6 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { deepAssign } from "../util/deep_assign.ts";
import { pad } from "../strings/pad.ts";
+import { assert } from "../testing/asserts.ts";
class KeyValuePair {
constructor(public key: string, public value: unknown) {}
@@ -138,15 +139,16 @@ class Parser {
}
}
_groupToOutput(): void {
- const arrProperty = this.context
- .currentGroup!.name.replace(/"/g, "")
+ assert(this.context.currentGroup != null, "currentGroup must be set");
+ const arrProperty = this.context.currentGroup.name
+ .replace(/"/g, "")
.replace(/'/g, "")
.split(".");
let u = {};
- if (this.context.currentGroup!.type === "array") {
- u = this._unflat(arrProperty, this.context.currentGroup!.arrValues);
+ if (this.context.currentGroup.type === "array") {
+ u = this._unflat(arrProperty, this.context.currentGroup.arrValues);
} else {
- u = this._unflat(arrProperty, this.context.currentGroup!.objValues);
+ u = this._unflat(arrProperty, this.context.currentGroup.objValues);
}
deepAssign(this.context.output, u);
delete this.context.currentGroup;
@@ -170,10 +172,14 @@ class Parser {
}
let type;
- let name = line.match(captureReg)![1];
+ let m = line.match(captureReg);
+ assert(m != null, "line mut be matched");
+ let name = m[1];
if (name.match(/\[.*\]/)) {
type = "array";
- name = name.match(captureReg)![1];
+ m = name.match(captureReg);
+ assert(m != null, "name must be matched");
+ name = m[1];
} else {
type = "object";
}