1.0.1 • Published 5 years ago

shiki-decorators v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Build Status

shiki-decorators

This repository contains set of useful es7 decorators: bind, memoize, chain, throttle and debounce.

@bind uses NoHomey/bind-decorator

Binds method to the current context.

import { bind } from 'shiki-decorators';
// or to import with tree shaking use
// import bind from 'shiki-decorators/src/bind';

class Example {
  a = 'a from example'

  @bind
  foo() {
    console.log(this.a);
  }

  boo() {
    console.log(this.a);
  }
}

const example = new Example();
const z = { a: 'a from z' };
z.foo = example.foo;
z.boo = example.boo;

z.foo();
z.boo();

// Output
// a from example
// a from z

@memoize uses andreypopp/memoize-decorator

A method/getter decorator which is when applied to a method or a getter memoizes the result of the first call and returns it on subsequent calls.

import { memoize } from 'shiki-decorators';
// or to import with tree shaking use
// import memoize from 'shiki-decorators/src/memoize';

class Example {
  @memoize
  foo() {
    console.log('heavy method computations');
    return 'foo';
  }

  @memoize
  get boo() {
    console.log('heavy getter computations');
    return 'boo';
  }
}

let example = new Example();

console.log(example.foo()); // prints '', and then prints "1"
console.log(example.boo); // prints '', and then prints "1"

console.log(example.foo()); // just prints '1'
console.log(example.boo); // just prints '1'

// Output
// heavy method computations
// foo
// heavy getter computations
// boo
// foo
// boo

@chain

Allows chaining execution of a function. Each method returns a called object, allowing the calls to be chained together in a single statement.

import { chain } from 'shiki-decorators';
// or to import with tree shaking use
// import chain from 'shiki-decorators/src/chain';

class Example {
  @chain
  foo(num) {
    console.log('num:', num);
  }
}

const example = new Example();
example.foo(1).foo(2);

// Output
// num: 1
// num: 2

@throttle

Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.

import { throttle } from 'shiki-decorators';
// or to import with tree shaking use
// import throttle from 'shiki-decorators/src/throttle';

class Example {
  @throttle(1000)
  foo(num) {
    console.log('num:', num);
  }
}

const example = new Example();

example.foo(1); // Will execute the callback
example.foo(2); // Won’t execute callback
example.foo(3); // Won’t execute callback

setTimeout(() => {
  example.foo(10); // Will execute the callback
}, 900);

setTimeout(() => {
  example.foo(100); // Will execute the callback
}, 1200);

// Output
// num: 1
// num: 10
// num: 100

@debounce

Debounce execution of a function. Debouncing, unlike throttling, guarantees that a function is only executed a single time, either at the very beginning of a series of calls, or at the very end.

import { debounce } from 'shiki-decorators';
// or to import with tree shaking use
// import debounce from 'shiki-decorators/src/debounce';

class Example {
  @debounce(1000)
  foo(num) {
    console.log('num:', num);
  }
}

const example = new Example();

example.foo(1); // Won't execute callback
example.foo(2); // Won’t execute callback
example.foo(3); // Will execute the callback

setTimeout(() => {
  example.foo(10); // Will execute the callback
}, 1200);

// Output
// num: 3
// num: 10

Package release command

GITHUB_TOKEN=... npm run release
1.0.1

5 years ago

1.0.0

5 years ago

0.15.29

5 years ago

0.15.31

5 years ago

0.15.32

5 years ago

0.15.30

5 years ago

0.15.28

5 years ago

0.15.26

5 years ago

0.15.27

5 years ago

0.15.25

5 years ago

0.15.24

5 years ago

0.15.23

5 years ago

0.15.22

5 years ago

0.15.21

5 years ago

0.15.20

5 years ago

0.15.19

5 years ago

0.15.18

5 years ago

0.15.17

5 years ago

0.15.16

5 years ago

0.15.14

5 years ago

0.15.15

5 years ago

0.15.13

5 years ago

0.15.12

5 years ago

0.15.8

5 years ago

0.15.9

5 years ago

0.15.10

5 years ago

0.15.6

5 years ago

0.15.7

5 years ago

0.15.5

5 years ago

0.15.4

5 years ago

0.15.1

5 years ago

0.15.2

5 years ago

0.15.3

5 years ago

0.15.0

5 years ago

0.14.7

5 years ago

0.14.5

5 years ago

0.14.6

5 years ago

0.14.4

5 years ago

0.14.3

5 years ago

0.14.2

5 years ago

0.14.1

5 years ago

0.14.0

5 years ago

0.13.2

5 years ago

0.13.3

5 years ago

0.13.4

5 years ago

0.13.1

5 years ago

0.13.0

5 years ago

0.12.8

5 years ago

0.12.7

5 years ago

0.12.6

5 years ago

0.12.4

5 years ago

0.12.5

5 years ago

0.12.3

5 years ago

0.12.2

5 years ago

0.12.1

5 years ago

0.12.0

5 years ago

0.11.13

5 years ago

0.11.12

5 years ago

0.11.11

5 years ago

0.11.10

5 years ago

0.11.9

5 years ago

0.11.8

5 years ago

0.11.7

5 years ago

0.11.5

5 years ago

0.11.6

5 years ago

0.11.4

5 years ago

0.11.3

5 years ago

0.10.9

5 years ago

0.11.0

5 years ago

0.11.1

5 years ago

0.11.2

5 years ago

0.10.7

5 years ago

0.10.8

5 years ago

0.10.6

5 years ago

0.10.5

5 years ago

0.10.4

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.9.9

5 years ago

0.10.0

5 years ago

0.9.8

5 years ago

0.9.6

5 years ago

0.9.5

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.25

5 years ago

0.8.27

5 years ago

0.8.26

5 years ago

0.8.23

5 years ago

0.8.24

5 years ago

0.8.22

5 years ago

0.8.21

5 years ago

0.8.20

5 years ago

0.8.19

5 years ago

0.8.18

5 years ago