Skip to content

Commit

Permalink
fix: exactOptionalPropertyTypes TS option compatibility (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold authored Nov 25, 2023
1 parent 264adfb commit 31b129a
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.parcel-cache
tsconfig.test.json
/lib/
/node_modules/
/test-results/
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@semantic-release/changelog": "6.0.3",
"@semantic-release/git": "10.0.1",
"@tsconfig/node18": "18.2.0",
"@tsconfig/strictest": "2.0.2",
"@types/chai": "4.3.5",
"@types/mocha": "10.0.1",
"@types/mocha-each": "2.0.0",
Expand All @@ -72,8 +73,15 @@
"build:browser": "npm run prepare:browser && tsc -P tsconfig.browser.json",
"test": "npm run test:unit && npm run test:esm",
"test:unit": "npm run test:unit:node && npm run test:unit:browser",
"test:unit:node": "npm run prepare:node && mocha",
"test:unit:browser": "npm run prepare:browser && TS_NODE_PROJECT='./tsconfig.browser.json' mocha",

"pretest:unit:node": "tsc --project tsconfig.json --showConfig > tsconfig.test.json",
"test:unit:node": "npm run prepare:node && TS_NODE_PROJECT='./tsconfig.test.json' mocha",
"posttest:unit:node": "rimraf tsconfig.test.json",

"pretest:unit:browser": "tsc --project tsconfig.browser.json --showConfig > tsconfig.test.json",
"test:unit:browser": "npm run prepare:browser && TS_NODE_PROJECT='./tsconfig.test.json' mocha",
"posttest:unit:browser": "rimraf tsconfig.test.json",

"test:esm": "npm run test:esm:node && npm run test:esm:browser",
"test:esm:node": "node --input-type=module -e \"import MDBReader from './lib/node/index.js';\"",
"test:esm:browser": "node --input-type=module -e \"import MDBReader from './lib/browser/index.js';\"",
Expand Down
2 changes: 1 addition & 1 deletion src/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class Database {
const dateValue = this.#databaseDefinitionPage.readDoubleLE(this.#format.databaseDefinitionPage.creationDateOffset);
mask.writeInt32LE(Math.floor(dateValue));
for (let i = 0; i < mask.length; ++i) {
mask[i] = mask[i % 4];
mask[i] = mask[i % 4]!;
}
return mask;
}
Expand Down
24 changes: 11 additions & 13 deletions src/MDBReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MSYS_OBJECTS_TABLE = "MSysObjects";
const MSYS_OBJECTS_PAGE = 2;

export interface Options {
password?: string;
password?: string | undefined;
}

export default class MDBReader {
Expand All @@ -19,7 +19,7 @@ export default class MDBReader {
/**
* @param buffer Buffer of the database.
*/
constructor(buffer: Buffer, { password }: Options = {}) {
constructor(buffer: Buffer, { password }: Options | undefined = {}) {
this.#buffer = buffer;

assertPageType(this.#buffer, PageType.DatabaseDefinitionPage);
Expand Down Expand Up @@ -74,17 +74,15 @@ export default class MDBReader {
* @param systemTables Includes system tables. Default false.
* @param linkedTables Includes linked tables. Default false.
*/
getTableNames(
{
normalTables,
systemTables,
linkedTables,
}: {
normalTables: boolean;
systemTables: boolean;
linkedTables: boolean;
} = { normalTables: true, systemTables: false, linkedTables: false }
): string[] {
getTableNames({
normalTables = true,
systemTables = false,
linkedTables = false,
}: {
normalTables?: boolean | undefined;
systemTables?: boolean | undefined;
linkedTables?: boolean | undefined;
} = {}): string[] {
const filteredSysObjects: SysObject[] = [];
for (const sysObject of this.#sysObjects) {
if (sysObject.objectType === SysObjectTypes.Table) {
Expand Down
28 changes: 15 additions & 13 deletions src/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export class Table {
#columnCount: number;

#variableColumnCount: number;
#fixedColumnCount: number;
// #fixedColumnCount: number;

#logicalIndexCount: number;
// #logicalIndexCount: number;
#realIndexCount: number;

/**
Expand Down Expand Up @@ -69,11 +69,11 @@ export class Table {
this.#variableColumnCount = this.#definitionBuffer.readUInt16LE(
this.#database.format.tableDefinitionPage.variableColumnCountOffset
);
this.#fixedColumnCount = this.#columnCount - this.#variableColumnCount;
// this.#fixedColumnCount = this.#columnCount - this.#variableColumnCount;

this.#logicalIndexCount = this.#definitionBuffer.readInt32LE(
this.#database.format.tableDefinitionPage.logicalIndexCountOffset
);
// this.#logicalIndexCount = this.#definitionBuffer.readInt32LE(
// this.#database.format.tableDefinitionPage.logicalIndexCountOffset
// );
this.#realIndexCount = this.#definitionBuffer.readInt32LE(
this.#database.format.tableDefinitionPage.realIndexCountOffset
);
Expand Down Expand Up @@ -204,11 +204,13 @@ export class Table {
* @param rowLimit Maximum number of rows to be returned. Defaults to Infinity.
*/
getData<TRow extends { [column in TColumn]: Value }, TColumn extends string = string>(
options: {
columns?: ReadonlyArray<string>;
rowOffset?: number;
rowLimit?: number;
} = {}
options:
| {
columns?: ReadonlyArray<string> | undefined;
rowOffset?: number | undefined;
rowLimit?: number | undefined;
}
| undefined = {}
): TRow[] {
const columnDefinitions = this.#getColumnDefinitions();

Expand Down Expand Up @@ -363,9 +365,9 @@ export class Table {
size = column.size;
++fixedColumnsFound;
} else if (!column.fixedLength && column.variableIndex < rowVariableColumnCount) {
const colStart = variableColumnOffsets[column.variableIndex];
const colStart = variableColumnOffsets[column.variableIndex]!;
start = recordStart + colStart;
size = variableColumnOffsets[column.variableIndex + 1] - colStart;
size = variableColumnOffsets[column.variableIndex + 1]! - colStart;
} else {
start = 0;
value = null;
Expand Down
10 changes: 5 additions & 5 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export function doCarry(values: ReadonlyArray<number>): number[] {
const length = result.length;

for (let i = 0; i < length - 1; ++i) {
result[i + 1] += Math.floor(result[i] / 10);
result[i] = result[i] % 10;
result[i + 1] += Math.floor(result[i]! / 10);
result[i] = result[i]! % 10;
}
result[length - 1] = result[length - 1] % 10;
result[length - 1] = result[length - 1]! % 10;

return result;
}
Expand All @@ -20,7 +20,7 @@ export function multiplyArray(a: ReadonlyArray<number>, b: ReadonlyArray<number>
for (let i = 0; i < a.length; ++i) {
if (a[i] === 0) continue;
for (let j = 0; j < b.length; j++) {
result[i + j] += a[i] * b[j];
result[i + j] += a[i]! * b[j]!;
}
}
return doCarry(result.slice(0, a.length));
Expand All @@ -34,7 +34,7 @@ export function addArray(a: ReadonlyArray<number>, b: ReadonlyArray<number>): nu

const result: number[] = [];
for (let i = 0; i < length; ++i) {
result[i] = a[i] + b[i];
result[i] = a[i]! + b[i]!;
}
return doCarry(result);
}
Expand Down
1 change: 0 additions & 1 deletion src/codec-handler/handlers/office/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export function createOfficeCodecHandler(databaseDefinitionPage: Buffer, passwor
throw new Error("Unknown encryption");
}
}
break;

case "1.1":
// RC4 Encryption: 1.1
Expand Down
10 changes: 5 additions & 5 deletions src/crypto/rc4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export function createRC4Decrypter(key: Buffer): (data: Buffer) => Buffer {
*/
for (let k = 0; k < data.length; ++k) {
i = (i + 1) % 256;
j = (j + S[i]) % 256;
[S[i], S[j]] = [S[j], S[i]];
resultBuffer[k] ^= S[(S[i] + S[j]) % 256];
j = (j + S[i]!) % 256;
[S[i], S[j]] = [S[j]!, S[i]!];
resultBuffer[k] ^= S[(S[i]! + S[j]!) % 256]!;
}

return resultBuffer;
Expand All @@ -39,8 +39,8 @@ function createKeyStream(key: Buffer): Uint8Array {
}
let j = 0;
for (let i = 0; i < 256; ++i) {
j = (j + S[i] + key[i % key.length]) % 256;
[S[i], S[j]] = [S[j], S[i]];
j = (j + S[i]! + key[i % key.length]!) % 256;
[S[i], S[j]] = [S[j]!, S[i]!];
}

return S;
Expand Down
4 changes: 2 additions & 2 deletions src/data/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export function readCurrency(buffer: Buffer): string {
const bytes = buffer.slice(0, bytesCount);

let negative = false;
if (bytes[bytesCount - 1] & 0x80) {
if (bytes[bytesCount - 1]! & 0x80) {
negative = true;
for (let i = 0; i < bytesCount; ++i) {
bytes[i] = ~bytes[i];
bytes[i] = ~bytes[i]!;
}
for (let i = 0; i < bytesCount; ++i) {
++bytes[i];
Expand Down
4 changes: 2 additions & 2 deletions src/data/numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export function readNumeric(buffer: Buffer, column: Pick<Column, "scale" | "prec

const bytes = buffer.slice(1, 17);
for (let i = 0; i < bytes.length; ++i) {
const byte = bytes[12 - 4 * Math.floor(i / 4) + (i % 4)];
const byte = bytes[12 - 4 * Math.floor(i / 4) + (i % 4)]!;
product = addArray(product, multiplyArray(multiplier, toArray(byte, MAX_PRECISION)));
multiplier = multiplyArray(multiplier, toArray(256, MAX_PRECISION));
}

const negative = !!(buffer[0] & 0x80);
const negative = !!(buffer[0]! & 0x80);
return buildValue(
product,
// Scale is always set for numeric columns
Expand Down
2 changes: 1 addition & 1 deletion src/data/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function buildValue(array: ReadonlyArray<number>, scale: number, negative
if (i === scale) {
value += ".";
}
value += array[i - 1].toString();
value += array[i - 1]!.toString();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/dependencies/iconv-lite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export function decodeWindows1252(buffer: Buffer): string {
const result = Buffer.alloc(buffer.length * 2);

for (let i = 0; i < buffer.length; ++i) {
const index = buffer[i] * 2;
result[i * 2] = charsBuffer[index];
result[i * 2 + 1] = charsBuffer[index + 1];
const index = buffer[i]! * 2;
result[i * 2] = charsBuffer[index]!;
result[i * 2 + 1] = charsBuffer[index + 1]!;
}

return result.toString("ucs2");
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { Column } from "./column.js";
export { default, Options } from "./MDBReader.js";
export { default, type Options } from "./MDBReader.js";
export type { Table } from "./Table.js";
export * from "./types.js";
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export function getBitmapValue(bitmap: Buffer, pos: number): boolean {
const byteNumber = Math.floor(pos / 8);
const bitNumber = pos % 8;
return !!(bitmap[byteNumber] & (1 << bitNumber));
return !!(bitmap[byteNumber]! & (1 << bitNumber));
}

/**
Expand All @@ -24,7 +24,7 @@ export function xor(a: Buffer, b: Buffer) {
const buffer = Buffer.allocUnsafe(length);

for (let i = 0; i < length; i++) {
buffer[i] = a[i] ^ b[i];
buffer[i] = a[i]! ^ b[i]!;
}

return buffer;
Expand Down
4 changes: 2 additions & 2 deletions test/bigint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ describe("BigInt", () => {

expect(rows.length).to.eq(1);

const row = rows[0];
expect(row.Numeric).to.eq(42n);
const row = rows[0]!;
expect(row['Numeric']).to.eq(42n);
});
});
});
4 changes: 2 additions & 2 deletions test/currency.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe("Currency", () => {

expect(rows.length).to.eq(1);

const row = rows[0];
expect(row.Money).to.eq("42.0000");
const row = rows[0]!;
expect(row['Money']).to.eq("42.0000");
});
});
});
2 changes: 1 addition & 1 deletion test/longtext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ describe("LongText", () => {
const buffer = readFileSync(path);
const reader = new MDBReader(buffer);
const data = reader.getTable("Table1").getData();
expect(data[0].LongText).to.have.length(5000);
expect(data[0]!['LongText']).to.have.length(5000);
});
});
14 changes: 7 additions & 7 deletions test/numeric.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ describe("Numeric", () => {

expect(rows.length).to.eq(1);

const row = rows[0];
expect(row.col2).to.eq("1");
expect(row.col3).to.eq("0");
expect(row.col4).to.eq("0");
expect(row.col5).to.eq("4");
expect(row.col6).to.eq("-1");
expect(row.col7).to.eq("1");
const row = rows[0]!;
expect(row['col2']).to.eq("1");
expect(row['col3']).to.eq("0");
expect(row['col4']).to.eq("0");
expect(row['col5']).to.eq("4");
expect(row['col6']).to.eq("-1");
expect(row['col7']).to.eq("1");
});
}
);
Expand Down
Loading

0 comments on commit 31b129a

Please sign in to comment.