Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

decorator-utils

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decorator-utils - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

20

compiled/index.js

@@ -19,18 +19,18 @@ "use strict";

_createClass(DecoratorUtils, null, [{
key: "getDeclarationType",
value: function getDeclarationType(args) {
var _Array$slice = Array.slice(args);
key: "getType",
value: function getType(args) {
var _Array$prototype$slice$call = Array.prototype.slice.call(args);
var _Array$slice2 = _slicedToArray(_Array$slice, 3);
var _Array$prototype$slice$call2 = _slicedToArray(_Array$prototype$slice$call, 3);
var target = _Array$slice2[0];
var name = _Array$slice2[1];
var descriptor = _Array$slice2[2];
var target = _Array$prototype$slice$call2[0];
var name = _Array$prototype$slice$call2[1];
var descriptor = _Array$prototype$slice$call2[2];
if (args.length === 1 && typeof target === "function") {
return DecoratorUtils.declarationType.CLASS;
return DecoratorUtils.type.CLASS;
} else if (args.length === 3 && typeof target === "object" && typeof target.constructor === "function") {
var isObjectLiteral = target.constructor.name === "Object";
var isAccessor = descriptor.get || descriptor.set;
return DecoratorUtils.declarationType[(isObjectLiteral ? "OBJECT_LITERAL" : "CLASS") + "_" + (isAccessor ? "ACCESSOR" : "METHOD")];
return DecoratorUtils.type[(isObjectLiteral ? "OBJECT_LITERAL" : "CLASS") + "_" + (isAccessor ? "ACCESSOR" : "METHOD")];
}

@@ -41,3 +41,3 @@

}, {
key: "declarationType",
key: "type",
value: ["CLASS", "CLASS_METHOD", "CLASS_ACCESSOR", "OBJECT_LITERAL_METHOD", "OBJECT_LITERAL_ACCESSOR"].reduce(function (obj, name) {

@@ -44,0 +44,0 @@ return Object.defineProperty(obj, name, { value: Symbol(name) });

"use strict";
var _ = require("./");
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; }; })();
function _createDecoratedObject(descriptors) { var target = {}; 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 = true; 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) { descriptor.value = descriptor.initializer.call(target); } Object.defineProperty(target, key, descriptor); } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _ = require("./");
describe("DecoratorUtils", function () {
describe("getType()", function () {
it("should correctly detect class declarations", function () {
function decorator() {
(_.DecoratorUtils.getType(arguments) === _.DecoratorUtils.type.CLASS).should.be["true"]();
}
var Thing = (function () {
function Thing() {
_classCallCheck(this, _Thing);
}
var _Thing = Thing;
Thing = decorator(Thing) || Thing;
return Thing;
})();
});
it("should correctly detect class methods", function () {
function decorator() {
(_.DecoratorUtils.getType(arguments) === _.DecoratorUtils.type.CLASS_METHOD).should.be["true"]();
}
var Thing = (function () {
function Thing() {
_classCallCheck(this, Thing);
}
_createDecoratedClass(Thing, [{
key: "hello",
decorators: [decorator],
value: function hello() {}
}]);
return Thing;
})();
});
it("should correctly detect class accessors", function () {
function decorator() {
(_.DecoratorUtils.getType(arguments) === _.DecoratorUtils.type.CLASS_ACCESSOR).should.be["true"]();
}
var Thing = (function () {
function Thing() {
_classCallCheck(this, Thing);
}
_createDecoratedClass(Thing, [{
key: "hello",
decorators: [decorator, decorator],
get: function get() {},
set: function set(value) {}
}]);
return Thing;
})();
});
it("should correctly detect object literal methods", function () {
function decorator() {
(_.DecoratorUtils.getType(arguments) === _.DecoratorUtils.type.OBJECT_LITERAL_METHOD).should.be["true"]();
}
var thing = _createDecoratedObject([{
key: "hello",
decorators: [decorator],
value: function hello() {}
}]);
});
it("should correctly detect object literal accessors", function () {
function decorator() {
(_.DecoratorUtils.getType(arguments) === _.DecoratorUtils.type.OBJECT_LITERAL_ACCESSOR).should.be["true"]();
}
var thing = _createDecoratedObject([{
key: "hello",
decorators: [decorator, decorator],
get: function get() {},
set: function set(value) {}
}]);
});
});
});
{
"name": "decorator-utils",
"version": "0.0.1",
"version": "0.0.2",
"description": "Utilities for ES7 decorators.",

@@ -5,0 +5,0 @@ "author": "Luke Horvat",

@@ -15,2 +15,39 @@ # decorator-utils [![NPM version](http://img.shields.io/npm/v/decorator-utils.svg?style=flat-square)](https://www.npmjs.org/package/decorator-utils) [![Build status](http://img.shields.io/travis/lukehorvat/decorator-utils.svg?style=flat-square)](https://travis-ci.org/lukehorvat/decorator-utils)

TODO.
A simple example:
```javascript
import {DecoratorUtils} from "decorator-utils";
function decorator() {
if (DecoratorUtils.getType(arguments) !== DecoratorUtils.type.CLASS_METHOD) {
throw new Error("Decorator target must be a class method declaration.");
}
}
@decorator // Error will be thrown.
class Dog {
@decorator // Error will NOT be thrown.
woof() {}
}
```
## API
The package exposes a static class, `DecoratorUtils`, which has the following functions and properties:
### getType(arguments)
- A function that can be called from within a decorator to determine the type of declaration that is being targeted. Useful for guarding a decorator against certain declaration types.
- Parameters:
- **arguments** - The `arguments` object the decorator function was called with. Just pass it through!
- Returns one of `DecoratorUtils.type`'s values.
### type
- A property describing the set of possible declaration types that a decorator can target, as key-value pairs of an object (`String` -> [`Symbol`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Symbol)). To be used in conjunction with `DecoratorUtils.getType()`.
- Keys:
- `CLASS`
- `CLASS_METHOD`
- `CLASS_ACCESSOR`
- `OBJECT_LITERAL_METHOD`
- `OBJECT_LITERAL_ACCESSOR`
export class DecoratorUtils {
static declarationType = [
static type = [
"CLASS",

@@ -12,11 +12,11 @@ "CLASS_METHOD",

static getDeclarationType(args) {
let [target, name, descriptor] = Array.slice(args);
static getType(args) {
let [target, name, descriptor] = Array.prototype.slice.call(args);
if (args.length === 1 && typeof target === "function") {
return DecoratorUtils.declarationType.CLASS;
return DecoratorUtils.type.CLASS;
} else if (args.length === 3 && typeof target === "object" && typeof target.constructor === "function") {
let isObjectLiteral = target.constructor.name === "Object";
let isAccessor = descriptor.get || descriptor.set;
return DecoratorUtils.declarationType[`${isObjectLiteral ? "OBJECT_LITERAL" : "CLASS"}_${isAccessor ? "ACCESSOR" : "METHOD"}`];
return DecoratorUtils.type[`${isObjectLiteral ? "OBJECT_LITERAL" : "CLASS"}_${isAccessor ? "ACCESSOR" : "METHOD"}`];
}

@@ -23,0 +23,0 @@

import {DecoratorUtils} from "./";
describe("DecoratorUtils", () => {
describe("getType()", () => {
it("should correctly detect class declarations", () => {
function decorator() {
(DecoratorUtils.getType(arguments) === DecoratorUtils.type.CLASS).should.be.true();
}
@decorator
class Thing {}
});
it("should correctly detect class methods", () => {
function decorator() {
(DecoratorUtils.getType(arguments) === DecoratorUtils.type.CLASS_METHOD).should.be.true();
}
class Thing {
@decorator
hello() {}
}
});
it("should correctly detect class accessors", () => {
function decorator() {
(DecoratorUtils.getType(arguments) === DecoratorUtils.type.CLASS_ACCESSOR).should.be.true();
}
class Thing {
@decorator
get hello() {}
@decorator
set hello(value) {}
}
});
it("should correctly detect object literal methods", () => {
function decorator() {
(DecoratorUtils.getType(arguments) === DecoratorUtils.type.OBJECT_LITERAL_METHOD).should.be.true();
}
let thing = {
@decorator
hello() {}
};
});
it("should correctly detect object literal accessors", () => {
function decorator() {
(DecoratorUtils.getType(arguments) === DecoratorUtils.type.OBJECT_LITERAL_ACCESSOR).should.be.true();
}
let thing = {
@decorator
get hello() {},
@decorator
set hello(value) {}
};
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc