Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

core-decorators

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

core-decorators - npm Package Compare versions

Comparing version
0.9.2
to
0.10.0
+51
lib/lazy-initialize.js
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports['default'] = lazyInitialize;
var _privateUtils = require('./private/utils');
function handleDescriptor(target, key, descriptor) {
var configurable = descriptor.configurable;
var enumerable = descriptor.enumerable;
var initializer = descriptor.initializer;
var value = descriptor.value;
return {
configurable: configurable,
enumerable: enumerable,
get: function get() {
// This happens if someone accesses the
// property directly on the prototype
if (this === target) {
return;
}
var ret = initializer ? initializer.call(this) : value;
Object.defineProperty(this, key, {
configurable: configurable,
enumerable: enumerable,
writable: true,
value: ret
});
return ret;
},
set: (0, _privateUtils.createDefaultSetter)(key)
};
}
function lazyInitialize() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (0, _privateUtils.decorate)(handleDescriptor, args);
}
module.exports = exports['default'];
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports['default'] = time;
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }
var _privateUtils = require('./private/utils');
var CONSOLE_NATIVE = console;
var labels = {};
var CONSOLE_TIME = console.time ? console.time.bind(console) : function (label) {
labels[label] = new Date();
};
var CONSOLE_TIMEEND = console.timeEnd ? console.timeEnd.bind(console) : function (label) {
var timeNow = new Date();
var timeTaken = timeNow - labels[label];
console.log('' + label + ': ' + timeTaken + 'ms');
};
var count = 0;
function handleDescriptor(target, key, descriptor, _ref) {
var _ref2 = _slicedToArray(_ref, 2);
var _ref2$0 = _ref2[0];
var prefix = _ref2$0 === undefined ? null : _ref2$0;
var _ref2$1 = _ref2[1];
var konsole = _ref2$1 === undefined ? null : _ref2$1;
var fn = descriptor.value;
var CONSOLE = konsole || CONSOLE_NATIVE;
if (prefix === null) {
prefix = '' + target.constructor.name + '.' + key;
}
if (typeof fn !== 'function') {
throw new SyntaxError('@time can only be used on functions, not: ' + fn);
}
return _extends({}, descriptor, {
value: function value() {
var time = CONSOLE.time || CONSOLE_TIME;
var timeEnd = CONSOLE.timeEnd || CONSOLE_TIMEEND;
var label = '' + prefix + '-' + count;
count++;
time(label);
try {
return fn.apply(this, arguments);
} finally {
timeEnd(label);
}
}
});
}
function time() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (0, _privateUtils.decorate)(handleDescriptor, args);
}
module.exports = exports['default'];
import { decorate, createDefaultSetter } from './private/utils';
function handleDescriptor(target, key, descriptor) {
const { configurable, enumerable, initializer, value } = descriptor;
return {
configurable,
enumerable,
get() {
// This happens if someone accesses the
// property directly on the prototype
if (this === target) {
return;
}
const ret = initializer ? initializer.call(this) : value;
Object.defineProperty(this, key, {
configurable,
enumerable,
writable: true,
value: ret
});
return ret;
},
set: createDefaultSetter(key)
};
}
export default function lazyInitialize(...args) {
return decorate(handleDescriptor, args);
}
import { decorate } from './private/utils';
const CONSOLE_NATIVE = console;
const labels = {};
const CONSOLE_TIME = console.time ? console.time.bind(console) : label => {
labels[label] = new Date();
};
const CONSOLE_TIMEEND = console.timeEnd ? console.timeEnd.bind(console) : label => {
const timeNow = new Date();
const timeTaken = timeNow - labels[label];
console.log(`${label}: ${timeTaken}ms`);
};
let count = 0;
function handleDescriptor(target, key, descriptor, [prefix = null, konsole = null]) {
const fn = descriptor.value;
const CONSOLE = konsole || CONSOLE_NATIVE;
if (prefix === null) {
prefix = `${target.constructor.name}.${key}`;
}
if (typeof fn !== 'function') {
throw new SyntaxError(`@time can only be used on functions, not: ${fn}`);
}
return {
...descriptor,
value() {
const time = CONSOLE.time || CONSOLE_TIME;
const timeEnd = CONSOLE.timeEnd || CONSOLE_TIMEEND;
const label = `${prefix}-${count}`;
count++;
time(label);
try {
return fn.apply(this, arguments);
} finally {
timeEnd(label);
}
}
}
}
export default function time(...args) {
return decorate(handleDescriptor, args);
}
+9
-1

@@ -61,2 +61,10 @@ 'use strict';

exports.mixin = _interopRequire(_mixin);
exports.mixins = _interopRequire(_mixin);
exports.mixins = _interopRequire(_mixin);
var _lazyInitialize = require('./lazy-initialize');
exports.lazyInitialize = _interopRequire(_lazyInitialize);
var _time = require('./time');
exports.time = _interopRequire(_time);

@@ -6,2 +6,5 @@ 'use strict';

});
var _createDecoratedClass = (function () { function defineProperties(target, descriptors, initializers) { for (var i = 0; i < descriptors.length; i++) { var descriptor = descriptors[i]; var decorators = descriptor.decorators; var key = descriptor.key; delete descriptor.key; delete descriptor.decorators; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor || descriptor.initializer) descriptor.writable = true; if (decorators) { for (var f = 0; f < decorators.length; f++) { var decorator = decorators[f]; if (typeof decorator === 'function') { descriptor = decorator(target, key, descriptor) || descriptor; } else { throw new TypeError('The decorator for method ' + descriptor.key + ' is of the invalid type ' + typeof decorator); } } if (descriptor.initializer !== undefined) { initializers[key] = descriptor; continue; } } Object.defineProperty(target, key, descriptor); } } return function (Constructor, protoProps, staticProps, protoInitializers, staticInitializers) { if (protoProps) defineProperties(Constructor.prototype, protoProps, protoInitializers); if (staticProps) defineProperties(Constructor, staticProps, staticInitializers); return Constructor; }; })();
var _slice = Array.prototype.slice;

@@ -14,6 +17,14 @@ exports.isDescriptor = isDescriptor;

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _defineDecoratedPropertyDescriptor(target, key, descriptors) { var _descriptor = descriptors[key]; if (!_descriptor) return; var descriptor = {}; for (var _key in _descriptor) descriptor[_key] = _descriptor[_key]; descriptor.value = descriptor.initializer.call(target); Object.defineProperty(target, key, descriptor); }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
var _lazyInitialize = require('../lazy-initialize');
var _lazyInitialize2 = _interopRequireDefault(_lazyInitialize);
var defineProperty = Object.defineProperty;

@@ -50,10 +61,41 @@ var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

var Meta = function Meta() {
_classCallCheck(this, Meta);
var Meta = (function () {
var _instanceInitializers = {};
this.debounceTimeoutIds = {};
this.throttleTimeoutIds = {};
this.throttlePreviousTimestamps = {};
};
function Meta() {
_classCallCheck(this, Meta);
_defineDecoratedPropertyDescriptor(this, 'debounceTimeoutIds', _instanceInitializers);
_defineDecoratedPropertyDescriptor(this, 'throttleTimeoutIds', _instanceInitializers);
_defineDecoratedPropertyDescriptor(this, 'throttlePreviousTimestamps', _instanceInitializers);
}
_createDecoratedClass(Meta, [{
key: 'debounceTimeoutIds',
decorators: [_lazyInitialize2['default']],
initializer: function () {
return {};
},
enumerable: true
}, {
key: 'throttleTimeoutIds',
decorators: [_lazyInitialize2['default']],
initializer: function () {
return {};
},
enumerable: true
}, {
key: 'throttlePreviousTimestamps',
decorators: [_lazyInitialize2['default']],
initializer: function () {
return {};
},
enumerable: true
}], null, _instanceInitializers);
return Meta;
})();
var META_KEY = typeof Symbol === 'function' ? Symbol('__core_decorators__') : '__core_decorators__';

@@ -60,0 +102,0 @@

+1
-1

@@ -23,3 +23,3 @@ 'use strict';

function handleDescriptor(target, key, descriptor, warningTypes) {
function handleDescriptor(target, key, descriptor) {
return _extends({}, descriptor, {

@@ -26,0 +26,0 @@ value: function suppressWarningsWrapper() {

{
"name": "core-decorators",
"version": "0.9.2",
"version": "0.10.0",
"description": "Library of ES2016 (ES7) JavaScript decorators inspired by languages that come with built-ins like @​override, @​deprecate, @​autobind, @​mixin and more! Works great with React/Angular/more!",

@@ -5,0 +5,0 @@ "main": "lib/core-decorators.js",

+73
-10

@@ -20,13 +20,19 @@ # core-decorators.js [![Build Status](https://travis-ci.org/jayphelps/core-decorators.js.svg?branch=master)](https://travis-ci.org/jayphelps/core-decorators.js)

##### For Properties and Methods
* [@readonly](#readonly)
* [@nonconfigurable](#nonconfigurable)
* [@decorate](#decorate) :new:
##### For Properties only
* [@nonenumerable](#nonenumerable)
* [@lazyInitialize](#lazyInitialize) :new:
##### For Methods only
* [@autobind](#autobind)
* [@readonly](#readonly)
* [@deprecate](#deprecate-alias-deprecated)
* [@suppressWarnings](#suppresswarnings)
* [@enumerable](#enumerable) :new:
* [@override](#override)
* [@deprecate](#deprecate-alias-deprecated)
* [@debounce](#debounce)
* [@throttle](#throttle) :new:
* [@suppressWarnings](#suppresswarnings)
* [@enumerable](#enumerable) :new:
* [@nonenumerable](#nonenumerable)
* [@nonconfigurable](#nonconfigurable)
* [@decorate](#decorate) :new:
* [@instrument](#instrument) :new:

@@ -38,3 +44,2 @@ ##### For Classes

##### Proposed (not implemented, PRs welcome!):
* @instrument/profile
* @assertArguments(arg1 => arg1, arg2 => arg2)

@@ -226,3 +231,3 @@ * @private

pay() {}
@enumerable

@@ -267,3 +272,3 @@ eat() {}

Marks a property or method as not being writable.
Marks a property or method so that it cannot be reconfigured, changed, or deleted.

@@ -316,2 +321,30 @@ ```js

### @lazyInitialize
Prevents a property initializer from running until the decorated property is actually looked up. Useful to prevent excess allocations that might otherwise not be used, but be careful not to over-optimize things.
```js
import { lazyInitialize } from 'core-decorators';
function createHugeBuffer() {
console.log('huge buffer created');
return new Array(1000000);
}
class Editor {
@lazyInitialize
hugeBuffer = createHugeBuffer();
}
var editor = new Editor();
// createHugeBuffer() has not been called yet
editor.hugeBuffer;
// logs 'huge buffer created', now it has been called
editor.hugeBuffer;
// already initialized and equals our buffer, so
// createHugeBuffer() is not called again
```
### @mixin (alias: @mixins)

@@ -350,3 +383,33 @@

### @instrument
Uses `console.time` and `console.timeEnd` to provide function timings with a unique label whose default prefix is `ClassName.method`. Supply a first argument to override the prefix:
```js
class Bird {
@instrument('sing')
sing() {
}
}
var bird = new Bird();
bird.sing(); // console.time label will be 'sing-0'
bird.sing(); // console.time label will be 'sing-1'
```
Will polyfill `console.time` if the current environment does not support it. You can also supply a custom `console` object as the second argument with the following methods:
* `myConsole.time(label)`
* `myConsole.timeEnd(label)`
* `myConsole.log(value)`
```js
let myConsole = {
time: function(label) { /* custom time() method */ },
timeEnd: function(label) { /* custom timeEnd method */ },
log: function(str) { /* custom log method */ }
}
```
# Future Compatibility
Since most people can't keep up to date with specs, it's important to note that ES2016 (including the decorators spec this relies on) is in-flux and subject to breaking changes. In fact, the [biggest change is coming shortly](https://github.com/wycats/javascript-decorators/pull/36) but I am active in the appropriate communities and will be keeping this project up to date as things progress. For the most part, these changes will usually be transparent to consumers of this project--that said, core-decorators has not yet reached 1.0 and may in fact introduce breaking changes. If you'd prefer not to receive these changes, be sure to lock your dependency to [PATCH](http://semver.org/). You can track the progress of core-decorators@1.0.0 in the [The Road to 1.0](https://github.com/jayphelps/core-decorators.js/issues/15) ticket.
{
"optional": [
"es7.objectRestSpread",
"es7.classProperties"
"es7.classProperties",
"es7.decorators"
]
}

@@ -14,1 +14,3 @@ export { default as override } from './override';

export { default as mixin, default as mixins } from './mixin';
export { default as lazyInitialize } from './lazy-initialize';
export { default as time } from './time';

@@ -0,1 +1,3 @@

import lazyInitialize from '../lazy-initialize';
const { defineProperty, getOwnPropertyDescriptor,

@@ -31,4 +33,7 @@ getOwnPropertyNames, getOwnPropertySymbols } = Object;

class Meta {
@lazyInitialize
debounceTimeoutIds = {};
@lazyInitialize
throttleTimeoutIds = {};
@lazyInitialize
throttlePreviousTimestamps = {};

@@ -35,0 +40,0 @@ }

@@ -15,3 +15,3 @@ import { decorate } from './private/utils';

function handleDescriptor(target, key, descriptor, warningTypes) {
function handleDescriptor(target, key, descriptor) {
return {

@@ -18,0 +18,0 @@ ...descriptor,