Skip to content

Commit

Permalink
Update localStorage to work with empty namespace
Browse files Browse the repository at this point in the history
Update localStorage to work with empty namespace
  • Loading branch information
sabio committed Oct 27, 2023
1 parent ac8fd8f commit 4f8f4e0
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/storage/localStorage/localStorage.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { parse, stringify } from 'src/utils/json';
import { JSONValue, parse, stringify } from 'src/utils/json';
import { memo } from 'src/utils/function';
import { isNull, isUndefined } from 'src/utils/object';

export const DEFAULT_LS_PREFIX = '_ym';

export interface LocalStorage {
isBroken: boolean;
getVal<T>(name: string): T | null;
getVal<T>(name: string, defVal: T): T;
setVal<T>(name: string, val: T): LocalStorage;
getVal<T extends JSONValue>(name: string): T | null;
getVal<T extends JSONValue>(name: string, defVal: T): T;
setVal<T extends JSONValue>(name: string, val: T): LocalStorage;
delVal(name: string): LocalStorage;
}

Expand Down Expand Up @@ -61,13 +61,16 @@ export const localStorage = (
nameSpace: string | number = '',
prefix: string = DEFAULT_LS_PREFIX,
): LocalStorage => {
const storageKey = `${prefix}${nameSpace}_`;
let storageKey = `${prefix}${nameSpace}`;
if (storageKey) {
storageKey = `${storageKey}_`;
}
const isBroken = isStorageBroken(ctx);

return {
isBroken,
getVal<T>(name: string, defVal?: T) {
const out = getItem(ctx, `${storageKey}${name}`) as T | null;
const out = getItem(ctx, `${storageKey}${name}`);
if (isNull(out) && !isUndefined(defVal)) {
return defVal;
}
Expand Down

0 comments on commit 4f8f4e0

Please sign in to comment.