ESLint prefer-const (#1956)

This commit is contained in:
Joel Jeremy Marquez
2023-11-22 11:12:22 -08:00
committed by GitHub
parent 9c124e8e44
commit 881999bcfe
30 changed files with 166 additions and 141 deletions

View File

@@ -69,8 +69,8 @@ describe('merkle trie', () => {
});
test('diffing works with empty tries', () => {
let trie1 = merkle.emptyTrie();
let trie2 = merkle.insert(
const trie1 = merkle.emptyTrie();
const trie2 = merkle.insert(
merkle.emptyTrie(),
Timestamp.parse('2009-01-02T10:17:37.789Z-0000-0000testinguuid1')!,
);
@@ -79,7 +79,7 @@ describe('merkle trie', () => {
});
test('pruning works and keeps correct hashes', () => {
let messages = [
const messages = [
message('2018-11-01T01:00:00.000Z-0000-0123456789ABCDEF', 1000),
message('2018-11-01T01:09:00.000Z-0000-0123456789ABCDEF', 1100),
message('2018-11-01T01:18:00.000Z-0000-0123456789ABCDEF', 1200),
@@ -101,13 +101,13 @@ describe('merkle trie', () => {
expect(trie.hash).toBe(2496);
expect(trie).toMatchSnapshot();
let pruned = merkle.prune(trie);
const pruned = merkle.prune(trie);
expect(pruned.hash).toBe(2496);
expect(pruned).toMatchSnapshot();
});
test('diffing differently shaped tries returns correct time', () => {
let messages = [
const messages = [
message('2018-11-01T01:00:00.000Z-0000-0123456789ABCDEF', 1000),
message('2018-11-01T01:09:00.000Z-0000-0123456789ABCDEF', 1100),
message('2018-11-01T01:18:00.000Z-0000-0123456789ABCDEF', 1200),
@@ -122,7 +122,7 @@ describe('merkle trie', () => {
message('2018-11-01T02:37:00.000Z-0000-0123456789ABCDEF', 2100),
];
let trie = insertMessages({}, messages);
const trie = insertMessages({}, messages);
// Case 0: It always returns a base time when comparing with an
// empty trie
@@ -136,7 +136,7 @@ describe('merkle trie', () => {
// Case 1: Add an older message that modifies the trie in such a
// way that it modifies the 1st out of 3 branches (so it will be
// pruned away)
let trie1 = insertMessages(trie, [
const trie1 = insertMessages(trie, [
message('2018-11-01T00:59:00.000Z-0000-0123456789ABCDEF', 900),
]);
@@ -167,7 +167,7 @@ describe('merkle trie', () => {
// Case 2: Add two messages similar to the above case, but the
// second message modifies the 2nd key at the same level as the
// first message modifying the 1st key
let trie2 = insertMessages(trie, [
const trie2 = insertMessages(trie, [
message('2018-11-01T00:59:00.000Z-0000-0123456789ABCDEF', 900),
message('2018-11-01T01:15:00.000Z-0000-0123456789ABCDEF', 1422),
]);

View File

@@ -36,7 +36,7 @@ export function getKeys(trie: TrieNode): NumberTrieNodeKey[] {
export function keyToTimestamp(key: string): number {
// 16 is the length of the base 3 value of the current time in
// minutes. Ensure it's padded to create the full value
let fullkey = key + '0'.repeat(16 - key.length);
const fullkey = key + '0'.repeat(16 - key.length);
// Parse the base 3 representation
return parseInt(fullkey, 3) * 1000 * 60;
@@ -46,8 +46,8 @@ export function keyToTimestamp(key: string): number {
* Mutates `trie` to insert a node at `timestamp`
*/
export function insert(trie: TrieNode, timestamp: Timestamp) {
let hash = timestamp.hash();
let key = Number(Math.floor(timestamp.millis() / 1000 / 60)).toString(3);
const hash = timestamp.hash();
const key = Number(Math.floor(timestamp.millis() / 1000 / 60)).toString(3);
trie = Object.assign({}, trie, { hash: (trie.hash || 0) ^ hash });
return insertKey(trie, key, hash);
@@ -68,8 +68,8 @@ function insertKey(trie: TrieNode, key: string, hash: number): TrieNode {
}
export function build(timestamps: Timestamp[]) {
let trie = emptyTrie();
for (let timestamp of timestamps) {
const trie = emptyTrie();
for (const timestamp of timestamps) {
insert(trie, timestamp);
}
return trie;
@@ -89,8 +89,8 @@ export function diff(trie1: TrieNode, trie2: TrieNode): number | null {
// left (this shouldn't happen, if that's the case the hash check at
// the top of this function should pass)
while (1) {
let keyset = new Set([...getKeys(node1), ...getKeys(node2)]);
let keys = [...keyset.values()];
const keyset = new Set([...getKeys(node1), ...getKeys(node2)]);
const keys = [...keyset.values()];
keys.sort();
let diffkey = null;
@@ -110,10 +110,10 @@ export function diff(trie1: TrieNode, trie2: TrieNode): number | null {
// changed time that we know of, because of pruning it might take
// multiple passes to sync up a trie.
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
const key = keys[i];
let next1 = node1[key];
let next2 = node2[key];
const next1 = node1[key];
const next2 = node2[key];
if (!next1 || !next2) {
break;
@@ -143,13 +143,13 @@ export function prune(trie: TrieNode, n = 2): TrieNode {
return trie;
}
let keys = getKeys(trie);
const keys = getKeys(trie);
keys.sort();
let next: TrieNode = { hash: trie.hash };
const next: TrieNode = { hash: trie.hash };
// Prune child nodes.
for (let k of keys.slice(-n)) {
for (const k of keys.slice(-n)) {
const node = trie[k];
if (!node) {

View File

@@ -28,7 +28,7 @@ describe('Timestamp', function () {
describe('parsing', function () {
it('should not parse', function () {
let invalidInputs = [
const invalidInputs = [
null,
undefined,
{},
@@ -44,19 +44,19 @@ describe('Timestamp', function () {
'9999-12-31T23:59:59.999Z-10000-FFFFFFFFFFFFFFFF',
'9999-12-31T23:59:59.999Z-FFFF-10000000000000000',
];
for (let invalidInput of invalidInputs) {
for (const invalidInput of invalidInputs) {
expect(Timestamp.parse(invalidInput as string)).toBe(null);
}
});
it('should parse', function () {
let validInputs = [
const validInputs = [
'1970-01-01T00:00:00.000Z-0000-0000000000000000',
'2015-04-24T22:23:42.123Z-1000-0123456789ABCDEF',
'9999-12-31T23:59:59.999Z-FFFF-FFFFFFFFFFFFFFFF',
];
for (let validInput of validInputs) {
let parsed = Timestamp.parse(validInput)!;
for (const validInput of validInputs) {
const parsed = Timestamp.parse(validInput)!;
expect(typeof parsed).toBe('object');
expect(parsed.millis() >= 0).toBeTruthy();
expect(parsed.millis() < 253402300800000).toBeTruthy();

View File

@@ -80,7 +80,7 @@ export function makeClientId() {
return uuidv4().replace(/-/g, '').slice(-16);
}
let config = {
const config = {
// Allow 5 minutes of clock drift
maxDrift: 5 * 60 * 1000,
};
@@ -168,11 +168,11 @@ export class Timestamp {
return timestamp;
}
if (typeof timestamp === 'string') {
let parts = timestamp.split('-');
const parts = timestamp.split('-');
if (parts && parts.length === 5) {
let millis = Date.parse(parts.slice(0, 3).join('-')).valueOf();
let counter = parseInt(parts[3], 16);
let node = parts[4];
const millis = Date.parse(parts.slice(0, 3).join('-')).valueOf();
const counter = parseInt(parts[3], 16);
const node = parts[4];
if (
!isNaN(millis) &&
millis >= 0 &&
@@ -198,17 +198,17 @@ export class Timestamp {
}
// retrieve the local wall time
let phys = Date.now();
const phys = Date.now();
// unpack the clock.timestamp logical time and counter
let lOld = clock.timestamp.millis();
let cOld = clock.timestamp.counter();
const lOld = clock.timestamp.millis();
const cOld = clock.timestamp.counter();
// calculate the next logical time and counter
// * ensure that the logical time never goes backward
// * increment the counter if phys time does not advance
let lNew = Math.max(lOld, phys);
let cNew = lOld === lNew ? cOld + 1 : 0;
const lNew = Math.max(lOld, phys);
const cNew = lOld === lNew ? cOld + 1 : 0;
// check the result for drift and counter overflow
if (lNew - phys > config.maxDrift) {
@@ -238,11 +238,11 @@ export class Timestamp {
}
// retrieve the local wall time
let phys = Date.now();
const phys = Date.now();
// unpack the message wall time/counter
let lMsg = msg.millis();
let cMsg = msg.counter();
const lMsg = msg.millis();
const cMsg = msg.counter();
// assert the node id and remote clock drift
// if (msg.node() === clock.timestamp.node()) {
@@ -253,8 +253,8 @@ export class Timestamp {
}
// unpack the clock.timestamp logical time and counter
let lOld = clock.timestamp.millis();
let cOld = clock.timestamp.counter();
const lOld = clock.timestamp.millis();
const cOld = clock.timestamp.counter();
// calculate the next logical time and counter
// . ensure that the logical time never goes backward
@@ -262,8 +262,8 @@ export class Timestamp {
// . if max = old > message, increment local counter
// . if max = messsage > old, increment message counter
// . otherwise, clocks are monotonic, reset counter
let lNew = Math.max(Math.max(lOld, phys), lMsg);
let cNew =
const lNew = Math.max(Math.max(lOld, phys), lMsg);
const cNew =
lNew === lOld && lNew === lMsg
? Math.max(cOld, cMsg) + 1
: lNew === lOld