Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove @gmod/binary-parser #46

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default [
'unicorn/number-literal-case': 0,
'unicorn/no-new-array': 0,
'unicorn/no-array-for-each': 0,
'unicorn/prevent-abbreviations': 0,
'unicorn/prefer-spread': 0,
'unicorn/prefer-string-replace-all': 0,
'@typescript-eslint/no-explicit-any': 0,
Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,18 @@
"biojs"
],
"dependencies": {
"@gmod/binary-parser": "^1.3.5",
"generic-filehandle": "^3.0.0",
"long": "^4.0.0"
"generic-filehandle": "^3.0.0"
},
"devDependencies": {
"@types/jest": "^29.2.4",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"@typescript-eslint/eslint-plugin": "^8.0.1",
"@typescript-eslint/parser": "^8.0.1",
"documentation": "^14.0.1",
"eslint": "^9.7.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-unicorn": "^54.0.0",
"eslint-plugin-unicorn": "^55.0.0",
"jest": "^29.3.1",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
Expand Down
13 changes: 13 additions & 0 deletions src/bigint-polyfill/polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getBigInt64, getBigUint64 } from './pure'

if (!('getBigInt64' in DataView)) {
DataView.prototype.getBigInt64 = function (byteOffset, littleEndian) {
return getBigInt64(this, byteOffset, littleEndian)
}
}

if (!('getBigUint64' in DataView)) {
DataView.prototype.getBigUint64 = function (byteOffset, littleEndian) {
return getBigUint64(this, byteOffset, littleEndian)
}
}
42 changes: 42 additions & 0 deletions src/bigint-polyfill/pure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const BigInt32 = BigInt(32)

export function getBigInt64(
dataView: DataView,
byteOffset: number,
littleEndian: boolean | undefined,
): bigint {
const littleEndianMask = Number(!!littleEndian)
const bigEndianMask = Number(!littleEndian)

return (
(BigInt(
dataView.getInt32(byteOffset, littleEndian) * bigEndianMask +
dataView.getInt32(byteOffset + 4, littleEndian) * littleEndianMask,
) <<
BigInt32) |
BigInt(
dataView.getUint32(byteOffset, littleEndian) * littleEndianMask +
dataView.getUint32(byteOffset + 4, littleEndian) * bigEndianMask,
)
)
}

export function getBigUint64(
dataView: DataView,
byteOffset: number,
littleEndian: boolean | undefined,
): bigint {
const a = dataView.getUint32(byteOffset, littleEndian)
const b = dataView.getUint32(byteOffset + 4, littleEndian)

const littleEndianMask = Number(!!littleEndian)
const bigEndianMask = Number(!littleEndian)

// This branch-less optimization is 77x faster than normal ternary operator.
// and only 3% slower than native implementation
// https://jsbench.me/p8kyhg1eqv/1
return (
(BigInt(a * bigEndianMask + b * littleEndianMask) << BigInt32) |
BigInt(a * littleEndianMask + b * bigEndianMask)
)
}
2 changes: 0 additions & 2 deletions src/declare.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './bigint-polyfill/polyfill'
export { default as TwoBitFile } from './twoBitFile'
Loading