Skip to content

Commit

Permalink
fix: improve null handling, as required by noUncheckedIndexedAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Oct 12, 2022
1 parent 3c2ef46 commit d6b79ec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
14 changes: 8 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import {notNull} from "@softwareventures/nullable";

export type Table = string[][];

export type ReadonlyTable = ReadonlyArray<ReadonlyArray<string>>;

export function* tableToRecords(table: ReadonlyTable): Iterable<Record<string, string>> {
const header = table[0];
const header = notNull(table[0]);

for (let i = 1; i < table.length; ++i) {
const row = table[i];
const row = notNull(table[i]);
const record: Record<string, string> = {};

for (let j = 0; j < row.length && j < header.length; ++j) {
const name = header[j];
record[name] = row[j];
const name = notNull(header[j]);
record[name] = notNull(row[j]);
}

yield record;
Expand All @@ -23,14 +25,14 @@ export function recordsToTable(records: ReadonlyArray<Readonly<Record<string, st
return [];
}

const headers = Object.keys(records[0]);
const headers = Object.keys(notNull(records[0]));

return [headers]
.concat(records
.map(record => headers
.map(header => {
if (header in record) {
return record[header];
return notNull(record[header]);
} else {
throw new Error("Inconsistent records.");
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"node": "^14 || ^16 || >=18"
},
"dependencies": {
"@softwareventures/nullable": "^3.0.0",
"tslib": "^1.9.3 || ^2.0.0 || ^2.1.0"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,13 @@
lodash "^4.17.4"
read-pkg-up "^7.0.0"

"@softwareventures/nullable@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@softwareventures/nullable/-/nullable-3.0.0.tgz#979045c99e7c8a9055cb25c3c5cb5b9edfc50085"
integrity sha512-kgYOGJHzgZl0QGqMqbRe34i5VaRQUsG+X9AcblJL1lGDq+npLsCS974VSJmOBxnSqIQ5dcgah+zBxHndH8qzfg==
dependencies:
tslib "2.4.0"

"@softwareventures/[email protected]":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@softwareventures/semantic-release-config/-/semantic-release-config-3.0.0.tgz#11bc1edbe4de1cd37a79dcf050d5be6e8467ada0"
Expand Down

0 comments on commit d6b79ec

Please sign in to comment.