项目作者: caiogondim

项目描述 :
:tophat: Classy decorators
高级语言: JavaScript
项目地址: git://github.com/caiogondim/monocle-decorators.js.git
创建时间: 2017-02-02T18:38:39Z
项目社区:https://github.com/caiogondim/monocle-decorators.js

开源协议:

下载


monocle-decorators


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

  1. 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)

  1. import _o from 'monocle-decorators'
  2. class Walkable {
  3. walk() {
  4. const speed = 5
  5. this.distanceFromOrigin += speed
  6. }
  7. }
  8. class Runnable {
  9. run() {
  10. const speed = 10
  11. this.distanceFromOrigin += speed
  12. }
  13. }
  14. @_o.mixin([Walkable, Runnable])
  15. class Thing {
  16. constructor() {
  17. this.distanceFromOrigin = 0
  18. }
  19. }
  20. const foo = new Thing()
  21. foo.walk() // method from Walkable class
  22. foo.run() // method from Runnable class
  23. foo.distanceFromOrigin // => 15

💡 Tip

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

  1. import _o from 'monocle-decorators'
  2. const walkable = {
  3. walk() {
  4. const speed = 5
  5. this.distanceFromOrigin += speed
  6. }
  7. }
  8. const runnable = {
  9. run() {
  10. const speed = 10
  11. this.distanceFromOrigin += speed
  12. }
  13. }
  14. @_o.mixin([walkable, runnable])
  15. class Thing {
  16. constructor() {
  17. this.distanceFromOrigin = 0
  18. }
  19. }
  20. const foo = new Thing()
  21. foo.walk() // method from Walkable class
  22. foo.run() // method from Runnable class
  23. foo.distanceFromOrigin // => 15

As function _o.mixin(TargetClass, ArrayOfMixins)

  1. import _o from 'monocle-decorators'
  2. _o.mixin(Thing, [Walkable, Runnable])
  3. const foo = new Thing()
  4. foo.walk() // method from Walkable class
  5. foo.run() // method from Runnable class
  6. 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

  1. import _o from 'monocle-decorators'
  2. @_o.freeze
  3. class Dummy {
  4. constructor() {
  5. this.a = 1
  6. this.b = 2
  7. }
  8. }
  9. const foo = new Dummy()
  10. foo.c = 3 // throws Error

As function _o.freeze(TargetClass)

  1. import _o from 'monocle-decorators'
  2. const DummyFrozen = _o.freeze(Dummy)
  3. const foo = new DummyFrozen()
  4. 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

  1. import _o from 'monocle-decorators'
  2. @_o.seal
  3. class Dummy {
  4. constructor() {
  5. this.a = 1
  6. this.b = 2
  7. }
  8. }
  9. const foo = new Dummy()
  10. foo.c = 3 // throws Error

As function _o.freeze(TargetClass)

  1. import _o from 'monocle-decorators'
  2. const DummySealed = _o.seal(Dummy)
  3. foo.c = 3 // throws Error

Decorators for instance methods/properties

@_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

  1. import _o from 'monocle-decorators'
  2. class Dummy {
  3. @_o.bind
  4. handleClick() {
  5. // ...
  6. }
  7. render() {
  8. return (
  9. <div onClick={this.handleClick}>Lorem ipsum</div>
  10. )
  11. }
  12. }

As function _o.bind(targetMethod, context)

  1. import _o from 'monocle-decorators'
  2. const obj = {
  3. handleClick() {
  4. // ...
  5. }
  6. }
  7. _o.bind(obj.handleClick, obj)
  8. 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)

  1. import _o from 'monocle-decorators'
  2. class Dummy {
  3. @_o.debounce(150)
  4. onScroll() {
  5. // ...
  6. }
  7. }

As function _o.debounce(targetMethod, wait)

  1. import _o from 'monocle-decorators'
  2. const onScroll = _o.debounce(() => {
  3. // ...
  4. }, 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)

  1. import _o from 'monocle-decorators'
  2. class Dummy {
  3. @_o.throttle(150)
  4. onScroll() {
  5. // ...
  6. }
  7. }

💡 Tip

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

As function _o.throttle(targetMethod, wait)

  1. import _o from 'monocle-decorators'
  2. const onScroll = _o.throttle(() => {
  3. // ...
  4. }, 150)

@_o.deprecate

Calls opts.logger with msg as depreciation message.
By default opts.logger is console.warn and msg is ${target.constructor.name}.${key} is deprecated.. Both are optional.

As decorator @_o.deprecate(msg, { logger })

  1. import _o from 'monocle-decorators'
  2. class Dummy {
  3. a() {
  4. // ...
  5. }
  6. @_o.deprecate('`dummy.b` is deprecated. Use `dummy.a`')
  7. b() {
  8. // ...
  9. }
  10. }

As function _o.deprecate(target, key, msg, { logger })

  1. import _o from 'monocle-decorators'
  2. const dummy = _o.deprecate({
  3. a() {},
  4. b() {}
  5. }, 'b', '`dummy.b` is deprecated. Use `dummy.a`')

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