functional
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
const Flags = {
|
||||
dir: './storage'
|
||||
dir: './storage',
|
||||
}
|
||||
|
||||
export function setFlag<T extends keyof typeof Flags>(key: T, val: typeof Flags[T]) {
|
||||
export function setFlag<T extends keyof typeof Flags>(
|
||||
key: T,
|
||||
val: typeof Flags[T]
|
||||
) {
|
||||
Flags[key] = val
|
||||
}
|
||||
|
||||
export function getFlag(key: keyof typeof Flags) {
|
||||
return Flags[key]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { Guild } from 'discord.js'
|
||||
import { readFileSync, writeFileSync, ensureDirSync, ensureFileSync } from 'fs-extra'
|
||||
import {
|
||||
readFileSync,
|
||||
writeFileSync,
|
||||
ensureDirSync,
|
||||
ensureFileSync,
|
||||
} from 'fs-extra'
|
||||
import { getFlag } from './flags'
|
||||
|
||||
type storageTypes = 'global' | Guild
|
||||
|
||||
32
src/main.ts
32
src/main.ts
@@ -1,25 +1,7 @@
|
||||
import { setFlag } from "./flags";
|
||||
import { globalStorage, initGlobalCache } from './storage'
|
||||
|
||||
function test2() {
|
||||
let storage = globalStorage()
|
||||
storage.set({
|
||||
a: 'aaaaa',
|
||||
b: 'bbbbb',
|
||||
c: 'ccccc',
|
||||
})
|
||||
}
|
||||
|
||||
function test() {
|
||||
setFlag('dir', './data')
|
||||
initGlobalCache({
|
||||
a: 'aaa',
|
||||
b: 'bbb',
|
||||
})
|
||||
let storage = globalStorage()
|
||||
console.log(storage.get())
|
||||
test2()
|
||||
console.log(storage.get())
|
||||
}
|
||||
|
||||
test()
|
||||
export { setFlag } from './flags'
|
||||
export {
|
||||
globalStorage,
|
||||
guildStorage,
|
||||
initGlobalCache,
|
||||
initGuildCache,
|
||||
} from './storage'
|
||||
|
||||
@@ -14,7 +14,10 @@ export function initGlobalCache(defaultValue?: string | JSONObject) {
|
||||
}
|
||||
}
|
||||
|
||||
export function initGuildCache(guild: Guild, defaultValue?: string | JSONObject) {
|
||||
export function initGuildCache(
|
||||
guild: Guild,
|
||||
defaultValue?: string | JSONObject
|
||||
) {
|
||||
if (!GuildCache.has(guild.id)) {
|
||||
let data = readFile(guild)
|
||||
GuildCache.set(guild.id, defaultJson(data, defaultValue))
|
||||
@@ -45,4 +48,4 @@ export function guildStorage(guild: Guild) {
|
||||
},
|
||||
}
|
||||
return new GuildStorage(guild, val)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,4 +43,4 @@ export class GuildStorage extends StorageBase {
|
||||
super(val)
|
||||
this.guild = guild
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
src/util.ts
11
src/util.ts
@@ -3,8 +3,10 @@ export type JSONObject<Value = JSONDataTypes, Key extends string = string> = {
|
||||
[key in Key]: Value
|
||||
}
|
||||
|
||||
export function defaultJson(data: string, defaultValue: string | JSONObject = '') {
|
||||
|
||||
export function defaultJson(
|
||||
data: string,
|
||||
defaultValue: string | JSONObject = ''
|
||||
) {
|
||||
if (data.length === 0) {
|
||||
//no existing data, give the default value instead
|
||||
|
||||
@@ -13,7 +15,6 @@ export function defaultJson(data: string, defaultValue: string | JSONObject = ''
|
||||
} else {
|
||||
return JSON.stringify(defaultValue)
|
||||
}
|
||||
|
||||
} else if (typeof defaultValue !== 'string') {
|
||||
//existing data should be compared
|
||||
//nothing to compare if default value is string
|
||||
@@ -36,9 +37,7 @@ export function defaultJson(data: string, defaultValue: string | JSONObject = ''
|
||||
//couldn't parse data
|
||||
return JSON.stringify(defaultValue)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user