1.0.1 • Published 3 years ago

shiki-decorators v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
3 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

3 years ago

1.0.0

3 years ago

0.15.29

3 years ago

0.15.31

3 years ago

0.15.32

3 years ago

0.15.30

3 years ago

0.15.28

3 years ago

0.15.26

3 years ago

0.15.27

3 years ago

0.15.25

4 years ago

0.15.24

4 years ago

0.15.23

4 years ago

0.15.22

4 years ago

0.15.21

4 years ago

0.15.20

4 years ago

0.15.19

4 years ago

0.15.18

4 years ago

0.15.17

4 years ago

0.15.16

4 years ago

0.15.14

4 years ago

0.15.15

4 years ago

0.15.13

4 years ago

0.15.12

4 years ago

0.15.8

4 years ago

0.15.9

4 years ago

0.15.10

4 years ago

0.15.6

4 years ago

0.15.7

4 years ago

0.15.5

4 years ago

0.15.4

4 years ago

0.15.1

4 years ago

0.15.2

4 years ago

0.15.3

4 years ago

0.15.0

4 years ago

0.14.7

4 years ago

0.14.5

4 years ago

0.14.6

4 years ago

0.14.4

4 years ago

0.14.3

4 years ago

0.14.2

4 years ago

0.14.1

4 years ago

0.14.0

4 years ago

0.13.2

4 years ago

0.13.3

4 years ago

0.13.4

4 years ago

0.13.1

4 years ago

0.13.0

4 years ago

0.12.8

4 years ago

0.12.7

4 years ago

0.12.6

4 years ago

0.12.4

4 years ago

0.12.5

4 years ago

0.12.3

4 years ago

0.12.2

4 years ago

0.12.1

4 years ago

0.12.0

4 years ago

0.11.13

4 years ago

0.11.12

4 years ago

0.11.11

4 years ago

0.11.10

4 years ago

0.11.9

4 years ago

0.11.8

4 years ago

0.11.7

4 years ago

0.11.5

4 years ago

0.11.6

4 years ago

0.11.4

4 years ago

0.11.3

4 years ago

0.10.9

4 years ago

0.11.0

4 years ago

0.11.1

4 years ago

0.11.2

4 years ago

0.10.7

4 years ago

0.10.8

4 years ago

0.10.6

4 years ago

0.10.5

4 years ago

0.10.4

4 years ago

0.10.3

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.9.9

4 years ago

0.10.0

4 years ago

0.9.8

4 years ago

0.9.6

4 years ago

0.9.5

4 years ago

0.9.4

4 years ago

0.9.3

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.25

4 years ago

0.8.27

4 years ago

0.8.26

4 years ago

0.8.23

4 years ago

0.8.24

4 years ago

0.8.22

4 years ago

0.8.21

4 years ago

0.8.20

4 years ago

0.8.19

4 years ago

0.8.18

4 years ago