6.0.1 • Published 5 years ago

lodash-decorators v6.0.1

Weekly downloads
109,386
License
MIT
Repository
github
Last release
5 years ago

lodash-decorators

Decorators using lodash functions. View the API docs for more in depth documentation.

Build Status npm version

Install

npm install --save lodash lodash-decorators

Polyfills

This library requires Map and WeakMap to be available globally. If Map or WeakMap is not supported in your environment then use a polyfill.

Usage

For more in depth documentation please visit Lodash

Decorators are exported as both start case and lower case.

import { Debounce } from 'lodash-decorators';

is the same as

import { debounce } from 'lodash-decorators';

They can also be imported directly.

import Debounce from 'lodash-decorators/debounce';

Decorators

These decorators are included in the package. These are also exported as lowercase for those who prefer lowercase decorators.

  • After
  • AfterAll
  • Ary
  • Attempt
  • Before
  • BeforeAll
  • Bind
  • BindAll
  • Curry
  • CurryAll
  • CurryRight
  • CurryRightAll
  • Debounce
  • DebounceAll
  • Defer
  • Delay
  • Flip
  • Flow
  • FlowRight
  • Memoize
  • MemoizeAll
  • Mixin
  • Negate
  • Once
  • OnceAll
  • OverArgs
  • Partial
  • PartialRight
  • Rearg
  • Rest
  • Spread
  • Tap
  • Throttle
  • ThrottleAll
  • ThrottleGetter
  • ThrottleSetter
  • Unary
  • Wrap

Example

import { Debounce, Memoize } from 'lodash-decorators';

class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  @Debounce(100)
  save(date) {
    return this.httpService.post(data);
  }

  @Memoize(item => item.id)
  doSomeHeavyProcessing(arg1, arg2) {}
}

Optional Params and Casing

If a decorator does not require params or has optional params then the decorator does not require invocation. Decorators are also exported in lower case as well as start case.

Example

// These are both valid decorator usages.
class Person {
  @Memoize()
  doSomething() {}

  @Memoize
  doSomething2() {}

  @memoize()
  doSomething3() {}

  @memoize
  doSomething4() {}
}

Partials

Some decorators work slightly differently than you would expect them to work than lodash.

  • Partial
  • PartialRight
  • Wrap

These can take a Function as their first argument or a String. If the argument is a String then a Function is resolved from the current object.

Example

import { Partial, Wrap } from 'lodash-decorators'

class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  getName(type) {
    return type === 'firstName' ? this.firstName : this.lastName
  }

  @Partial('getName', 'firstName')
  getFirstName() {}

  @Partial('getName', null)
  getLastName() {}

  @Wrap('getName')
  getUpperCaseName(fn) {
    return fn().toUpperCase();
  }
}

const person = new Person('Joe', 'Smith');

person.getFirstName(); // 'Joe'
person.getLastName(); // 'Smith'
person.getUpperCaseName(); // JOE SMITH

Composition

You can use methods like compose and flow similiar to partials. The arguments are resolved the same way partials are resolved.

Example

import { Flow } from 'lodash-decorators'
import { kebabCase } from 'lodash';

class Person {
  @Flow('getName', kebabCase)
  logName;

  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  getName() {
    return `${this.firstName} ${this.lastName}`;
  }
}

const person = new Person('Joe', 'Smith');

person.logName(); // joe-smith

Instance Decorators

Normally decorators are applied to the prototype method of the class you are working with, but with some of these decorators that is not the desired behavour. These decorators are applied at the instance level.

  • Debounce
  • Throttle
  • Memoize
  • After
  • Before
  • Curry
  • CurryRight
  • Once
  • Flow
  • FlowRight
  • Rearg
  • Negate
  • Flip
  • Bind
  • Partial
  • PartialRight

Mixin

You can mixin methods into a class by using the Mixin decorator.

Example

import { Mixin } from 'lodash-decorators';

const MyOtherApi = {
  someCoolMethod() {
    // Do something cool
  }
};

@Mixin(MyOtherApi)
class Person {}

Person.prototype.someCoolMethod === MyOtherApi.someCoolMethod; // => true

Attempt

You can wrap a method in a lodash attempt method.

Example

import { Attempt } from 'lodash-decorators';

class Person {
  @Attempt()
  throwAnError() {
    throw new Error();
  }

  @Attempt()
  doNotThrowAnError() {
    return '0_o';
  }
}

const person = new Person();

let result = person.throwAnError();

result instanceof Error; // => true

result = person.doNotThrowAnError();

result === '0_o'; // => true

Bind

Bind takes arguments based on lodash's bind and binds the Function to the current instance object.

Example

import { Bind } from 'lodash-decorators'

class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  @Bind()
  getName() {
    return `${this.firstName} ${this.lastName}`;
  }

  // It can also function as a partial
  @Bind('Joe')
  getUpperCaseName(name) {
    return name.toUpperCase();
  }
}

const person = new Person('Joe', 'Smith');

person.getName.call(null); // Joe Smith
person.getUpperCaseName(); // JOE

You can also bind entire classes with bindAll or bind.

Example

import { BindAll } from 'lodash-decorators'

@BindAll()
class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  getName() {
    return `${this.firstName} ${this.lastName}`;
  }
}

const person = new Person('Joe', 'Smith');

person.getName.call(null); // Joe Smith

v4 Breaking Changes

Version 4 is a rewrite of the library and has many breaking changes.

Not all decorators can be applied to or forced on getters/setters.

Only certain decorators make sense to be applied to getters/setters. Before you could specify the target of the decorator like debounce.set(15). This behavior is removed and decorators that make sense to apply to getters/setters are configured to be applied to methods and either the getter or the setter. For example:

class MyClass {
  // This only gets applied to the setter as it doesn't make sense to apply it to the getter.
  @Debounce(1000)
  get value() {
    return this._value;
  }

  set value(val) {
    this._value = val;
  }

  @Debounce(15)
  fn() {}
}

This keeps the API cleaner and doesn't require the developer to know how the decorator applies to the descriptor. Some decorators have explicit version that apply to either getters of setters, such as ThrottleGetter and ThrottleSetter.

No longer force instance decorator onto prototype

There is no longer a Proto decorator attached to instance decorators. Most instance decorators now have a counterpart that applies to the prototype instead of the instance. Debounce.Proto() is now DebounceAll().

All decorators now take arguments

All decorators now take arguments. So instead of @Once you would do @Once(). This keeps the API consistent and doesn't require the developer to remember which decorators take arguments.

Removal of extensions and validation package

All extensions like enumerable have been removed in favor of core-decorators. There may be some slight over lap like debounce and throttle. Fair warning, instance decorators may not play nice with other implementations of instance decorators.

We want to keep lodash decorators focused specifically on lodash specific functions.

Prototype decorator order no longer throws an error

If a prototype decorator comes after an instance decorator it will be ignored since there is no way to apply it in the chain.

Other breaking changes

  • Attempt now takes an argument to line up with lodash API.
  • Bind used on a class no longer delegates to BindAll. Use BindAll instead.
  • Curry, Partial, Flow, FlowRight are now instance decorators.

v4 Improvements

  • Ships with TypeScript typings.
  • Predictable performance.
  • Improvements to Bind decorator.
  • Improved API for decorator factory.
  • More and better unit tests.
  • Better performance with instance decorators.
  • Single imports with import { Debounce } from 'lodash-decorators/debounce';
  • Composition decorators can be used on properties. These will generate the composed function.
front-template@ytd/fe-vue@ytd/fiber-tool-front@ytd/lite-vue@ytd/odn-tool-frontd5-layout@redwoodjs/structureant-design-pro-scaffoldant-design-pro-csilottieyunjian-player-testyj-player-beta@rem-js/rem-ant-design-provue-ant-patterns-cicdcommon-xuhzero-boothzero-boot-purehzero-front-runtimesimple-antd-management-fast-frameworkzcx-uimaycur-business@everything-registry/sub-chunk-2094snbc-paas-bootcpaas-boot@geekcojp/gpkxt-weblets-fetch-wrapperhiredchina_webanthilo-adapterhht-sdkicon-font-importerhy-design-zzlkb-smartkalazango1d-deploy-testlambdragonlearn-publish-npmlc-ant-reactjh-bigdatajltdloggingmanagementmoon-core-backendmoon-core-backend-typesnode-novel-infongx-json-schema-formnnusercentermtg-api-for-deckmhc-adminhp-antd-plusheader-sider-layoutjujin-dvinspection-agent-ljpintercom-reactjylibmushiny-componentnest-reactng-ansynnpo-platform-uilotus-scaffold-micro-antdlotus-scaffold-micro-antd-subkhusamov-express-typescriptdt-menu-managedumbosdtusercenterdtusercentercomphealthshare_adminfeeweefr-schema-antd-schemafr-schema-antd-utilsgatsby-theme-ant-docsgatsby-theme-antdsitegithub-repo-toolsform-table-autofillform-select-inputflavioomni-layout@wct_npm/hs-ui-lib@zalastax/nolb-loda@webex/plugin-phone@webex/media-engine-webrtcprivate_mng_comps@threekit/polaris@threekit/shopify-javascript-utilities@t-ui/pro-layout@vats-front/core-mobile@theling/pro-layoutreact-common-kitair-ant-design-proowenflyowenfly-bi@ssjs/collective-look-widgetpackagejson-demopages-config@amc-udk/coreasasasasa@anterostecnologia/anteros-react-designeditor@ansyn/ansyn@ansyn/corereact-slickhyjant-ts-pro
6.0.1

5 years ago

6.0.0

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.5.0

6 years ago

4.4.1

7 years ago

4.4.0

7 years ago

4.3.5

7 years ago

4.3.4

7 years ago

4.3.3

7 years ago

4.3.2

7 years ago

4.3.1

7 years ago

4.2.1

7 years ago

4.2.0

7 years ago

4.1.0

7 years ago

4.0.3

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.0.2

7 years ago

3.0.1

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

3.0.0

8 years ago

2.0.0

8 years ago

1.1.0

8 years ago

1.0.6

8 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.4.13

9 years ago

0.4.12

9 years ago

0.4.11

9 years ago

0.4.10

9 years ago

0.4.9

9 years ago

0.4.8

9 years ago

0.4.7

9 years ago

0.4.6

9 years ago

0.4.5

9 years ago

0.4.4

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago