Skip to content

Commit

Permalink
Tighten up.
Browse files Browse the repository at this point in the history
  • Loading branch information
karanlyons committed Jan 2, 2020
1 parent 7bb1ba7 commit 9cd6b5f
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 49 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
2520298415

// Hash buffers:
> const buf = new Uint8Array([...new Array(256).keys()]);
> const buf = new Uint8Array(Array.from({ length: 256}, (_, i) => i));
> murmurHash3.x86.hash32(buf);
3825864278
> murmurHash3.x86.hash128(buf);
Expand All @@ -47,7 +47,6 @@

```javascript
murmurHash3 = {
version: string,
strToBuf: (str: string = ""): Uint8Array,
bufToHex: (buf: Uint8Array = new Uint8Array(0)): string,
x86: {
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
"clean": "rm -rf ./lib ./coverage",
"lint": "tslint --project tsconfig.json --format stylish",
"test": "jest --config jestconfig.json --coverage",
"test:watch": "jest --config jestconfig.json --coverage --watch",
"build": "tsc",
"build:watch": "tsc --watch",
"travis": "jest --config jestconfig.json",
"coveralls": "jest --config jestconfig.json --silent --coverage --coverageReporters=text-lcov | coveralls",
"prepublishOnly": "npm run-script clean && npm run-script lint && npm run-script test && jq -e '.total | to_entries | map(.value.pct) | min == 100' ./coverage/coverage-summary.json && npm run-script build"
"prepublishOnly": "npm run clean && npm run lint && npm run test && jq -e '.total | to_entries | map(.value.pct) | min == 100' ./coverage/coverage-summary.json && npm run-script build"
},
"devDependencies": {
"@types/jest": "^24.0.25",
Expand Down
66 changes: 37 additions & 29 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ describe(
);

describe.each(
// tslint:disable-next-line: prefer-array-literal
[...new Array(256).keys()]
.map(i => [
Array.from(
{ length: 256 },
(_, i) => [
new Uint8Array([i, 255 - i]),
`${i.toString(16).padStart(2, "0")}${(255 - i).toString(16).padStart(2, "0")}`,
] as [Uint8Array, string]),
] as [Uint8Array, string],
),
)(
"bufToHex(Uint8Array %p)",
(buf, expected) => {
Expand All @@ -144,7 +145,6 @@ describe.each(Object.entries(testVectorsx86hash32)
.map(([k, v]) => [k, v] as [string, u32]),
)(
"x86.hash32(%j)",
// tslint:disable-next-line: variable-name
(str, expected) => {
test(`returns ${expected}`, () => {
expect(x86.hash32(str)).toBe(expected);
Expand All @@ -156,7 +156,6 @@ describe.each(Object.entries(testVectorsx86hash128str)
.map(([k, v]) => [k, v]),
)(
"x86.hash128(%j)",
// tslint:disable-next-line: variable-name
(str, expected) => {
test(`returns '${expected}'`, () => {
expect(x86.hash128(str)).toBe(expected);
Expand All @@ -179,7 +178,6 @@ describe.each(Object.entries(testVectorsx64hash128str)
.map(([k, v]) => [k, v]),
)(
"x64.hash128(%j)",
// tslint:disable-next-line: variable-name
(str, expected) => {
test(`returns '${expected}'`, () => {
expect(x64.hash128(str)).toBe(expected);
Expand All @@ -203,10 +201,12 @@ describe.each(
Object.entries(testVectorsx86hash32)
.slice(0, -1)
.flatMap(([k, v]) => (
// tslint:disable-next-line: prefer-array-literal
[...new Array(Math.max(1, k.length - 1)).keys()].map(i => [
k, i + 1, chunk(k, i + 1), v,
] as [string, number, string[], u32])
Array.from(
{ length: Math.max(1, k.length - 1) },
(_, i) => [
k, i + 1, chunk(k, i + 1), v,
] as [string, number, string[], u32],
)
)),
)(
"x86.hash32(chunk(%j, %p))",
Expand All @@ -228,12 +228,14 @@ describe.each(
Object.entries(testVectorsx86hash128str)
.slice(0, -1)
.flatMap(([k, v]) => (
// tslint:disable-next-line: prefer-array-literal
[...new Array(Math.max(1, k.length - 1)).keys()].map(i => [
k, i + 1, chunk(k, i + 1), v,
] as [string, number, string[], string])
),
))(
Array.from(
{ length: Math.max(1, k.length - 1) },
(_, i) => [
k, i + 1, chunk(k, i + 1), v,
] as [string, number, string[], string],
)
)),
)(
"x86.hash128(chunk(%j, %p))",
// tslint:disable-next-line: variable-name
(_k, _size, chunks, expected) => {
Expand All @@ -252,10 +254,12 @@ describe.each(
Object.entries(testVectorsx86hash128buf)
.map(([k, v]) => ([k, strToBuf(k), v] as const))
.flatMap(([k, b, v]) => (
// tslint:disable-next-line: prefer-array-literal
[...new Array(Math.max(1, b.byteLength - 1)).keys()].map(i => [
k, i + 1, chunk(b, i + 1), v,
] as [string, number, Uint8Array[], Uint8Array])
Array.from(
{ length: Math.max(1, b.byteLength - 1) },
(_, i) => [
k, i + 1, chunk(b, i + 1), v,
] as [string, number, Uint8Array[], Uint8Array],
)
)),
)(
"x86.hash128(chunk(strToBuf(%j), %p))",
Expand All @@ -276,10 +280,12 @@ describe.each(
Object.entries(testVectorsx64hash128str)
.slice(0, -1)
.flatMap(([k, v]) => (
// tslint:disable-next-line: prefer-array-literal
[...new Array(Math.max(1, k.length - 1)).keys()].map(i => [
k, i + 1, chunk(k, i + 1), v,
] as [string, number, string[], string])
Array.from(
{ length: Math.max(1, k.length - 1) },
(_, i) => [
k, i + 1, chunk(k, i + 1), v,
] as [string, number, string[], string],
)
)),
)(
"x64.hash128(chunk(%j, %p))",
Expand All @@ -300,10 +306,12 @@ describe.each(
Object.entries(testVectorsx64hash128buf)
.map(([k, v]) => ([k, strToBuf(k), v] as const))
.flatMap(([k, b, v]) => (
// tslint:disable-next-line: prefer-array-literal
[...new Array(Math.max(1, b.byteLength - 1)).keys()].map(i => [
k, i + 1, chunk(b, i + 1), v,
] as [string, number, Uint8Array[], Uint8Array])
Array.from(
{ length: Math.max(1, b.byteLength - 1) },
(_, i) => [
k, i + 1, chunk(b, i + 1), v,
] as [string, number, Uint8Array[], Uint8Array],
)
)),
)(
"x64.hash128(chunk(strToBuf(%j), %p))",
Expand Down
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/


export const version = '3.0.0';

type Brand<Name, Type> = Type & {_type?: Name};
export type u32 = Brand<'u32', number>;
export type u64 = Brand<'u64', [u32, u32]>;
Expand All @@ -19,8 +17,7 @@ type u64spill = Brand<'u64spill', [u32, u32, u32, u32]>;

export const strToBuf = TextEncoder.prototype.encode.bind(new TextEncoder());

// tslint:disable-next-line: prefer-array-literal
const hexLUT = [...new Array(256).keys()].map(i => `00${i.toString(16)}`.slice(-2));
const hexLUT = Array.from({ length: 256 }, (_, i) => `00${i.toString(16)}`.slice(-2));

export function bufToHex(buf: Uint8Array = new Uint8Array(0)): string {
let str = "";
Expand Down
Loading

0 comments on commit 9cd6b5f

Please sign in to comment.