template_0205

This commit is contained in:
Sofio
2026-02-05 13:16:05 +08:00
commit d93e4d9c9f
197 changed files with 52810 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
import { MMKV } from "react-native-mmkv"
export const storage = new MMKV()
/**
* Loads a string from storage.
*
* @param key The key to fetch.
*/
export function loadString(key: string): string | null {
try {
return storage.getString(key) ?? null
} catch {
// not sure why this would fail... even reading the RN docs I'm unclear
return null
}
}
/**
* Saves a string to storage.
*
* @param key The key to fetch.
* @param value The value to store.
*/
export function saveString(key: string, value: string): boolean {
try {
storage.set(key, value)
return true
} catch {
return false
}
}
/**
* Loads something from storage and runs it thru JSON.parse.
*
* @param key The key to fetch.
*/
export function load<T>(key: string): T | null {
let almostThere: string | null = null
try {
almostThere = loadString(key)
return JSON.parse(almostThere ?? "") as T
} catch {
return (almostThere as T) ?? null
}
}
/**
* Saves an object to storage.
*
* @param key The key to fetch.
* @param value The value to store.
*/
export function save(key: string, value: unknown): boolean {
try {
saveString(key, JSON.stringify(value))
return true
} catch {
return false
}
}
/**
* Removes something from storage.
*
* @param key The key to kill.
*/
export function remove(key: string): void {
try {
storage.delete(key)
} catch {}
}
/**
* Burn it all to the ground.
*/
export function clear(): void {
try {
storage.clearAll()
} catch {}
}

View File

@@ -0,0 +1,61 @@
import { load, loadString, save, saveString, clear, remove, storage } from "."
const VALUE_OBJECT = { x: 1 }
const VALUE_STRING = JSON.stringify(VALUE_OBJECT)
describe("MMKV Storage", () => {
beforeEach(() => {
storage.clearAll()
storage.set("string", "string")
storage.set("object", JSON.stringify(VALUE_OBJECT))
})
it("should be defined", () => {
expect(storage).toBeDefined()
})
it("should have default keys", () => {
expect(storage.getAllKeys()).toEqual(["string", "object"])
})
it("should load data", () => {
expect(load<object>("object")).toEqual(VALUE_OBJECT)
expect(loadString("object")).toEqual(VALUE_STRING)
expect(load<string>("string")).toEqual("string")
expect(loadString("string")).toEqual("string")
})
it("should save strings", () => {
saveString("string", "new string")
expect(loadString("string")).toEqual("new string")
})
it("should save objects", () => {
save("object", { y: 2 })
expect(load<object>("object")).toEqual({ y: 2 })
save("object", { z: 3, also: true })
expect(load<object>("object")).toEqual({ z: 3, also: true })
})
it("should save strings and objects", () => {
saveString("object", "new string")
expect(loadString("object")).toEqual("new string")
})
it("should remove data", () => {
remove("object")
expect(load<object>("object")).toBeNull()
expect(storage.getAllKeys()).toEqual(["string"])
remove("string")
expect(load<string>("string")).toBeNull()
expect(storage.getAllKeys()).toEqual([])
})
it("should clear all data", () => {
expect(storage.getAllKeys()).toEqual(["string", "object"])
clear()
expect(storage.getAllKeys()).toEqual([])
})
})