Skip to content

Commit 373eac4

Browse files
committed
chore: lint and format with prettier
1 parent b17fa41 commit 373eac4

File tree

10 files changed

+324
-192
lines changed

10 files changed

+324
-192
lines changed

.prettierrc

Whitespace-only changes.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"scripts": {
2323
"build": "unbuild",
2424
"dev": "vitest dev",
25-
"lint": "eslint --ext .ts,.js,.mjs,.cjs .",
25+
"lint": "eslint --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
26+
"lint:fix": "eslint --fix --ext .ts,.js,.mjs,.cjs . && prettier -w src test",
2627
"prepack": "unbuild",
2728
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
2829
"test": "pnpm lint && vitest run"
@@ -33,6 +34,7 @@
3334
"c8": "^7.13.0",
3435
"eslint": "^8.38.0",
3536
"eslint-config-unjs": "^0.1.0",
37+
"prettier": "^2.8.7",
3638
"standard-version": "^9.5.0",
3739
"typescript": "^5.0.4",
3840
"unbuild": "^1.2.1",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/crypto/core.ts

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@ export class WordArray {
44
words: number[];
55
sigBytes: number;
66

7-
constructor (words?, sigBytes?) {
7+
constructor(words?, sigBytes?) {
88
words = this.words = words || [];
99

1010
this.sigBytes = sigBytes !== undefined ? sigBytes : words.length * 4;
1111
}
1212

13-
toString (encoder?): string {
13+
toString(encoder?): string {
1414
return (encoder || Hex).stringify(this);
1515
}
1616

17-
concat (wordArray: WordArray) {
17+
concat(wordArray: WordArray) {
1818
// Clamp excess bits
1919
this.clamp();
2020

2121
// Concat
2222
if (this.sigBytes % 4) {
2323
// Copy one byte at a time
2424
for (let i = 0; i < wordArray.sigBytes; i++) {
25-
const thatByte = (wordArray.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xFF;
26-
this.words[(this.sigBytes + i) >>> 2] |= thatByte << (24 - ((this.sigBytes + i) % 4) * 8);
25+
const thatByte =
26+
(wordArray.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
27+
this.words[(this.sigBytes + i) >>> 2] |=
28+
thatByte << (24 - ((this.sigBytes + i) % 4) * 8);
2729
}
2830
} else {
2931
// Copy one word at a time
@@ -37,89 +39,90 @@ export class WordArray {
3739
return this;
3840
}
3941

40-
clamp () {
42+
clamp() {
4143
// Clamp
42-
this.words[this.sigBytes >>> 2] &= 0xFF_FF_FF_FF << (32 - (this.sigBytes % 4) * 8);
44+
this.words[this.sigBytes >>> 2] &=
45+
0xff_ff_ff_ff << (32 - (this.sigBytes % 4) * 8);
4346
this.words.length = Math.ceil(this.sigBytes / 4);
4447
}
4548

46-
clone () {
49+
clone() {
4750
return new WordArray([...this.words]);
4851
}
4952
}
5053

5154
export const Hex = {
52-
stringify (wordArray: WordArray) {
55+
stringify(wordArray: WordArray) {
5356
// Convert
5457
const hexChars: string[] = [];
5558
for (let i = 0; i < wordArray.sigBytes; i++) {
56-
const bite = (wordArray.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xFF;
57-
hexChars.push(
58-
(bite >>> 4).toString(16),
59-
(bite & 0x0F).toString(16)
60-
);
59+
const bite = (wordArray.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
60+
hexChars.push((bite >>> 4).toString(16), (bite & 0x0f).toString(16));
6161
}
6262

6363
return hexChars.join("");
64-
}
64+
},
6565
};
6666

6767
export const Base64 = {
68-
stringify (wordArray: WordArray) {
69-
const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
68+
stringify(wordArray: WordArray) {
69+
const keyStr =
70+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
7071
const base64Chars: string[] = [];
7172
for (let i = 0; i < wordArray.sigBytes; i += 3) {
72-
const byte1 = (wordArray.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xFF;
73-
const byte2 = (wordArray.words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xFF;
74-
const byte3 = (wordArray.words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xFF;
73+
const byte1 = (wordArray.words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
74+
const byte2 =
75+
(wordArray.words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
76+
const byte3 =
77+
(wordArray.words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
7578

7679
const triplet = (byte1 << 16) | (byte2 << 8) | byte3;
77-
for (let j = 0; (j < 4) && (i * 8 + j * 6 < wordArray.sigBytes * 8); j++) {
78-
base64Chars.push(keyStr.charAt((triplet >>> (6 * (3 - j))) & 0x3F));
80+
for (let j = 0; j < 4 && i * 8 + j * 6 < wordArray.sigBytes * 8; j++) {
81+
base64Chars.push(keyStr.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
7982
}
8083
}
8184
return base64Chars.join("");
82-
}
85+
},
8386
};
8487

8588
export const Latin1 = {
86-
parse (latin1Str) {
89+
parse(latin1Str) {
8790
// Shortcut
8891
const latin1StrLength = latin1Str.length;
8992

9093
// Convert
9194
const words = [];
9295
for (let i = 0; i < latin1StrLength; i++) {
93-
words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xFF) << (24 - (i % 4) * 8);
96+
words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
9497
}
9598

9699
return new WordArray(words, latin1StrLength);
97-
}
100+
},
98101
};
99102

100103
export const Utf8 = {
101-
parse (utf8Str) {
104+
parse(utf8Str) {
102105
return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
103-
}
106+
},
104107
};
105108

106109
export class BufferedBlockAlgorithm {
107110
_data: WordArray;
108111
_nDataBytes: number;
109-
_minBufferSize: number = 0;
112+
_minBufferSize = 0;
110113
blockSize = 512 / 32;
111114

112-
constructor () {
115+
constructor() {
113116
this.reset();
114117
}
115118

116-
reset () {
119+
reset() {
117120
// Initial values
118121
this._data = new WordArray();
119122
this._nDataBytes = 0;
120123
}
121124

122-
_append (data) {
125+
_append(data) {
123126
// Convert string to WordArray, else assume WordArray already
124127
if (typeof data === "string") {
125128
data = Utf8.parse(data);
@@ -130,13 +133,14 @@ export class BufferedBlockAlgorithm {
130133
this._nDataBytes += data.sigBytes;
131134
}
132135

133-
_doProcessBlock (_dataWords, _offset) {}
136+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
137+
_doProcessBlock(_dataWords, _offset) {}
134138

135-
_process (doFlush?: Boolean) {
139+
_process(doFlush?: boolean) {
136140
let processedWords;
137141

138142
// Count blocks ready
139-
let nBlocksReady = this._data.sigBytes / (this.blockSize * 4 /* bytes */);
143+
let nBlocksReady = this._data.sigBytes / (this.blockSize * 4); /* bytes */
140144
if (doFlush) {
141145
// Round up to include partial blocks
142146
nBlocksReady = Math.ceil(nBlocksReady);
@@ -170,7 +174,7 @@ export class BufferedBlockAlgorithm {
170174
}
171175

172176
export class Hasher extends BufferedBlockAlgorithm {
173-
update (messageUpdate) {
177+
update(messageUpdate) {
174178
// Append
175179
this._append(messageUpdate);
176180

@@ -181,7 +185,7 @@ export class Hasher extends BufferedBlockAlgorithm {
181185
return this;
182186
}
183187

184-
finalize (messageUpdate) {
188+
finalize(messageUpdate) {
185189
// Final message update
186190
if (messageUpdate) {
187191
this._append(messageUpdate);

src/crypto/murmur.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @param {number} seed Positive integer only
66
* @return {number} 32-bit positive integer hash
77
*/
8-
export function murmurHash (key: Uint8Array | string, seed = 0) {
8+
export function murmurHash(key: Uint8Array | string, seed = 0) {
99
if (typeof key === "string") {
1010
key = createBuffer(key);
1111
}
@@ -17,50 +17,72 @@ export function murmurHash (key: Uint8Array | string, seed = 0) {
1717

1818
const remainder = key.length & 3; // key.length % 4
1919
const bytes = key.length - remainder;
20-
const c1 = 0xCC_9E_2D_51;
21-
const c2 = 0x1B_87_35_93;
20+
const c1 = 0xcc_9e_2d_51;
21+
const c2 = 0x1b_87_35_93;
2222

2323
while (i < bytes) {
2424
k1 =
25-
((key[i] & 0xFF)) |
26-
((key[++i] & 0xFF) << 8) |
27-
((key[++i] & 0xFF) << 16) |
28-
((key[++i] & 0xFF) << 24);
25+
(key[i] & 0xff) |
26+
((key[++i] & 0xff) << 8) |
27+
((key[++i] & 0xff) << 16) |
28+
((key[++i] & 0xff) << 24);
2929
++i;
3030

31-
k1 = ((((k1 & 0xFF_FF) * c1) + ((((k1 >>> 16) * c1) & 0xFF_FF) << 16))) & 0xFF_FF_FF_FF;
31+
k1 =
32+
((k1 & 0xff_ff) * c1 + ((((k1 >>> 16) * c1) & 0xff_ff) << 16)) &
33+
0xff_ff_ff_ff;
3234
k1 = (k1 << 15) | (k1 >>> 17);
33-
k1 = ((((k1 & 0xFF_FF) * c2) + ((((k1 >>> 16) * c2) & 0xFF_FF) << 16))) & 0xFF_FF_FF_FF;
35+
k1 =
36+
((k1 & 0xff_ff) * c2 + ((((k1 >>> 16) * c2) & 0xff_ff) << 16)) &
37+
0xff_ff_ff_ff;
3438

3539
h1 ^= k1;
3640
h1 = (h1 << 13) | (h1 >>> 19);
37-
h1b = ((((h1 & 0xFF_FF) * 5) + ((((h1 >>> 16) * 5) & 0xFF_FF) << 16))) & 0xFF_FF_FF_FF;
38-
h1 = (((h1b & 0xFF_FF) + 0x6B_64) + ((((h1b >>> 16) + 0xE6_54) & 0xFF_FF) << 16));
41+
h1b =
42+
((h1 & 0xff_ff) * 5 + ((((h1 >>> 16) * 5) & 0xff_ff) << 16)) &
43+
0xff_ff_ff_ff;
44+
h1 =
45+
(h1b & 0xff_ff) + 0x6b_64 + ((((h1b >>> 16) + 0xe6_54) & 0xff_ff) << 16);
3946
}
4047

4148
k1 = 0;
4249

4350
switch (remainder) {
44-
case 3: k1 ^= (key[i + 2] & 0xFF) << 16; break;
45-
case 2: k1 ^= (key[i + 1] & 0xFF) << 8; break;
46-
case 1: k1 ^= (key[i] & 0xFF);
47-
k1 = (((k1 & 0xFF_FF) * c1) + ((((k1 >>> 16) * c1) & 0xFF_FF) << 16)) & 0xFF_FF_FF_FF;
51+
case 3:
52+
k1 ^= (key[i + 2] & 0xff) << 16;
53+
break;
54+
case 2:
55+
k1 ^= (key[i + 1] & 0xff) << 8;
56+
break;
57+
case 1:
58+
k1 ^= key[i] & 0xff;
59+
k1 =
60+
((k1 & 0xff_ff) * c1 + ((((k1 >>> 16) * c1) & 0xff_ff) << 16)) &
61+
0xff_ff_ff_ff;
4862
k1 = (k1 << 15) | (k1 >>> 17);
49-
k1 = (((k1 & 0xFF_FF) * c2) + ((((k1 >>> 16) * c2) & 0xFF_FF) << 16)) & 0xFF_FF_FF_FF;
63+
k1 =
64+
((k1 & 0xff_ff) * c2 + ((((k1 >>> 16) * c2) & 0xff_ff) << 16)) &
65+
0xff_ff_ff_ff;
5066
h1 ^= k1;
5167
}
5268

5369
h1 ^= key.length;
5470

5571
h1 ^= h1 >>> 16;
56-
h1 = (((h1 & 0xFF_FF) * 0x85_EB_CA_6B) + ((((h1 >>> 16) * 0x85_EB_CA_6B) & 0xFF_FF) << 16)) & 0xFF_FF_FF_FF;
72+
h1 =
73+
((h1 & 0xff_ff) * 0x85_eb_ca_6b +
74+
((((h1 >>> 16) * 0x85_eb_ca_6b) & 0xff_ff) << 16)) &
75+
0xff_ff_ff_ff;
5776
h1 ^= h1 >>> 13;
58-
h1 = ((((h1 & 0xFF_FF) * 0xC2_B2_AE_35) + ((((h1 >>> 16) * 0xC2_B2_AE_35) & 0xFF_FF) << 16))) & 0xFF_FF_FF_FF;
77+
h1 =
78+
((h1 & 0xff_ff) * 0xc2_b2_ae_35 +
79+
((((h1 >>> 16) * 0xc2_b2_ae_35) & 0xff_ff) << 16)) &
80+
0xff_ff_ff_ff;
5981
h1 ^= h1 >>> 16;
6082

6183
return h1 >>> 0;
6284
}
6385

64-
function createBuffer (val: any) {
86+
function createBuffer(val: any) {
6587
return new TextEncoder().encode(val);
6688
}

0 commit comments

Comments
 (0)