Giter VIP home page Giter VIP logo

monocle-decorators.js's Introduction

monocle-decorators.js

ย Travis CI File size Code coverage

Tiny library with most common/useful decorators. Think of it as underscore.js, but with class.

Table of contents

Installation

npm install monocle-decorators --save

Decorators for classes

@_o.mixin

Extends decorated class with all enumerable properties from ArrayOfMixins passed as argument.

๐Ÿ’ก Tip

Prefer composability over inheritance.

As decorator @_o.mixin(ArrayOfMixins)

import _o from 'monocle-decorators'

class Walkable {
  walk() {
    const speed = 5
    this.distanceFromOrigin += speed
  }
}

class Runnable {
  run() {
    const speed = 10
    this.distanceFromOrigin += speed
  }
}

@_o.mixin([Walkable, Runnable])
class Thing {
  constructor() {
    this.distanceFromOrigin = 0
  }
}

const foo = new Thing()
foo.walk() // method from Walkable class
foo.run() // method from Runnable class
foo.distanceFromOrigin // => 15

๐Ÿ’ก Tip

Array of mixins can also be an array of objects, if you don't feel classy.

import _o from 'monocle-decorators'

const walkable = {
  walk() {
    const speed = 5
    this.distanceFromOrigin += speed
  }
}

const runnable = {
  run() {
    const speed = 10
    this.distanceFromOrigin += speed
  }
}

@_o.mixin([walkable, runnable])
class Thing {
  constructor() {
    this.distanceFromOrigin = 0
  }
}

const foo = new Thing()
foo.walk() // method from Walkable class
foo.run() // method from Runnable class
foo.distanceFromOrigin // => 15

As function _o.mixin(TargetClass, ArrayOfMixins)

import _o from 'monocle-decorators'

_o.mixin(Thing, [Walkable, Runnable])

const foo = new Thing()
foo.walk() // method from Walkable class
foo.run() // method from Runnable class
foo.distanceFromOrigin // => 15

@_o.freeze

Freezes every new instance of decorated class.

A frozen object prevents:

  • new properties from being added to it
  • existing properties from being removed
  • existing properties, or their enumerability, configurability, or writability, from being changed

๐Ÿ’ก Tip

@_o.seal and @_o.freeze makes it easier to work with objects, since you have to declare beforehand all properties and methods an object has and will have in it's lifecycle, concentrating in one single place the definition of the object structure.

As decorator @_o.freeze

import _o from 'monocle-decorators'

@_o.freeze
class Dummy {
  constructor() {
    this.a = 1
    this.b = 2
  }
}

const foo = new Dummy()
foo.c = 3 // throws Error

As function _o.freeze(TargetClass)

import _o from 'monocle-decorators'

const DummyFrozen = _o.freeze(Dummy)
const foo = new DummyFrozen()
foo.c = 3 // throws Error

@_o.seal

Seals every new instance of decorated class.

A sealed object prevents:

  • new properties from being added to it
  • marking all existing properties as non-configurable

Values of present properties can still be changed as long as they are writable.

๐Ÿ’ก Tip

@_o.seal and @_o.freeze makes it easier to work with objects, since you have to declare beforehand all properties and methods an object has and will have in it's lifecycle, concentrating in one single place the definition of the object structure.

As decorator @_o.seal

import _o from 'monocle-decorators'

@_o.seal
class Dummy {
  constructor() {
    this.a = 1
    this.b = 2
  }
}

const foo = new Dummy()
foo.c = 3 // throws Error

As function _o.freeze(TargetClass)

import _o from 'monocle-decorators'

const DummySealed = _o.seal(Dummy)
foo.c = 3 // throws Error

Decorators for instance methods

@_o.bind

Autobind the decorated method to it's owner, so this will always refer to the object that owns the method.

๐Ÿ’ก Tip

This decorator avoids the verbose <button onClick={this.handleClick.bind(this)}></button> idiom, using only <button onClick={this.handleClick}></button>.

As decorator @_o.bind

import _o from 'monocle-decorators'

class Dummy {
  @_o.bind
  handleClick() {
    // ...
  }

  render() {
    return (
      <div onClick={this.handleClick}>Lorem ipsum</div>
    )
  }
}

As function _o.bind(targetMethod, context)

import _o from 'monocle-decorators'

const obj = {
  handleClick() {
    // ...
  }
}

_o.bind(obj.handleClick, obj)

element.addEventListener('click', obj.handleClick)

@_o.debounce

Debounces decorated method, which will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked.

๐Ÿ’ก Tip

Useful for implementing behavior that should only happen after the input has stopped arriving. For example: rendering a preview of a Markdown comment, recalculating a layout after the window has stopped being resized, and so on.

As decorator @_o.debounce(wait)

import _o from 'monocle-decorators'

class Dummy {
  @_o.debounce(150)
  onScroll() {
    // ...
  }
}

As function _o.debounce(targetMethod, wait)

import _o from 'monocle-decorators'

const onScroll = _o.debounce(() => {
  // ...
}, 150)

@_o.throttle

Throttles decorated method, that, when invoked repeatedly, will only actually call the original function at most once per every wait milliseconds.

๐Ÿ’ก Tip

Useful for rate-limiting events that occur faster than you can keep up with.

As decorator @_o.throttle(wait)

import _o from 'monocle-decorators'

class Dummy {
  @_o.throttle(150)
  onScroll() {
    // ...
  }
}

๐Ÿ’ก Tip

To have the same behavior as a hypothetical @_o.once, use @_o.throttle(Infinity).

As function _o.throttle(targetMethod, wait)

import _o from 'monocle-decorators'

const onScroll = _o.throttle(() => {
  // ...
}, 150)

Why monocle?

Because you import it as _o and use it as @_o. Classy decorators.

Reference

  • Icon by Ben Iconator from The Noun Prokect

Sponsor

If you found this library useful and are willing to donate, consider transferring some bitcoins to 1BqqKiZA8Tq43CdukdBEwCdDD42jxuX9UY.


caiogondim.com ย ยทย  GitHub @caiogondim ย ยทย  Twitter @caio_gondim

monocle-decorators.js's People

Contributors

caiogondim avatar callahanchris avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.