@nativescript-use/nativescript-localstorage
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

@nativescript-use/nativescript-localstorage

npm install @nativescript-use/nativescript-localstorage

Use localStorage like in the browser. This library uses ApplicationStorage from NativeScript behind it.

Usage

import localStorage from '@nativescript-use/nativescript-localstorage';

localStorage.setItem('myKey', "myValue");
localStorage.getItem('myKey'); // -> "myValue"
localStorage.has('myKey');
localStorage.removeItem('myKey');
localStorage.length();
localStorage.clear();

Extend LocalStorage class

You can define a storage with a prefix for all entries in ApplicationSettings like:

// BusinessStorage.ts
import { LocalStorage } from '@nativescript-use/nativescript-localstorage';

export enum BusinessKeys {
  NAME = 'name',
}

class BusinessStorage extends LocalStorage<BusinessKeys> {
  prefix = '_businnes_.';
}
const businessStorage = new BusinessStorage();
export default businessStorage;

And use it like.

import businessStorage, { BusinessKeys } from './BusinessStorage';

businessStorage.setItem(BusinessKeys.NAME, 'MyBusiness');
businessStorage.getItem(BusinessKeys.NAME);

This will generate the following entry in ApplicationSettings

_businnes_.name = 'MyBusiness'

Type declaration

declare class LocalStorage<K extends string> {
    protected prefix: string;
    getItem<T>(key: K): T;
    setItem(key: K, data: any): void;
    has(key: K): boolean;
    removeItem(key: K): void;
    length(): number;
    clear(): void;
}
export declare const localStorage: LocalStorage<string>;

License

Apache License Version 2.0

Package Sidebar

Install

npm i @nativescript-use/nativescript-localstorage

Weekly Downloads

2

Version

0.0.1

License

Apache-2.0

Unpacked Size

16.7 kB

Total Files

6

Last publish

Collaborators

  • vallemar89