-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2351 from dusk-network/feature-2350
web-wallet: Add base functionalities for the cache layer
- Loading branch information
Showing
14 changed files
with
2,162 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
web-wallet/src/lib/dusk/array/__tests__/arrayMaxByKey.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { arrayMaxByKey } from ".."; | ||
|
||
describe("arrayMaxByKey", () => { | ||
const testArr = [ | ||
{ active: true, date: new Date(2024, 1, 2), uid: "id3", value: 6, x: 0 }, | ||
{ active: false, date: new Date(2024, 1, 3), uid: "id2", value: 3, x: -0 }, | ||
{ active: false, date: new Date(2024, 1, 4), uid: "id1", value: 5, x: -1 }, | ||
]; | ||
|
||
it("should get the max value of a key holding a comparable value in an array of objects", () => { | ||
expect(arrayMaxByKey("active")(testArr)).toBe(true); | ||
expect(arrayMaxByKey("date")(testArr)).toStrictEqual(new Date(2024, 1, 4)); | ||
expect(arrayMaxByKey("uid")(testArr)).toBe("id3"); | ||
expect(arrayMaxByKey("value")(testArr)).toBe(6); | ||
}); | ||
|
||
it("will use the last encountered value if `0` or `-0` are the max value", () => { | ||
expect(arrayMaxByKey("x")(testArr)).toBe(-0); | ||
}); | ||
|
||
it("should return `undefined` if given an empty array", () => { | ||
expect(arrayMaxByKey("value")([])).toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { reduce } from "lamb"; | ||
|
||
/** | ||
* Gets the max value of the given key in an array of objects. | ||
* | ||
* | ||
* @example | ||
* const scores = [ | ||
* { score: 7, user: "John" }, | ||
* { score: 9, user: "Jane" }, | ||
* { score: 5, user: "Mario" } | ||
* ]; | ||
* const getMaxScore = arrayMaxByKey("score"); | ||
* | ||
* getMaxScore(scores) // => 9 | ||
* | ||
* @template {string} K | ||
* @template {Record<PropertyKey, any> & Record<K, import("lamb").Ord>} S | ||
* @param {K} key | ||
* @returns {<const L extends S[]>(array: L) => L extends [] ? undefined : typeof array[number][K]} | ||
*/ | ||
const arrayMaxByKey = (key) => (array) => | ||
array.length === 0 | ||
? undefined | ||
: reduce(array, (r, c) => (r[key] > c[key] ? r : c))[key]; | ||
|
||
export default arrayMaxByKey; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as arraysEqual } from "./arraysEqual"; | ||
export { default as arrayMaxByKey } from "./arrayMaxByKey"; | ||
export { default as shuffleArray } from "./shuffleArray"; | ||
export { default as findFirstNMatches } from "./findFirstNMatches"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
/** @type {WalletCacheHistoryEntry[]} */ | ||
export default [ | ||
{ | ||
history: [ | ||
{ | ||
amount: 400, | ||
block_height: 201605, | ||
direction: "In", | ||
fee: 0.000290986, | ||
id: "678e9430fd8ca2e8b6161dd6ea30e4b923505a446dac77bdbe911ef3f50e450d", | ||
tx_type: "TRANSFER", | ||
}, | ||
{ | ||
amount: 5000, | ||
block_height: 201643, | ||
direction: "In", | ||
fee: 0.00030326, | ||
id: "eeeca070ed7650fc2692be43ba36f5d74c0168eca92680debe0de172f64a130b", | ||
tx_type: "TRANSFER", | ||
}, | ||
{ | ||
amount: -15.00030345, | ||
block_height: 3504088, | ||
direction: "Out", | ||
fee: 0.00030345, | ||
id: "e3d9b492474d76da6edc415dd04d47d3fc40ecb0b49ee8e1dd087eb3dab0100a", | ||
tx_type: "TRANSFER", | ||
}, | ||
{ | ||
amount: -8.000291559, | ||
block_height: 3504122, | ||
direction: "Out", | ||
fee: 0.000291559, | ||
id: "8e435db8584eac07e3a7f69afbdf2d00b8176271695ea1e1b4da79922f3c050c", | ||
tx_type: "TRANSFER", | ||
}, | ||
], | ||
lastBlockHeight: 3504122, | ||
psk: "43UC3K4oy7wNBDQR4AUdkwrT1iB9ucU65GvJ8kkqB6BGwz9fwPEqzwqsJD2Rgc1UYegxXSsJmrqN2zEm2ycHitWG", | ||
}, | ||
{ | ||
history: [ | ||
{ | ||
amount: 10, | ||
block_height: 3101148, | ||
direction: "In", | ||
fee: 0.00029775, | ||
id: "5847cc9b96407fdc9ff973c3095f0d34958a47ba7272053f43038ba8e6afaf02", | ||
tx_type: "TRANSFER", | ||
}, | ||
{ | ||
amount: -5.000284098, | ||
block_height: 3101191, | ||
direction: "Out", | ||
fee: 0.000284098, | ||
id: "84d0c5727b93c47e9827524a77ea1c7492cc775faf117d26a53df6a97b791309", | ||
tx_type: "TRANSFER", | ||
}, | ||
{ | ||
amount: 1, | ||
block_height: 3101764, | ||
direction: "In", | ||
fee: 0.000296467, | ||
id: "4d9bb16589f6b723408305744eada6c05aaa4b27296ec440055c487694ce0a05", | ||
tx_type: "TRANSFER", | ||
}, | ||
], | ||
lastBlockHeight: 3101764, | ||
psk: "4Vd2cY4XAuqpcpiVFVCtveuZq7o9PV1WhmqqdNNAsZwWEojKwctU3ud7PqciKCpUnJWjZPspudQwbNwYT6Fq8Bzo", | ||
}, | ||
]; |
Oops, something went wrong.