Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

hash-accessor

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hash-accessor

Typescript Hash Accessor Decorator

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Hash Accessor

Defines accessors to map class properties to a hash / object.

Creates getter and setter for given properties to map against a given hash.

You can use HashAccessor when using data objects (like from an API) within classes without the hassle of synchronizing classes' properties with data hashes.

You can define a mapping for each property in each class that is automatically applied every time you access the decorated properties.

Basic Usage

@DeclareHash({
  setter: (instance: User, key: string, value: any) => {
    instance.data[key] = value;
  },
  getter: (instance: User, key: string) => {
    return instance.data[key];
  }
})
class User {
  public data: {[key: string]: any};
  
  @Accessor() public id: number;
  @Accessor('full_name') public fullName: string;
  @Accessor('email_address') public email: string;
  
  public constructor(data?: {[key: string]: any}) {
    this.data = data;
  }
}

let user1: User = new User({
  id: 1,
  full_name: 'John Smith',
  email_address: 'john@conclurer.com'
});

console.log(user1.email); // => 'john@conclurer.com'

user1.fullName = 'The Doctor';
console.log(user1.data['full_name']); // => 'The Doctor'

How to use it

  • Use @DeclareHash() to decorate an object and define setters and getters for the properties hash
  • Use @Accessor() for each property withing the decorated object to store in the object's hash. You can also supply a custom key for each @Accessor() to synchronize the property with.
@Accessor() protected prop1: any; // will be linked with key 'prop1'
@Accessor('otherKey') protected prop2: any; // will be linked with key 'otherKey'

Installation

Run

npm install --save hash-accessor

Keywords

typescript

FAQs

Package last updated on 16 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts