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

inher

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inher - npm Package Compare versions

Comparing version 0.0.2 to 0.1.0

17

Gruntfile.js

@@ -44,3 +44,18 @@ 'use strict';

}
}
},
release: {
options: {
bump: true, //default: true
file: 'package.json', //default: package.json
add: true, //default: true
commit: true, //default: true
tag: true, //default: true
push: true, //default: true
pushTags: true, //default: true
npm: true, //default: true
tagName: 'v<%= version %>', //default: '<%= version %>'
commitMessage: 'releasing v<%= version %>', //default: 'release <%= version %>'
tagMessage: 'v<%= version %>' //default: 'Version <%= version %>'
}
},
});

@@ -47,0 +62,0 @@

@@ -38,1 +38,16 @@ /**

};
/**
* Apply an array of arguments to a new instance.
*
* @param {Function} Ctor The constructor to instanciate.
* @param {...*} Any number of any type args.
* @return {Object} An instance of Ctor.
*/
helpers.applyArgsToCtor = function(Ctor) {
var args = Array.prototype.slice.call(arguments, 1);
var instance = Object.create(Ctor.prototype);
instance.constructor = Ctor;
Ctor.apply(instance, args);
return instance;
};

62

lib/inher.js

@@ -89,15 +89,11 @@ /*jshint unused:false */

*
* @param {Inher} ParentCtor Inher inherited Ctor.
* @param {...*} Any number of any type args.
* @param {Inher} Ctor Inher inherited Ctor.
* @return {Inher} The singleton instance for the provided Ctor.
*/
Inher.getInstance = function(ParentCtor) {
if (ParentCtor[Inher.KEY].singleton) {
return ParentCtor[Inher.KEY].singleton;
Inher.getInstance = function(Ctor) {
if (Ctor[Inher.KEY].singleton) {
return Ctor[Inher.KEY].singleton;
}
var args = Array.prototype.slice.call(arguments, 1);
var singleton = Object.create(ParentCtor.prototype);
singleton.constructor = ParentCtor;
ParentCtor.apply(singleton, args);
ParentCtor[Inher.KEY].singleton = singleton;
var singleton = new Ctor();
Ctor[Inher.KEY].singleton = singleton;
return singleton;

@@ -108,5 +104,27 @@ };

/**
* Augments the constructor with static helper functions, references
* and Inher's private store.
* The API exposed static function
*
* @param {Function} Ctor The Constructor to augment.
* @return {Function} The Ctor augmented but not required, passed Ctor gets
* mutated anyway.
*/
Inher.wrap = function(Ctor) {
/** @constructor */
function TempCtor() {}
TempCtor.prototype = Ctor.prototype;
/** @constructor */
function Clone() {
Ctor.apply(this, arguments);
}
Clone.prototype = new TempCtor();
return Inher._wrapActual(Clone);
};
/**
* The actual Wrapping implementation, augments the constructor with
* static helper functions, references and Inher's private store.
*
* @param {Function} Ctor The Constructor to augment, WARNING function gets

@@ -122,3 +140,6 @@ * mutated by design.

*/
Inher.wrap = function(Ctor, optParentCtor, optStubbedArgs, optChildCtor) {
Inher._wrapActual = function(Ctor, optParentCtor, optStubbedArgs, optChildCtor) {
if (Inher.isInher(Ctor)) {
return Ctor;
}
// partially apply extend to singleton instance

@@ -213,3 +234,3 @@ Ctor.extend = Inher.extend.bind(null, Ctor);

Inher.wrap(Ctor, ParentCtor, args, ChildCtor);
Inher._wrapActual(Ctor, ParentCtor, args, ChildCtor);

@@ -244,3 +265,16 @@ return Ctor;

}
if (typeof Ctor.extend !== 'function') {
return false;
}
if (typeof Ctor.getInstance !== 'function') {
return false;
}
if (typeof Ctor.mixin !== 'function') {
return false;
}
return true;
};
{
"name": "inher",
"version": "0.0.2",
"version": "0.1.0",
"main": "./",

@@ -30,8 +30,8 @@ "description": "Pseudo-classical Inheritance at its best",

"grunt-mocha-test": "~0.7.0",
"sinon": "~1.8.1",
"chai": "~1.9.0",
"mocha": "~1.17.1"
"mocha": "~1.17.1",
"grunt-release": "~0.6.0"
},
"dependencies": {
"sinon": "~1.8.1"
}
"dependencies": {}
}

@@ -43,3 +43,3 @@ # Inher

## Documentation
## API

@@ -56,3 +56,3 @@ ### extend() Creates new children

Extend uses the [Pseudo Classical][proto.post] [pattern][addy.proto], the same exact mechanism that is used by [`util.inherits`][util.inherits].
Extend uses the Pseudo Classical [pattern][addy.proto], the same exact mechanism that is used by [`util.inherits`][util.inherits].

@@ -115,3 +115,2 @@ [Check out the tests relating to `extend()` and inheritance.][test.inheritance]

[proto.post]: http://dsheiko.com/weblog/prototypal-inheritance-in-javascript-for-modules/
[addy.proto]: http://addyosmani.com/resources/essentialjsdesignpatterns/book/#constructorpatternjavascript

@@ -165,2 +164,3 @@ [util.inherits]: http://nodejs.org/docs/latest/api/util.html#util_util_inherits_constructor_superconstructor

* **Constructor** `...Function|Array.<Function>` :: Any number of Constructors passed as separate arguments or in an Array.
* Returns `void` Nothing.

@@ -241,7 +241,7 @@ The `mixin()` method will merge the prototype of the mixed in Ctors and ensure their constructors are invoked. The full inheritance chain of a Mixin is honored along with their respective Stubbed Arguments, if any. The Mixin's constructor will be invoked in the same context and therefore you can easily interact with internal properties and methods.

> Ctor.getInstance(...args)
> Ctor.getInstance()
* **...args** `*` :: Any number of any type of arguments, will be passed to constructors.
* Returns `Object` An instance of Ctor.
Use the `getInstance()` for getting a singleton of the Constructor. Note that arguments can only be passed on the first invocation of `getInstance` which is the one that actually creates a new instance of the Constructor. So be proactive if your singletons require instantiation arguments and invoke early.
Use the `getInstance()` for getting a singleton of the Constructor.

@@ -253,4 +253,4 @@ ```js

// create the singleton to pass app
UserController.getInstance(require('some-fancy-DI');
// Get the singleton
UserController.getInstance();
```

@@ -272,2 +272,3 @@

* **VanillaCtor** `Function` :: A vanilla constructor.
* Returns `Function` :: A clone copy of the VanillaCtor augmented with Inher's static properties and functions.

@@ -282,8 +283,9 @@ The `wrap()` method is only available from the Inher module, it will add all the static methods that every Inher ctor has. `wrap()` is used by Inher itself to create the new ancestors.

inher.wrap(EventEmitter);
var IeventEmitter = inher.wrap(EventEmitter);
var Thing = EventEmitter.extend();
var Thing = IeventEmitter.extend();
var newThing = new Thing();
newThing instanceof IeventEmitter; // true
newThing instanceof EventEmitter; // true

@@ -312,2 +314,7 @@ ```

## Release History
- **v0.1.0**, *7 Feb 2014*
- `wrap()` Now does not muttate the Ctor passed.
- `getInstance()` will not accept arguments, it's an anti-pattern.
- **v0.0.3**, *7 Feb 2014*
- Clear dependencies
- **v0.0.2**, *7 Feb 2014*

@@ -314,0 +321,0 @@ - Bug Fixes

@@ -28,3 +28,3 @@ /*jshint unused:false */

var child = new Child(1, 2);
var childSingleton = Child.getInstance(3, 4);
var childSingleton = new Child(3, 4);

@@ -47,3 +47,3 @@ assert.equal(child.a, 1);

var grandChild = new GrandChild('lol');
var grandChildSingleton = GrandChild.getInstance(0);
var grandChildSingleton = new GrandChild(0);

@@ -68,3 +68,3 @@ assert.equal(grandChild.a, 5);

var grandChild = new GrandChild(9, 'lol');
var grandChildSingleton = GrandChild.getInstance(8, 0);
var grandChildSingleton = new GrandChild(8, 0);

@@ -93,3 +93,3 @@ assert.equal(grandChild.a, 5);

var greatGrandChild = new GreatGrandChild('lol');
var greatGrandChildSingleton = GreatGrandChild.getInstance(0);
var greatGrandChildSingleton = new GreatGrandChild(0);

@@ -116,3 +116,3 @@ assert.equal(greatGrandChild.a, 5);

var grandChild = new GrandChild('lol');
var grandChildSingleton = GrandChild.getInstance(0);
var grandChildSingleton = new GrandChild(0);

@@ -138,3 +138,3 @@ assert.isFunction(grandChild.a);

var grandChild = new GrandChild();
var grandChildSingleton = GrandChild.getInstance();
var grandChildSingleton = new GrandChild();

@@ -141,0 +141,0 @@ assert.isFunction(grandChild.a);

@@ -48,2 +48,9 @@ /*jshint unused:false */

});
test('4.1.3 Wrapping constructors does not mutate original Ctor', function() {
var InherTypicalCtor = inher.wrap(TypicalCtor);
var Child = InherTypicalCtor.extend();
assert.notProperty(TypicalCtor, 'extend');
assert.notOk(inher.isInher(TypicalCtor));
});
});
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