Skip to content

Commit

Permalink
chore: use ?? instead of || where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Pkmmte committed Dec 16, 2022
1 parent b43cc27 commit e5df286
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Stashy {
backend,
id,
logger
} = options || {};
} = options ?? {};

// IDs are often used to separate out data storage into different entry groups
this._id = id;
Expand All @@ -53,9 +53,9 @@ export class Stashy {

// Prepare our backing storage backend(s)
// Defaults only get assigned if currently running in that platform
this._backendNative = backend?.['native'] || (backend?.['_init'] ? backend : getDefaultBackend('native'));
this._backendSsr = backend?.['ssr'] || (backend?.['_init'] ? backend : getDefaultBackend('ssr'));
this._backendWeb = backend?.['web'] || (backend?.['_init'] ? backend : getDefaultBackend('web'));
this._backendNative = backend?.['native'] ?? (backend?.['_init'] ? backend : getDefaultBackend('native'));
this._backendSsr = backend?.['ssr'] ?? (backend?.['_init'] ? backend : getDefaultBackend('ssr'));
this._backendWeb = backend?.['web'] ?? (backend?.['_init'] ? backend : getDefaultBackend('web'));
this._backend()._init({ id });
}

Expand Down Expand Up @@ -103,57 +103,57 @@ export class Stashy {
const encodedValue = this._backend(options).getString(key, options);
const value = encodedValue ? JSON.parse(encodedValue) : null;
this._log('debug', `get(${key}) value is:`, value);
return value || options?.default;
return value ?? options?.default;
};

public async getAsync<T>(key: string, options?: StashyOptions): Promise<T> {
this._log('trace', `getAsync(${key}) with options:`, options);
const encodedValue = await this._backend(options).getStringAsync(key, options);
const value = encodedValue ? JSON.parse(encodedValue) : null;
this._log('debug', `getAsync(${key}) value is:`, value);
return value || options?.default;
return value ?? options?.default;
};

public getBoolean(key: string, options?: StashyOptions): boolean {
this._log('trace', `getBoolean(${key}) with options:`, options);
const value = this._backend(options).getBoolean(key, options);
this._log('debug', `getBoolean(${key}) value is:`, value);
return value || options?.default;
return value ?? options?.default;
};

public async getBooleanAsync(key: string, options?: StashyOptions): Promise<boolean> {
this._log('trace', `getBooleanAsync(${key}) with options:`, options);
const value = await this._backend(options).getBooleanAsync(key, options);
this._log('debug', `getBooleanAsync(${key}) value is:`, value);
return value || options?.default;
return value ?? options?.default;
};

public getNumber(key: string, options?: StashyOptions): number {
this._log('trace', `getNumber(${key}) with options:`, options);
const value = this._backend(options).getNumber(key, options);
this._log('debug', `getNumber(${key}) value is:`, value);
return value || options?.default;
return value ?? options?.default;
};

public async getNumberAsync(key: string, options?: StashyOptions): Promise<number> {
this._log('trace', `getNumberAsync(${key}) with options:`, options);
const value = await this._backend(options).getNumberAsync(key, options);
this._log('debug', `getNumberAsync(${key}) value is:`, value);
return value || options?.default;
return value ?? options?.default;
};

public getString(key: string, options?: StashyOptions): string {
this._log('trace', `getString(${key}) with options:`, options);
const value = this._backend(options).getString(key, options);
this._log('debug', `getString(${key}) value is:`, value);
return value || options?.default;
return value ?? options?.default;
};

public async getStringAsync(key: string, options?: StashyOptions): Promise<string> {
this._log('trace', `getStringAsync(${key}) with options:`, options);
const value = await this._backend(options).getStringAsync(key, options);
this._log('debug', `getStringAsync(${key}) value is:`, value);
return value || options?.default;
return value ?? options?.default;
};

public set(key: string, value: boolean | number | string | any, options?: StashyOptions) {
Expand Down

0 comments on commit e5df286

Please sign in to comment.