Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

macroable

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

macroable

A simple ES6 class that can be extended to provide macros and getters functionality

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
24K
decreased by-7.8%
Maintainers
1
Weekly downloads
 
Created
Source

Macroable

Macroable is a small ES6 class that can be extended to add functionality of macros and getters to your own class.

It is helpful if you want to expose an API to get your classes extended.

This library is used by AdonisJs to offer extend interface for core class.

Installation

As always, pull it from npm.

npm i --save macroable

Usage

const Macroable = require('macroable')

class User extends Macroable {

}

// it's required to define empty objects for macros and getters.
User._macros = {}
User._getters = {}



Once your class extends Macroable class, it get's a bunch of static methods to define macros and getters.

Using Macros

User.macro('getUsers', function () {
  // do some work
})

and now you can use the method from the class instance.

const user = new User()
user.getUsers()



Using Getters

Getters are values evaluated everytime someone access them.

User.getter('username', function () {
	// return username
})

and now you can use the property from the class instance

const user = new User()
user.username



Singleton Getters

Calling the getter callback everytime may be unrequired, since you do not want to re-compute the values. A singleton getter can also be defined.

User.getter('username', function () {
	// I am only called once
}, true)
const user = new User()

user.username // invokes callback and caches value
user.username // returns from cache



Hydrating Class

If for some reason you want to remove all getters and macros, you can call the hydrate method.

User.macro('getUsers', callback)

User.hasMacro('getUsers') // true

User.hydrate()

User.hasMacro('getUsers') // false



Checking Existence

You can also find whether a getter or macro already exists or not.

User.hasMacro('getUsers')
User.hasGetter('username')

Keywords

FAQs

Package last updated on 01 Jun 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc