feat: Test running overhaul, switch to Prettier & reformat everything (#1407)

* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
Roland Hummel
2023-10-03 23:08:19 +02:00
committed by GitHub
parent 0ca18c2b2c
commit 86d333ee94
392 changed files with 5849 additions and 16622 deletions

View File

@@ -1,5 +1,5 @@
class CacheNode {
constructor (key, value, frequency) {
constructor(key, value, frequency) {
this.key = key
this.value = value
this.frequency = frequency
@@ -10,15 +10,19 @@ class CacheNode {
// This frequency map class will act like javascript Map DS with more two custom method refresh & insert
class FrequencyMap extends Map {
static get [Symbol.species] () { return Map } // for using Symbol.species we can access Map constructor @see -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@species
get [Symbol.toStringTag] () { return '' }
static get [Symbol.species]() {
return Map
} // for using Symbol.species we can access Map constructor @see -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@species
get [Symbol.toStringTag]() {
return ''
}
/**
* @method refresh
* @description - It's revive a CacheNode, increment of this nodes frequency and refresh the frequencyMap via new incremented nodes frequency
* @param {CacheNode} node
*/
refresh (node) {
* @method refresh
* @description - It's revive a CacheNode, increment of this nodes frequency and refresh the frequencyMap via new incremented nodes frequency
* @param {CacheNode} node
*/
refresh(node) {
const { frequency } = node
const freqSet = this.get(frequency)
freqSet.delete(node)
@@ -33,7 +37,7 @@ class FrequencyMap extends Map {
* @description - Add new CacheNode into HashSet by the frequency
* @param {CacheNode} node
*/
insert (node) {
insert(node) {
const { frequency } = node
if (!this.has(frequency)) {
@@ -49,10 +53,10 @@ class LFUCache {
#frequencyMap
/**
* @param {number} capacity - The range of LFUCache
* @returns {LFUCache} - sealed
*/
constructor (capacity) {
* @param {number} capacity - The range of LFUCache
* @returns {LFUCache} - sealed
*/
constructor(capacity) {
this.#capacity = capacity
this.#frequencyMap = new FrequencyMap()
this.misses = 0
@@ -66,7 +70,7 @@ class LFUCache {
* Get the capacity of the LFUCache
* @returns {number}
*/
get capacity () {
get capacity() {
return this.#capacity
}
@@ -74,14 +78,14 @@ class LFUCache {
* Get the current size of LFUCache
* @returns {number}
*/
get size () {
get size() {
return this.cache.size
}
/**
* Set the capacity of the LFUCache if you decrease the capacity its removed CacheNodes following the LFU - least frequency used
*/
set capacity (newCapacity) {
* Set the capacity of the LFUCache if you decrease the capacity its removed CacheNodes following the LFU - least frequency used
*/
set capacity(newCapacity) {
if (this.#capacity > newCapacity) {
let diff = this.#capacity - newCapacity // get the decrement number of capacity
@@ -95,7 +99,7 @@ class LFUCache {
this.#capacity = newCapacity
}
get info () {
get info() {
return Object.freeze({
misses: this.misses,
hits: this.hits,
@@ -105,7 +109,7 @@ class LFUCache {
})
}
get leastFrequency () {
get leastFrequency() {
const freqCacheIterator = this.#frequencyMap.keys()
let leastFrequency = freqCacheIterator.next().value || null
@@ -117,7 +121,7 @@ class LFUCache {
return leastFrequency
}
#removeCacheNode () {
#removeCacheNode() {
const leastFreqSet = this.#frequencyMap.get(this.leastFrequency)
// Select the least recently used node from the least Frequency set
const LFUNode = leastFreqSet.values().next().value
@@ -131,19 +135,19 @@ class LFUCache {
* @param {any} key
* @returns {boolean}
*/
has (key) {
has(key) {
key = String(key) // converted to string
return this.cache.has(key)
}
/**
* @method get
* @description - This method return the value of key & refresh the frequencyMap by the oldNode
* @param {string} key
* @returns {any}
*/
get (key) {
* @method get
* @description - This method return the value of key & refresh the frequencyMap by the oldNode
* @param {string} key
* @returns {any}
*/
get(key) {
key = String(key) // converted to string
if (this.cache.has(key)) {
@@ -160,14 +164,14 @@ class LFUCache {
}
/**
* @method set
* @description - This method stored the value by key & add frequency if it doesn't exist
* @param {string} key
* @param {any} value
* @param {number} frequency
* @returns {LFUCache}
*/
set (key, value, frequency = 1) {
* @method set
* @description - This method stored the value by key & add frequency if it doesn't exist
* @param {string} key
* @param {any} value
* @param {number} frequency
* @returns {LFUCache}
*/
set(key, value, frequency = 1) {
key = String(key) // converted to string
if (this.#capacity === 0) {
@@ -197,12 +201,12 @@ class LFUCache {
}
/**
* @method parse
* @description - This method receive a valid LFUCache JSON & run JSON.prase() method and merge with existing LFUCache
* @param {JSON} json
* @returns {LFUCache} - merged
*/
parse (json) {
* @method parse
* @description - This method receive a valid LFUCache JSON & run JSON.prase() method and merge with existing LFUCache
* @param {JSON} json
* @returns {LFUCache} - merged
*/
parse(json) {
const { misses, hits, cache } = JSON.parse(json)
this.misses += misses ?? 0
@@ -217,11 +221,11 @@ class LFUCache {
}
/**
* @method clear
* @description - This method cleared the whole LFUCache
* @returns {LFUCache}
*/
clear () {
* @method clear
* @description - This method cleared the whole LFUCache
* @returns {LFUCache}
*/
clear() {
this.cache.clear()
this.#frequencyMap.clear()
@@ -229,12 +233,12 @@ class LFUCache {
}
/**
* @method toString
* @description - This method generate a JSON format of LFUCache & return it.
* @param {number} indent
* @returns {string} - JSON
*/
toString (indent) {
* @method toString
* @description - This method generate a JSON format of LFUCache & return it.
* @param {number} indent
* @returns {string} - JSON
*/
toString(indent) {
const replacer = (_, value) => {
if (value instanceof Set) {
return [...value]

View File

@@ -6,7 +6,7 @@ class LRUCache {
* @param {number} capacity - the capacity of LRUCache
* @returns {LRUCache} - sealed
*/
constructor (capacity) {
constructor(capacity) {
if (!Number.isInteger(capacity) || capacity < 0) {
throw new TypeError('Invalid capacity')
}
@@ -19,7 +19,7 @@ class LRUCache {
return Object.seal(this)
}
get info () {
get info() {
return Object.freeze({
misses: this.misses,
hits: this.hits,
@@ -28,15 +28,15 @@ class LRUCache {
})
}
get size () {
get size() {
return this.cache.size
}
get capacity () {
get capacity() {
return this.#capacity
}
set capacity (newCapacity) {
set capacity(newCapacity) {
if (newCapacity < 0) {
throw new RangeError('Capacity should be greater than 0')
}
@@ -53,9 +53,9 @@ class LRUCache {
}
/**
* delete oldest key existing in map by the help of iterator
*/
#removeLeastRecentlyUsed () {
* delete oldest key existing in map by the help of iterator
*/
#removeLeastRecentlyUsed() {
this.cache.delete(this.cache.keys().next().value)
}
@@ -63,7 +63,7 @@ class LRUCache {
* @param {string} key
* @returns {*}
*/
has (key) {
has(key) {
key = String(key)
return this.cache.has(key)
@@ -73,7 +73,7 @@ class LRUCache {
* @param {string} key
* @param {*} value
*/
set (key, value) {
set(key, value) {
key = String(key)
// Sets the value for the input key and if the key exists it updates the existing key
if (this.size === this.capacity) {
@@ -87,7 +87,7 @@ class LRUCache {
* @param {string} key
* @returns {*}
*/
get (key) {
get(key) {
key = String(key)
// Returns the value for the input key. Returns null if key is not present in cache
if (this.cache.has(key)) {
@@ -109,7 +109,7 @@ class LRUCache {
* @param {JSON} json
* @returns {LRUCache}
*/
parse (json) {
parse(json) {
const { misses, hits, cache } = JSON.parse(json)
this.misses += misses ?? 0
@@ -126,7 +126,7 @@ class LRUCache {
* @param {number} indent
* @returns {JSON} - string
*/
toString (indent) {
toString(indent) {
const replacer = (_, value) => {
if (value instanceof Set) {
return [...value]

View File

@@ -15,11 +15,13 @@
*/
const memoize = (func, cache = new Map()) => {
const jsonReplacer = (_, value) => {
if (value instanceof Set) { // if the value is Set it's converted to Array cause JSON.stringify can't convert Set
if (value instanceof Set) {
// if the value is Set it's converted to Array cause JSON.stringify can't convert Set
return [...value]
}
if (value instanceof Map) { // if the value is Map it's converted to Object cause JSON.stringify can't convert Map
if (value instanceof Map) {
// if the value is Map it's converted to Object cause JSON.stringify can't convert Map
return Object.fromEntries(value)
}

View File

@@ -36,7 +36,8 @@ describe('Testing LFUCache class', () => {
leastFrequency: 2
})
const json = '{"misses":3,"hits":6,"cache":{"2":{"key":"2","value":2,"frequency":4},"4":{"key":"4","value":4,"frequency":2}}}'
const json =
'{"misses":3,"hits":6,"cache":{"2":{"key":"2","value":2,"frequency":4},"4":{"key":"4","value":4,"frequency":2}}}'
expect(cache.toString()).toBe(json)
const cacheInstance = cache.parse(json) // again merge the json
@@ -45,7 +46,8 @@ describe('Testing LFUCache class', () => {
cache.capacity = 1 // decrease the capacity
expect(cache.info).toEqual({ // after merging the info
expect(cache.info).toEqual({
// after merging the info
misses: 6,
hits: 12,
capacity: 1,

View File

@@ -43,11 +43,7 @@ describe('Testing Memoize', () => {
it('expects the union function to use the cache on the second call', () => {
const memoUnion = memoize(union)
const inputs = [
new Set([1, 2, 3]),
new Set([4, 3, 2]),
new Set([5, 3, 6])
]
const inputs = [new Set([1, 2, 3]), new Set([4, 3, 2]), new Set([5, 3, 6])]
expect(memoUnion(...inputs)).toEqual(new Set([1, 2, 3, 4, 5, 6]))
expect(memoUnion(...inputs)).toEqual(union(...inputs))

View File

@@ -31,7 +31,5 @@ export const fibonacciCache = (n, cache = null) => {
* @return {new Set}
*/
export const union = (...sets) => {
return new Set(
sets.reduce((flatArray, set) => [...flatArray, ...set], [])
)
return new Set(sets.reduce((flatArray, set) => [...flatArray, ...set], []))
}