macroable

  • Version 7.0.2
  • Published
  • 13.5 kB
  • No dependencies
  • MIT license

Install

npm i macroable
yarn add macroable
pnpm add macroable

Overview

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

Index

Classes

class Macroable

abstract class Macroable {}
  • Macroable is an abstract class to add ability to extend your class prototype using better syntax.

    Macroable has handful of benefits over using traditional prototype approach.

    1. Methods or properties added dynamically to the class can be removed using hydrate method. 2. Can define singleton getters.

constructor

constructor();

    property getters

    protected static getters: { [key: string]: GetterFn<any, any> };

      property macros

      protected static macros: { [key: string]: MacroFn<any, any[], any> };

        method getGetter

        static getGetter: (name: string) => GetterFn<any, any> | undefined;
        • Return the existing getter or null if it doesn't exists

        method getMacro

        static getMacro: (name: string) => MacroFn<any, any[], any> | undefined;
        • Return the existing macro or null if it doesn't exists

        method getter

        static getter: <T extends unknown, K extends string>(
        this: new (...args: any) => T,
        name: string,
        callback: GetGetterFn<T, K>,
        singleton?: boolean
        ) => void;
        • Define a getter, which is invoked everytime the value is accessed. This method also allows adding single getters, whose value is cached after first time

          Example 1

          Macroable.getter('time', function () {
          return new Date().getTime()
          })
          console.log(new Macroable().time)
          // Singletons
          Macroable.getter('time', function () {
          return new Date().getTime()
          }, true)
          console.log(new Macroable().time)

        method hasGetter

        static hasGetter: (name: string) => boolean;
        • Returns a boolean telling if a getter exists

        method hasMacro

        static hasMacro: (name: string) => boolean;
        • Returns a boolean telling if a macro exists

        method hydrate

        static hydrate: () => void;
        • Cleanup getters and macros from the class

        method macro

        static macro: <T extends unknown, K extends string>(
        this: new (...args: any) => T,
        name: string,
        callback: GetMacroFn<T, K>
        ) => void;
        • Add a macro to the class. This method is a better to manually adding to class.prototype.method.

          Also macros added using Macroable.macro can be cleared anytime

          Example 1

          Macroable.macro('getUsername', function () {
          return 'virk'
          })

        Interfaces

        interface MacroableConstructorContract

        interface MacroableConstructorContract<T extends any> {}
        • Shape of the macroable constructor

        method getter

        getter: <K extends string>(
        name: K,
        callback: GetGetterFn<T, K>,
        singleton?: boolean
        ) => void;

          method hydrate

          hydrate: () => void;

            method macro

            macro: <K extends string>(name: K, callback: GetMacroFn<T, K>) => void;

              Package Files (1)

              Dependencies (0)

              No dependencies.

              Dev Dependencies (18)

              Peer Dependencies (0)

              No peer dependencies.

              Badge

              To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

              You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/macroable.

              • Markdown
                [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/macroable)
              • HTML
                <a href="https://www.jsdocs.io/package/macroable"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>