Socket
Socket
Sign inDemoInstall

cla6

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cla6 - npm Package Compare versions

Comparing version 1.1.5 to 1.2.1

test/mixins.js

2

bower.json
{
"name": "cla6",
"version": "1.1.5",
"version": "1.2.1",
"main": "client/cla6.js",

@@ -5,0 +5,0 @@ "repository": {

@@ -47,2 +47,26 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Cla6 = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

var classExtensions = {
mixin: function(props) {
if (arguments.length > 1) {
Array.prototype.forEach.call(arguments, function(props) {
this.mixin(props);
}, this);
} else {
if (props == null)
throwErr('properties must be provided');
if (typeof props != 'object')
throwErr('properties must be defined using an object');
descriptors = _.toDescriptors(props);
applyPlugins(descriptors);
Object.defineProperties(this.prototype, descriptors);
}
return this;
}
};
var createClass = function(name, props, Parent) {

@@ -59,25 +83,14 @@ props = _.clone(props);

var fixedProps = getFixedProps(props);
applyPlugins(fixedProps);
var descriptors = _.toDescriptors(props);
applyPlugins(descriptors);
var Child = _.nameFn(fixedProps.constructor.value, name);
fixedProps.constructor.value = Child;
var Child = _.nameFn(descriptors.constructor.value, name);
_.extend(Child, classExtensions);
Child.prototype = Object.create(Parent.prototype, fixedProps);
descriptors.constructor.value = Child;
Child.prototype = Object.create(Parent.prototype, descriptors);
return Child;
};
var getFixedProps = function(props) {
return Object.keys(props).reduce(function(result, k) {
var descriptor = Object.getOwnPropertyDescriptor(props, k);
delete descriptor.enumerable;
if (descriptor.value == null)
delete descriptor.writable;
result[k] = descriptor;
return result;
}, {});
};
var addPlugin = function(plugin) {

@@ -87,8 +100,12 @@ plugins.push(plugin);

var applyPlugins = function(props) {
var applyPlugins = function(descriptors) {
plugins.forEach(function(plugin) {
plugin(props);
plugin(descriptors);
});
};
var throwErr = function(msg) {
throw Error('Cla6 Class error - ' + msg);
};
module.exports = {

@@ -140,2 +157,8 @@ create: createClass,

var extend = function(obj, extension) {
Object.keys(extension).forEach(function(k) {
obj[k] = extension[k];
});
};
var nameFn = function(fn, name) {

@@ -145,7 +168,22 @@ return eval('(function ' + name + '() {return fn.apply(this, arguments);})');

var toDescriptors = function(props) {
return Object.keys(props).reduce(function(result, k) {
var descriptor = Object.getOwnPropertyDescriptor(props, k);
delete descriptor.enumerable;
if (descriptor.value == null)
delete descriptor.writable;
result[k] = descriptor;
return result;
}, {});
};
module.exports = {
clone: clone,
nameFn: nameFn
extend: extend,
nameFn: nameFn,
toDescriptors: toDescriptors
};
},{}]},{},[1])(1)
});

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

(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Cla6=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var ClassFactory=require("./classFactory");var Extender=require("./extender");function Cla6(name,props){if(name==null)throwErr("a name must be provided");if(typeof name!="string")throwErr("name must be a string");if(props!=null){if(typeof props!="object")throwErr("properties must be defined using an object");if(props.hasOwnProperty("constructor")&&typeof props.constructor!="function")throwErr("constructor must be a function")}if(props==null)return new Extender(name);else return ClassFactory.create(name,props)}Cla6.use=function(plugin){if(plugin==null)throwErr("a plugin must be provided");if(typeof plugin!="function")throwErr("plugin must be a function");ClassFactory.use(plugin)};var throwErr=function(msg){throw Error("Cla6 error - "+msg)};module.exports=Cla6},{"./classFactory":2,"./extender":3}],2:[function(require,module,exports){var _=require("./utils");var plugins=[];var createClass=function(name,props,Parent){props=_.clone(props);if(typeof Parent!="function")Parent=Object;if(!props.hasOwnProperty("constructor"))props.constructor=function(){Parent.apply(this,arguments)};var fixedProps=getFixedProps(props);applyPlugins(fixedProps);var Child=_.nameFn(fixedProps.constructor.value,name);fixedProps.constructor.value=Child;Child.prototype=Object.create(Parent.prototype,fixedProps);return Child};var getFixedProps=function(props){return Object.keys(props).reduce(function(result,k){var descriptor=Object.getOwnPropertyDescriptor(props,k);delete descriptor.enumerable;if(descriptor.value==null)delete descriptor.writable;result[k]=descriptor;return result},{})};var addPlugin=function(plugin){plugins.push(plugin)};var applyPlugins=function(props){plugins.forEach(function(plugin){plugin(props)})};module.exports={create:createClass,use:addPlugin}},{"./utils":4}],3:[function(require,module,exports){var ClassFactory=require("./classFactory");var Extender=ClassFactory.create("Extender",{constructor:function(name){this.name=name},extend:function(Parent,props){if(Parent==null)throwErr("a parent must be provided");if(typeof Parent!="function")throwErr("parent must be a function");if(props==null)throwErr("properties must be provided");if(typeof props!="object")throwErr("properties must be defined using an object");if(props.hasOwnProperty("constructor")&&typeof props.constructor!="function")throwErr("constructor must be a function");return ClassFactory.create(this.name,props,Parent)}});var throwErr=function(msg){throw Error("Cla6 extension error - "+msg)};module.exports=Extender},{"./classFactory":2}],4:[function(require,module,exports){var clone=function(obj){return Object.keys(obj).reduce(function(result,k){var descriptor=Object.getOwnPropertyDescriptor(obj,k);return Object.defineProperty(result,k,descriptor)},{})};var nameFn=function(fn,name){return eval("(function "+name+"() {return fn.apply(this, arguments);})")};module.exports={clone:clone,nameFn:nameFn}},{}]},{},[1])(1)});
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Cla6=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){var ClassFactory=require("./classFactory");var Extender=require("./extender");function Cla6(name,props){if(name==null)throwErr("a name must be provided");if(typeof name!="string")throwErr("name must be a string");if(props!=null){if(typeof props!="object")throwErr("properties must be defined using an object");if(props.hasOwnProperty("constructor")&&typeof props.constructor!="function")throwErr("constructor must be a function")}if(props==null)return new Extender(name);else return ClassFactory.create(name,props)}Cla6.use=function(plugin){if(plugin==null)throwErr("a plugin must be provided");if(typeof plugin!="function")throwErr("plugin must be a function");ClassFactory.use(plugin)};var throwErr=function(msg){throw Error("Cla6 error - "+msg)};module.exports=Cla6},{"./classFactory":2,"./extender":3}],2:[function(require,module,exports){var _=require("./utils");var plugins=[];var classExtensions={mixin:function(props){if(arguments.length>1){Array.prototype.forEach.call(arguments,function(props){this.mixin(props)},this)}else{if(props==null)throwErr("properties must be provided");if(typeof props!="object")throwErr("properties must be defined using an object");descriptors=_.toDescriptors(props);applyPlugins(descriptors);Object.defineProperties(this.prototype,descriptors)}return this}};var createClass=function(name,props,Parent){props=_.clone(props);if(typeof Parent!="function")Parent=Object;if(!props.hasOwnProperty("constructor"))props.constructor=function(){Parent.apply(this,arguments)};var descriptors=_.toDescriptors(props);applyPlugins(descriptors);var Child=_.nameFn(descriptors.constructor.value,name);_.extend(Child,classExtensions);descriptors.constructor.value=Child;Child.prototype=Object.create(Parent.prototype,descriptors);return Child};var addPlugin=function(plugin){plugins.push(plugin)};var applyPlugins=function(descriptors){plugins.forEach(function(plugin){plugin(descriptors)})};var throwErr=function(msg){throw Error("Cla6 Class error - "+msg)};module.exports={create:createClass,use:addPlugin}},{"./utils":4}],3:[function(require,module,exports){var ClassFactory=require("./classFactory");var Extender=ClassFactory.create("Extender",{constructor:function(name){this.name=name},extend:function(Parent,props){if(Parent==null)throwErr("a parent must be provided");if(typeof Parent!="function")throwErr("parent must be a function");if(props==null)throwErr("properties must be provided");if(typeof props!="object")throwErr("properties must be defined using an object");if(props.hasOwnProperty("constructor")&&typeof props.constructor!="function")throwErr("constructor must be a function");return ClassFactory.create(this.name,props,Parent)}});var throwErr=function(msg){throw Error("Cla6 extension error - "+msg)};module.exports=Extender},{"./classFactory":2}],4:[function(require,module,exports){var clone=function(obj){return Object.keys(obj).reduce(function(result,k){var descriptor=Object.getOwnPropertyDescriptor(obj,k);return Object.defineProperty(result,k,descriptor)},{})};var extend=function(obj,extension){Object.keys(extension).forEach(function(k){obj[k]=extension[k]})};var nameFn=function(fn,name){return eval("(function "+name+"() {return fn.apply(this, arguments);})")};var toDescriptors=function(props){return Object.keys(props).reduce(function(result,k){var descriptor=Object.getOwnPropertyDescriptor(props,k);delete descriptor.enumerable;if(descriptor.value==null)delete descriptor.writable;result[k]=descriptor;return result},{})};module.exports={clone:clone,extend:extend,nameFn:nameFn,toDescriptors:toDescriptors}},{}]},{},[1])(1)});

@@ -5,2 +5,26 @@ var _ = require('./utils');

var classExtensions = {
mixin: function(props) {
if (arguments.length > 1) {
Array.prototype.forEach.call(arguments, function(props) {
this.mixin(props);
}, this);
} else {
if (props == null)
throwErr('properties must be provided');
if (typeof props != 'object')
throwErr('properties must be defined using an object');
descriptors = _.toDescriptors(props);
applyPlugins(descriptors);
Object.defineProperties(this.prototype, descriptors);
}
return this;
}
};
var createClass = function(name, props, Parent) {

@@ -17,25 +41,14 @@ props = _.clone(props);

var fixedProps = getFixedProps(props);
applyPlugins(fixedProps);
var descriptors = _.toDescriptors(props);
applyPlugins(descriptors);
var Child = _.nameFn(fixedProps.constructor.value, name);
fixedProps.constructor.value = Child;
var Child = _.nameFn(descriptors.constructor.value, name);
_.extend(Child, classExtensions);
Child.prototype = Object.create(Parent.prototype, fixedProps);
descriptors.constructor.value = Child;
Child.prototype = Object.create(Parent.prototype, descriptors);
return Child;
};
var getFixedProps = function(props) {
return Object.keys(props).reduce(function(result, k) {
var descriptor = Object.getOwnPropertyDescriptor(props, k);
delete descriptor.enumerable;
if (descriptor.value == null)
delete descriptor.writable;
result[k] = descriptor;
return result;
}, {});
};
var addPlugin = function(plugin) {

@@ -45,8 +58,12 @@ plugins.push(plugin);

var applyPlugins = function(props) {
var applyPlugins = function(descriptors) {
plugins.forEach(function(plugin) {
plugin(props);
plugin(descriptors);
});
};
var throwErr = function(msg) {
throw Error('Cla6 Class error - ' + msg);
};
module.exports = {

@@ -53,0 +70,0 @@ create: createClass,

@@ -8,2 +8,8 @@ var clone = function(obj) {

var extend = function(obj, extension) {
Object.keys(extension).forEach(function(k) {
obj[k] = extension[k];
});
};
var nameFn = function(fn, name) {

@@ -13,5 +19,20 @@ return eval('(function ' + name + '() {return fn.apply(this, arguments);})');

var toDescriptors = function(props) {
return Object.keys(props).reduce(function(result, k) {
var descriptor = Object.getOwnPropertyDescriptor(props, k);
delete descriptor.enumerable;
if (descriptor.value == null)
delete descriptor.writable;
result[k] = descriptor;
return result;
}, {});
};
module.exports = {
clone: clone,
nameFn: nameFn
extend: extend,
nameFn: nameFn,
toDescriptors: toDescriptors
};
{
"name": "cla6",
"version": "1.1.5",
"version": "1.2.1",
"main": "lib/cla6.js",

@@ -5,0 +5,0 @@ "repository": {

# Cla6.js
ES6 style class system.
Although originally designed for
use with [Node.js](http://nodejs.org) and installable via `npm install cla6`,
ES6 style class system.
Provides a class factory with additional functionality, like [`mixins`](#mixins) and [`plugins`](#plugins). Although originally designed for use with [Node.js](http://nodejs.org) and installable via `npm install cla6`,
it can also be used directly in the browser.

@@ -12,3 +12,3 @@

## Example
## Basic Example

@@ -49,2 +49,3 @@ ```js

## Why Use It
- Easy to use

@@ -97,5 +98,45 @@ - Easy to read

## Mixins
Each class created by Cla6 can be extended using a mixin. Mixins can be applied at class creation or during run time.
```js
var mixin1 = {
method1: function() {
console.log('mixin1');
}
};
var mixin2 = {
method2: function() {
console.log('mixin2');
}
};
var mixin3 = {
method3: function() {
console.log('mixin3');
}
};
var Klass = Cla6('Klass', {
constructor: function() {
console.log("klass");
},
}).mixin(mixin1, mixin2);
Klass.mixin(mixin3);
var obj = new Klass(); // klass
obj.method1(); // mixin1
obj.method2(); // mixin2
obj.method3(); // mixin3
```
## Plugins
Plugins can be applied by Cla6 using the following syntax:
A Cla6 plugin is a manupulation function which gets the classe's descriptors anytime before it gets created, thus the properties can be manipulated. Multipile plugins can be applied and will be called by their use order.
Note, each plugin will affect the descriptors object for the next plugin in the plugins chain.
```js

@@ -105,4 +146,6 @@ Cla6.use(plugin);

The plugin will be called with the classe's properties object anytime before it gets created, thus the properties can be manipulated. No official plugins are yet available, please stay tuned.
The official plugins currently available are:
- [cla6-hidden](https://github.com/DAB0mB/cla6-hidden)
## Download

@@ -109,0 +152,0 @@ The source is available for download from

@@ -10,3 +10,3 @@ var Chai = require('chai');

it('should throw an error if a name is not provided', function() {
expect(Cla6).to.throw(Error, 'name');
expect(Cla6).to.throw(Error, /name/);
});

@@ -16,3 +16,3 @@

var boundCla6 = Cla6.bind(null, false);
expect(boundCla6).to.throw(Error, 'name', 'string');
expect(boundCla6).to.throw(Error, /name.*string/);
});

@@ -22,3 +22,3 @@

var boundCla6 = Cla6.bind(null, 'Klass', false);
expect(boundCla6).to.throw(Error, 'properties', 'object');
expect(boundCla6).to.throw(Error, /properties.*object/);
});

@@ -31,72 +31,70 @@

expect(boundCla6).to.throw(Error, 'constructor', 'function');
expect(boundCla6).to.throw(Error, /constructor.*function/);
});
});
describe('properties', function() {
it('should create an empty class if no properties provided', function() {
var Klass = Cla6('Klass', {});
expect(Klass).to.be.a('function');
expect(Klass.name).to.equal('Klass');
expect(Klass.prototype).to.be.an.instanceOf(Object);
expect(Klass.prototype.constructor).to.equal(Klass);
});
it('should create an empty class if no properties provided', function() {
var Klass = Cla6('Klass', {});
expect(Klass).to.be.a('function');
expect(Klass.name).to.equal('Klass');
expect(Klass.prototype).to.be.an.instanceOf(Object);
expect(Klass.prototype.constructor).to.equal(Klass);
});
it('should create a class with the constructor provided', function() {
var constructor = spy(function() {});
it('should create a class with the constructor provided', function() {
var constructor = spy(function() {});
var Klass = Cla6('Klass', {
constructor: constructor
});
expect(Klass).to.be.a('function');
expect(Klass.name).to.equal('Klass');
expect(Klass.prototype).to.be.an.instanceOf(Object);
expect(Klass.prototype.constructor).to.equal(Klass);
new Klass();
expect(constructor).to.have.been.called.once;
var Klass = Cla6('Klass', {
constructor: constructor
});
it('should create a class with the methods provided', function() {
var method = spy(function() {});
expect(Klass).to.be.a('function');
expect(Klass.name).to.equal('Klass');
expect(Klass.prototype).to.be.an.instanceOf(Object);
expect(Klass.prototype.constructor).to.equal(Klass);
new Klass();
expect(constructor).to.have.been.called.once;
});
var Klass = Cla6('Klass', {
method: method
});
it('should create a class with the methods provided', function() {
var method = spy(function() {});
expect(Klass).to.be.a('function');
expect(Klass.name).to.equal('Klass');
expect(Klass.prototype).to.be.an.instanceOf(Object);
expect(Klass.prototype.constructor).to.equal(Klass);
new Klass().method();
expect(method).to.have.been.called.once;
var Klass = Cla6('Klass', {
method: method
});
it('should create a class with the accessors provided', function() {
var getter = spy(function() {});
var setter = spy(function() {});
expect(Klass).to.be.a('function');
expect(Klass.name).to.equal('Klass');
expect(Klass.prototype).to.be.an.instanceOf(Object);
expect(Klass.prototype.constructor).to.equal(Klass);
new Klass().method();
expect(method).to.have.been.called.once;
});
var Klass = Cla6('Klass', {
get accessor() {
getter();
},
it('should create a class with the accessors provided', function() {
var getter = spy(function() {});
var setter = spy(function() {});
set accessor(value) {
setter(value);
}
});
var Klass = Cla6('Klass', {
get accessor() {
getter();
},
expect(Klass).to.be.a('function');
expect(Klass.name).to.equal('Klass');
expect(Klass.prototype).to.be.an.instanceOf(Object);
expect(Klass.prototype.constructor).to.equal(Klass);
set accessor(value) {
setter(value);
}
});
var k = new Klass();
k.accessor = k.accessor;
expect(getter).to.have.been.called.once;
expect(setter).to.have.been.called.once;
});
expect(Klass).to.be.a('function');
expect(Klass.name).to.equal('Klass');
expect(Klass.prototype).to.be.an.instanceOf(Object);
expect(Klass.prototype.constructor).to.equal(Klass);
var k = new Klass();
k.accessor = k.accessor;
expect(getter).to.have.been.called.once;
expect(setter).to.have.been.called.once;
});
});

@@ -17,3 +17,3 @@ var Chai = require('chai');

var extend = Cla6('Child').extend;
expect(extend).to.throw(Error, 'parent');
expect(extend).to.throw(Error, /parent/);
});

@@ -23,3 +23,3 @@

var extend = Cla6('Child').extend.bind(null, false);
expect(extend).to.throw(Error, 'parent', 'function');
expect(extend).to.throw(Error, /parent.*function/);
});

@@ -30,3 +30,3 @@

var extend = Cla6('Child').extend.bind(null, Parent);
expect(extend).to.throw(Error, 'properties');
expect(extend).to.throw(Error, /properties/);
});

@@ -37,3 +37,3 @@

var extend = Cla6('Child').extend.bind(null, Parent, false);
expect(extend).to.throw(Error, 'properties', 'object');
expect(extend).to.throw(Error, /properties.*object/);
});

@@ -48,3 +48,3 @@

expect(extend).to.throw(Error, 'constructor', 'function');
expect(extend).to.throw(Error, /constructor.*function/);
});

@@ -51,0 +51,0 @@ });

@@ -10,2 +10,3 @@ var Chai = require('chai');

require('./extender');
require('./mixins');
require('./plugins');

@@ -11,3 +11,3 @@ var Chai = require('chai');

it('should throw an error if a plugin is not provided', function() {
expect(Cla6.use).to.throw(Error, 'plugin');
expect(Cla6.use).to.throw(Error, /plugin/);
});

@@ -17,14 +17,14 @@

bondUse = Cla6.use.bind(null, false);
expect(bondUse).to.throw(Error, 'plugin', 'function');
expect(bondUse).to.throw(Error, /plugin.*function/);
});
});
it('should call plugin with class properties', function() {
var plugin = spy(function(fixedProps) {
expect(fixedProps).to.have.all.keys('constructor', 'method', 'accessor');
expect(fixedProps.method.value).to.equal(props.method);
it('should call plugin with class descriptors', function() {
var plugin = spy(function(descriptors) {
expect(descriptors).to.have.all.keys('constructor', 'method', 'accessor');
expect(descriptors.method.value).to.equal(props.method);
var accessorDescriptor = Object.getOwnPropertyDescriptor(props, 'accessor');
expect(fixedProps.accessor.get).to.equal(accessorDescriptor.get);
expect(fixedProps.accessor.set).to.equal(accessorDescriptor.set);
expect(descriptors.accessor.get).to.equal(accessorDescriptor.get);
expect(descriptors.accessor.set).to.equal(accessorDescriptor.set);
});

@@ -31,0 +31,0 @@

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