Options
All
  • Public
  • Public/Protected
  • All
Menu

Summary

A utility unit designed to encapsulate any Storage implementation.

Structure

Description

Designed and implemented to be in full, drop-in replacement compatibility with the native Storage API both interface and behavior-wise, it empowers developers to create namespaced Storage scopes. It allows you to subdivide Storage implementations into smaller groups of Storage units with reduced focus to their smaller scope.

Due to its dependency on Proxy, instances may only be created via the factory method #create:

const storage: Storage = NamespacedStorage.create(window.localStorage, 'example');
const nestedStorage: Storage = NamespacedStorage.create(storage, 'nested');
const deepestStorage: Storage = NamespacedStorage.create(storage, 'deepest');

Important Tip

Despite the contemporary trends with TypeScript on the UI or the Back-End, please, make sure to always depend on interfaces instead of implementations. It will help you keep your software as dynamic and flexible as it can be.

Software is meant to be soft. Keep it that way.

Perks

Get & Set Items (AKA: CRUD)

As per the official Storage API, it allows for IO operations both via methods and properties. Take the following example:

The following lines of code all set the same value to the same storage unit

storage.cool    = 'It\'s awesome!';
storage['cool'] = 'It\'s awesome!';
localStorage['example.cool'] = 'It\'s awesome!';
storage.setItem('cool', 'It\'s awesome!');
localStorage.setItem('example.cool', 'It\'s awesome!');

The following lines of code all return the same value written to the same storage unit

console.info(storage.getItem('cool'));
console.info(storage.cool);
console.info(storage['cool']);
console.info(localStorage.getItem('example.cool'));
console.info(localStorage['example.cool']);

A more practical example for property accessors:

type TodoItem = {
  summary?: string,
  priority?: number
}

function increasePriority(todoItem: TodoItem): void {
  todoItem.priority = (todoItem.priority ?? 0) + 1;
}

const todos: Storage = NamespacedStorage.create(storage, 'todoItems');
const todo: Storage & TodoItem = NamespacedStorage.create(todos, 'item1');
increasePriority(todo);
increasePriority(todo);
increasePriority(todo);
// returns String("3")
console.info(localStorage.getItem('example.todoItem.item1.priority'));

Scope-Reduced interactions

A namespaced storage will only execute its methods within its namespace. This implies, that the keys which belong a specific namespace are strongly connected to the given instance. When running clear, key, or getting length, it behaves as expected: all operations & information is restricted to the namespace only.

version

1.0.0

since

1.0.0

see

MDN Proxy Object

see

MDN Storage Interface

Hierarchy-Diagram

UML class diagram of NamespacedStorage

Legend

icon for a class in the UML class diagram class
icon for a private property in the UML class diagram private property
icon for a public method in the UML class diagram public method
icon for a private method in the UML class diagram private method
underlined static property/method

Hierarchy

  • NamespacedStorage

Implements

  • Storage

Indexable

[name: string]: unknown

Summary

A utility unit designed to encapsulate any Storage implementation.

Structure

Description

Designed and implemented to be in full, drop-in replacement compatibility with the native Storage API both interface and behavior-wise, it empowers developers to create namespaced Storage scopes. It allows you to subdivide Storage implementations into smaller groups of Storage units with reduced focus to their smaller scope.

Due to its dependency on Proxy, instances may only be created via the factory method #create:

const storage: Storage = NamespacedStorage.create(window.localStorage, 'example');
const nestedStorage: Storage = NamespacedStorage.create(storage, 'nested');
const deepestStorage: Storage = NamespacedStorage.create(storage, 'deepest');

Important Tip

Despite the contemporary trends with TypeScript on the UI or the Back-End, please, make sure to always depend on interfaces instead of implementations. It will help you keep your software as dynamic and flexible as it can be.

Software is meant to be soft. Keep it that way.

Perks

Get & Set Items (AKA: CRUD)

As per the official Storage API, it allows for IO operations both via methods and properties. Take the following example:

The following lines of code all set the same value to the same storage unit

storage.cool    = 'It\'s awesome!';
storage['cool'] = 'It\'s awesome!';
localStorage['example.cool'] = 'It\'s awesome!';
storage.setItem('cool', 'It\'s awesome!');
localStorage.setItem('example.cool', 'It\'s awesome!');

The following lines of code all return the same value written to the same storage unit

console.info(storage.getItem('cool'));
console.info(storage.cool);
console.info(storage['cool']);
console.info(localStorage.getItem('example.cool'));
console.info(localStorage['example.cool']);

A more practical example for property accessors:

type TodoItem = {
  summary?: string,
  priority?: number
}

function increasePriority(todoItem: TodoItem): void {
  todoItem.priority = (todoItem.priority ?? 0) + 1;
}

const todos: Storage = NamespacedStorage.create(storage, 'todoItems');
const todo: Storage & TodoItem = NamespacedStorage.create(todos, 'item1');
increasePriority(todo);
increasePriority(todo);
increasePriority(todo);
// returns String("3")
console.info(localStorage.getItem('example.todoItem.item1.priority'));

Scope-Reduced interactions

A namespaced storage will only execute its methods within its namespace. This implies, that the keys which belong a specific namespace are strongly connected to the given instance. When running clear, key, or getting length, it behaves as expected: all operations & information is restricted to the namespace only.

Index

Constructors

Private constructor

  • new NamespacedStorage(storage: Storage, namespace: string, keys: string[]): NamespacedStorage

Properties

Private keys

keys: string[]

Private Readonly namespace

namespace: string

Private Readonly storage

storage: Storage

Private Static namespaceSeparator

namespaceSeparator: string = '.'

Private Static proxyHandler

proxyHandler: ProxyHandler<Storage> = ...

Accessors

length

  • get length(): number
  • Tells the number of entries within the given NamespacedStorage.

    version

    1.0.0

    since

    1.0.0

    Returns number

    Number of entries within the namespace.

Methods

clear

  • clear(): void
  • Removes all storage entries keyed to belong to the current NamespacedStorage.

    version

    1.0.0

    since

    1.0.0

    Returns void

getItem

  • getItem(key: string): null | string
  • Returns the queried entry within the encapsulated namespace.

    version

    1.0.0

    since

    1.0.0

    Parameters

    • key: string

    Returns null | string

key

  • key(index: number): null | string
  • Returns the indexth key encapsulated by the NamespacedStorage.

    version

    1.0.0

    since

    1.0.0

    Parameters

    • index: number

    Returns null | string

Private packNamespacedKey

  • packNamespacedKey(key: string): string

removeItem

  • removeItem(key: string): void
  • Removes the item stored under the given key within the encapsulated namespace.

    version

    1.0.0

    since

    1.0.0

    Parameters

    • key: string

    Returns void

setItem

  • setItem(key: string, value: string): void
  • Sets the value under the given key within the encapsulated namespace.

    version

    1.0.0

    since

    1.0.0

    Parameters

    • key: string
    • value: string

    Returns void

Private unpackNamespacedKey

  • unpackNamespacedKey(key: string): string

Static create

  • create(storage: Storage, namespace: string): Storage
  • Creates a new NamespacedStorage instance with the appropriate Proxy handler in place.

    version

    1.0.0

    since

    1.0.0

    Parameters

    • storage: Storage

      The Storage object to encapsulate. Any Storage implementation.

    • namespace: string

      The string to use as a namespace for the newly created NamespacedStorage.

    Returns Storage

    The newly created namespaced storage object.

Generated using TypeDoc