UNPKG

javascript-decorators

Version:
80 lines (68 loc) 1.94 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports._timesCalled = exports._once = exports._times = undefined; var _helpers = require('./helpers'); /** * Executes a function n times, any repeat call returns * the value of the nth call * * @method __times * * * @return { function } decorator function */ var __times = function __times() { var times = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; var timescalled = 0; var res = void 0; return function (target, key, descriptor) { var func = descriptor.value; (0, _helpers.descriptorIsFunc)(key, func); descriptor.value = function () { if (timescalled !== times) { timescalled++; for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } res = func.apply(this, args); } return res; }; return descriptor; }; }; /** * Attaches a property on the function indicating how many times * has been called * * @method __times * * * @return { function } decorator function */ /** * Execution related decorators * * @author Avraam Mavridis <avr.mav@gmail.com> * */ var __timesCalled = function __timesCalled() { return function (target, key, descriptor) { var func = descriptor.value; (0, _helpers.descriptorIsFunc)(key, func); descriptor.value = function () { descriptor.value.timesCalled = descriptor.value.timesCalled || 0; descriptor.value.timesCalled++; for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } return func.apply(this, args); }; return descriptor; }; }; var _times = exports._times = __times; var _once = exports._once = __times.bind({}, 1); var _timesCalled = exports._timesCalled = __timesCalled;