mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-22 03:05:19 -04:00
Add prettier for codestyle and re-format everything (#1294)
This commit is contained in:
parent
8b78154075
commit
0aae1f70d2
319 changed files with 4809 additions and 3936 deletions
60
src/components/common/cache/cache.test.ts
vendored
60
src/components/common/cache/cache.test.ts
vendored
|
@ -16,88 +16,68 @@ describe('Test caching functionality', () => {
|
|||
it('initialize with right lifetime, no entry limit', () => {
|
||||
const lifetime = 1000
|
||||
const lifetimedCache = new Cache<string, string>(lifetime)
|
||||
expect(lifetimedCache.entryLifetime)
|
||||
.toEqual(lifetime)
|
||||
expect(lifetimedCache.maxEntries)
|
||||
.toEqual(0)
|
||||
expect(lifetimedCache.entryLifetime).toEqual(lifetime)
|
||||
expect(lifetimedCache.maxEntries).toEqual(0)
|
||||
})
|
||||
|
||||
it('initialize with right lifetime, given entry limit', () => {
|
||||
const lifetime = 1000
|
||||
const maxEntries = 10
|
||||
const limitedCache = new Cache<string, string>(lifetime, maxEntries)
|
||||
expect(limitedCache.entryLifetime)
|
||||
.toEqual(lifetime)
|
||||
expect(limitedCache.maxEntries)
|
||||
.toEqual(maxEntries)
|
||||
expect(limitedCache.entryLifetime).toEqual(lifetime)
|
||||
expect(limitedCache.maxEntries).toEqual(maxEntries)
|
||||
})
|
||||
|
||||
it('entry exists after inserting', () => {
|
||||
testCache.put('test', 123)
|
||||
expect(testCache.has('test'))
|
||||
.toBe(true)
|
||||
expect(testCache.has('test')).toBe(true)
|
||||
})
|
||||
|
||||
it('entry does not exist prior inserting', () => {
|
||||
expect(testCache.has('test'))
|
||||
.toBe(false)
|
||||
expect(testCache.has('test')).toBe(false)
|
||||
})
|
||||
|
||||
it('entry does expire', () => {
|
||||
const shortLivingCache = new Cache<string, number>(2)
|
||||
shortLivingCache.put('test', 123)
|
||||
expect(shortLivingCache.has('test'))
|
||||
.toBe(true)
|
||||
expect(shortLivingCache.has('test')).toBe(true)
|
||||
setTimeout(() => {
|
||||
expect(shortLivingCache.has('test'))
|
||||
.toBe(false)
|
||||
expect(shortLivingCache.has('test')).toBe(false)
|
||||
}, 2000)
|
||||
})
|
||||
|
||||
it('entry value does not change', () => {
|
||||
const testValue = Date.now()
|
||||
testCache.put('test', testValue)
|
||||
expect(testCache.get('test'))
|
||||
.toEqual(testValue)
|
||||
expect(testCache.get('test')).toEqual(testValue)
|
||||
})
|
||||
|
||||
it('error is thrown on non-existent entry', () => {
|
||||
const accessNonExistentEntry = () => {
|
||||
testCache.get('test')
|
||||
}
|
||||
expect(accessNonExistentEntry)
|
||||
.toThrow(Error)
|
||||
expect(accessNonExistentEntry).toThrow(Error)
|
||||
})
|
||||
|
||||
it('newer item replaces older item', () => {
|
||||
testCache.put('test', 123)
|
||||
testCache.put('test', 456)
|
||||
expect(testCache.get('test'))
|
||||
.toEqual(456)
|
||||
expect(testCache.get('test')).toEqual(456)
|
||||
})
|
||||
|
||||
it('entry limit is respected', () => {
|
||||
const limitedCache = new Cache<string, number>(1000, 2)
|
||||
limitedCache.put('first', 1)
|
||||
expect(limitedCache.has('first'))
|
||||
.toBe(true)
|
||||
expect(limitedCache.has('second'))
|
||||
.toBe(false)
|
||||
expect(limitedCache.has('third'))
|
||||
.toBe(false)
|
||||
expect(limitedCache.has('first')).toBe(true)
|
||||
expect(limitedCache.has('second')).toBe(false)
|
||||
expect(limitedCache.has('third')).toBe(false)
|
||||
limitedCache.put('second', 2)
|
||||
expect(limitedCache.has('first'))
|
||||
.toBe(true)
|
||||
expect(limitedCache.has('second'))
|
||||
.toBe(true)
|
||||
expect(limitedCache.has('third'))
|
||||
.toBe(false)
|
||||
expect(limitedCache.has('first')).toBe(true)
|
||||
expect(limitedCache.has('second')).toBe(true)
|
||||
expect(limitedCache.has('third')).toBe(false)
|
||||
limitedCache.put('third', 3)
|
||||
expect(limitedCache.has('first'))
|
||||
.toBe(false)
|
||||
expect(limitedCache.has('second'))
|
||||
.toBe(true)
|
||||
expect(limitedCache.has('third'))
|
||||
.toBe(true)
|
||||
expect(limitedCache.has('first')).toBe(false)
|
||||
expect(limitedCache.has('second')).toBe(true)
|
||||
expect(limitedCache.has('third')).toBe(true)
|
||||
})
|
||||
})
|
||||
|
|
5
src/components/common/cache/cache.ts
vendored
5
src/components/common/cache/cache.ts
vendored
|
@ -27,7 +27,7 @@ export class Cache<K, V> {
|
|||
return false
|
||||
}
|
||||
const entry = this.store.get(key)
|
||||
return (!!entry && entry.entryCreated >= (Date.now() - this.entryLifetime * 1000))
|
||||
return !!entry && entry.entryCreated >= Date.now() - this.entryLifetime * 1000
|
||||
}
|
||||
|
||||
get(key: K): V {
|
||||
|
@ -40,8 +40,7 @@ export class Cache<K, V> {
|
|||
|
||||
put(key: K, value: V): void {
|
||||
if (this.maxEntries > 0 && this.store.size === this.maxEntries) {
|
||||
this.store.delete(this.store.keys()
|
||||
.next().value)
|
||||
this.store.delete(this.store.keys().next().value)
|
||||
}
|
||||
this.store.set(key, {
|
||||
entryCreated: Date.now(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue