Core-wrappers
Core-wrappers is a small library exporting basic wrapper functions compatible with ES7 Decorators.
Use it in nodeJS
A version compiled to ES5 in CJS format is published to npm as core-wrappers.
npm install core-wrappers
Use it on browser
core-wrappers CDN
You can use it with any AMD loader or standalone
{ console;} test = CoreWrappers; ;; //WARN: This function should not be called more than 1 times.
The wrapper and the decorator
A wrapper is a function that returns another function.
Wrapper:
{ return { //do sth... return fn; }} { var fn = argsargslength - 1; return { //do sth... return fn; }}
A decorator is a function like ES7 Decorators
{ //do sth... return descriptor;} { return { //do sth... return descriptor; }}
This library can transform a wrapper to a decorator.
Wrapper in ES5:
var w = ; { } Fooprototypebar = w;
Decorator in ES7:
const w = ;const debounce = w; @ { //do sth. }
API Doc
For both Wrappers and Decorates
- @allow
- @bind
- @debounce
- @defer
- @deprecate
- @enumerable
- @methodize
- @multicast
- @observable
- @once
- @promisify
- @readonly
- @repeat
- @spread
- @supressWarnings
- @throttle
For Decorates only
@allow
allow(times, fn)
Creates a version of the function that can only be called servaral times. Repeated calls exteeded by max times will have no effect, returning the value of undefined. Useful for initialization functions, instead of having to set a boolean flag and then check it later.
Wrapper:
var allow = allow; var initialize = ;;;// Application is only created once.
Decorator:
const w = ;const allow = w; let times = 0; { ; } @ { return times++; } let f = f2 = ;to;
@bind
bind(...args, fn)
Forces invocations of this function to always have this
refer to the class instance, even if the function is passed around or would otherwise lose its this
context. e.g. var fn = context.method;
Popular with React components.
Wrapper:
var bind = bind; { thisname = name;}Personprototype{ return thisname;} var akira = 'akira';akiragetName = ;var getName = akiragetName;console; //akira
Decorator:
const w = ; const bind = w; { thisname =name; } @bind { return thisname; } const akira = 'akira';akiragetName = ;let getName = akiragetName;console; //akira
@debounce
debounce(wait, immediate, fn)
Creates and returns a new debounced version of the passed function which will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked. 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.
At the end of the wait interval, the function will be called with the arguments that were passed most recently to the debounced function.
Pass true for the immediate argument to cause debounce to trigger the function on the leading instead of the trailing edge of the wait interval. Useful in circumstances like preventing accidental double-clicks on a "submit" button from firing a second time.
Wrapper:
var w = ; var i = 0;{ i++;}var inc1 = w;;;;to;;; var inc2 = w;;;;console; //1
Decorator:
const w = ;const debounce = w; { thisi = i; } @ { thisi++; } let foo = 0;foo;foo;foo;console; //1 ;
@decorator
decorator(wrapper, ...args)
The default decorator. Immediately applies the provided wrapper and arguments to the method.
const w = ;const decorator = w; @ { return 1; } let f = ;to;to;to;
@defer
defer(promisify, fn)
Defers invoking the func until the current call stack has cleared. Any additional arguments are provided to func when it’s invoked.
If promisify is true, it returns a promise otherwise it returns the timer id.
Wrapper:
var w = ; var i = 0;{ return i++;}inc = wdeferinc;;to;process;
Decorator:
const w = ;const defer = w; { thisi = i; } @ { return thisi++; } let foo = 0;foo;
@delay
delay(wait, promisify, fn)
Invokes func after wait milliseconds.
If promisify is true, it returns a promise otherwise it returns the timer id.
@deprecate
deprecate(message, url, fn)
Calls console.warn() with a deprecation message. Provide a custom message to override the default one. You can also provide a url, for further reading.
const w = ; const deprecate = w; const suppressWarnings = w; @deprecate { } @ { } @suppressWarnings { this; }var foo = ;foo;foo;foo;
@enumerable
enumerable(target, key, isEnumerable, fn)
Marks a property as being enumerable. Note that class methods are always nonenumerable, so this is only useful for instance properties.
Wrapper:
var w = ; var obj = {}; w; to; to;
Decorator:
const w = ; const enumerable = w; //Note: class methods are nonenumerable //so this is only useful for instance properties. @ bar = 1 var foo = ;to;to;
@methodize
methodize([...keys,] fn)
Invoke this wrapper without keys will add this
context to the first argument of the function.
const w = ; const methodize = w; x = 1; @methodize { return selfx + y; } var foo = ;to;
Invoke this wrapper with keys will add this[key1], thie[key2]...
to the arguments of the function.
const w = ; const methodize = w; x = 1; y = 2; @ { return x + y + z; } var foo = ;to;
@multicast
multicast(fn)
Allow the first argument of the function be an array and invokes function with the elements of the array one by one.
Wrapper:
var w = ; { return elstylecolor = color;} var setColorMany = w; var els = document;console;;
Decorator:
const w = ;let multicast = w;let spread = w; { thisitems = ; } @spread @multicast { thisitems; return thisitems0; } var c = ;c;to; //2
@multiset
multiset(fn)
Allow setter's first argument to be a json object to set many properties at once.
const w = ;let multiset = w; { thismap = {}; } @multiset { thismapkey = value; } let store = ;store;store; to;to;to;
@readonly
readonly(obj, prop, fn)
Marks a property or method readonly so that it cannot be reset or deleted.
Wrapper:
var obj = {}; w; to; to; to;
Decorator:
const w = ; const readonly = w; @readonly { return 1; } var foo = ;to;to;delete foobar;to;
@observable
observable(fn)
Let function to be observable so we can trigger before
and after
events when the function is invoked.
var w = ; { return x + y;}to; add = w; add{ args1 += 4;} to;
@once
once(fn)
This wrapper is a shortcut of allow(1, fn).
@promisify
promisify(fn)
Transform an asynchronous function with a callback(err, ...args) parameter to a function returns a promise.
Wrapper:
var w = ; var fs = ; var readFile = w; ;
Decorator:
const w = ;let promisify = w; { thisurl = url; } @promisify { let request = ; ; } var site = 'https://registry.npmjs.org/core-wrappers';site;
@repeat
repeat(times, wait, fn)
Call function many times.
@spread
spread(fn)
See multicast
@suppressWarnings
suppressWarnings(fn)
See deprecate
@throttle
throttle(wait, options, fn)
Creates and returns a new, throttled version of the passed function, that, when invoked repeatedly, will only actually call the original function at most once per every wait milliseconds. Useful for rate-limiting events that occur faster than you can keep up with.
By default, throttle will execute the function as soon as you call it for the first time, and, if you call it again any number of times during the wait period, as soon as that period is over. If you'd like to disable the leading-edge call, pass {leading: false}, and if you'd like to disable the execution on the trailing-edge, pass {trailing: false}.
var w = ; var i = 0;{ i++;}inc = w;;to;;
getDecorator
getDecorator(nameOrWrapper)
Transform a wrapper function to a decorator function. If you pass a string as parameter, it will find a build-in wrapper to transform.
A wrapper is a function that returns other function. A decorator is a function like ES7 Decorators.