Skip to content

Commit

Permalink
fix: Resolve linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Apr 21, 2024
1 parent 5874fee commit 7c9026d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 86 deletions.
28 changes: 14 additions & 14 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module.exports = {
presets: [
"@babel/typescript",
[
"@babel/env",
{
useBuiltIns: "usage",
corejs: "3.0.0",
},
],
],
plugins: [
"@babel/plugin-proposal-class-properties",
],
};
presets: [
'@babel/typescript',
[
'@babel/env',
{
useBuiltIns: 'usage',
corejs: '3.0.0',
},
],
],
plugins: [
'@babel/plugin-proposal-class-properties',
],
}
45 changes: 30 additions & 15 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
import StorageBuilder from './storagebuilder'
import ScopedStorage from './scopedstorage'

/**
* Get the storage builder for an app
* @param appId App ID to scope storage
*/
export function getBuilder(appId: string): StorageBuilder {
return new StorageBuilder(appId)
return new StorageBuilder(appId)
}

function clearStorage(storage: Storage, pred?: (string) => boolean): void {
Object.keys(storage)
.filter(k => pred ? pred(k) : true)
.map(storage.removeItem.bind(storage))
/**
* Clear values from storage
* @param storage The storage to clear
* @param pred Callback to check if value should be cleared
*/
function clearStorage(storage: Storage, pred?: (value: string) => boolean): void {
Object.keys(storage)
.filter(k => pred ? pred(k) : true)
.map(storage.removeItem.bind(storage))
}

/**
* Clear all values from all storages
*/
export function clearAll(): void {
const storages = [
window.sessionStorage,
window.localStorage,
]
storages.map(s => clearStorage(s))
const storages = [
window.sessionStorage,
window.localStorage,
]
storages.map(s => clearStorage(s))
}

/**
* Clear ony non persistent values
*/
export function clearNonPersistent(): void {
const storages = [
window.sessionStorage,
window.localStorage,
]
storages.map(s => clearStorage(s, k => !k.startsWith(ScopedStorage.GLOBAL_SCOPE_PERSISTENT)))
const storages = [
window.sessionStorage,
window.localStorage,
]
storages.map(s => clearStorage(s, k => !k.startsWith(ScopedStorage.GLOBAL_SCOPE_PERSISTENT)))
}
62 changes: 31 additions & 31 deletions lib/scopedstorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@ import NextcloudStorage from './storage'

export default class ScopedStorage implements NextcloudStorage {

public static GLOBAL_SCOPE_VOLATILE = 'nextcloud_vol'
public static GLOBAL_SCOPE_PERSISTENT = 'nextcloud_per'
private scope: string
private wrapped: Storage

constructor(scope: string, wrapped: Storage, persistent: boolean) {
this.scope = `${persistent ? ScopedStorage.GLOBAL_SCOPE_PERSISTENT : ScopedStorage.GLOBAL_SCOPE_VOLATILE}_${btoa(scope)}_`
this.wrapped = wrapped
}

private scopeKey(key: string) {
return `${this.scope}${key}`
}

setItem(key: string, value: string): void {
this.wrapped.setItem(this.scopeKey(key), value)
}

getItem(key: string): string | null {
return this.wrapped.getItem(this.scopeKey(key))
}

removeItem(key: string): void {
this.wrapped.removeItem(this.scopeKey(key))
}

clear(): void {
Object.keys(this.wrapped)
.filter(key => key.startsWith(this.scope))
.map(this.wrapped.removeItem.bind(this.wrapped))
}
public static GLOBAL_SCOPE_VOLATILE = 'nextcloud_vol'
public static GLOBAL_SCOPE_PERSISTENT = 'nextcloud_per'
private scope: string
private wrapped: Storage

constructor(scope: string, wrapped: Storage, persistent: boolean) {
this.scope = `${persistent ? ScopedStorage.GLOBAL_SCOPE_PERSISTENT : ScopedStorage.GLOBAL_SCOPE_VOLATILE}_${btoa(scope)}_`
this.wrapped = wrapped
}

private scopeKey(key: string) {
return `${this.scope}${key}`
}

setItem(key: string, value: string): void {
this.wrapped.setItem(this.scopeKey(key), value)
}

getItem(key: string): string | null {
return this.wrapped.getItem(this.scopeKey(key))
}

removeItem(key: string): void {
this.wrapped.removeItem(this.scopeKey(key))
}

clear(): void {
Object.keys(this.wrapped)
.filter(key => key.startsWith(this.scope))
.map(this.wrapped.removeItem.bind(this.wrapped))
}

}
51 changes: 25 additions & 26 deletions lib/storagebuilder.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@

import Storage from './storage'
import ScopedStorage from './scopedstorage'

export default class StorageBuilder {

private appId: string
private persisted = false
private clearedOnLogout = false

constructor(appId: string) {
this.appId = appId
}

persist(persist: boolean = true): StorageBuilder {
this.persisted = persist
return this
}

clearOnLogout(clear: boolean = true): StorageBuilder {
this.clearedOnLogout = clear
return this
}

build(): Storage {
return new ScopedStorage(
this.appId,
this.persisted ? window.localStorage : window.sessionStorage,
!this.clearedOnLogout
)
}
private appId: string
private persisted = false
private clearedOnLogout = false

constructor(appId: string) {
this.appId = appId
}

persist(persist: boolean = true): StorageBuilder {
this.persisted = persist
return this
}

clearOnLogout(clear: boolean = true): StorageBuilder {
this.clearedOnLogout = clear
return this
}

build(): Storage {
return new ScopedStorage(
this.appId,
this.persisted ? window.localStorage : window.sessionStorage,
!this.clearedOnLogout,
)
}

}

0 comments on commit 7c9026d

Please sign in to comment.