Comparing version 0.2.1 to 0.3.0
{ | ||
"name": "pd", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Manage propertyDescriptors, an OO utility", | ||
@@ -10,7 +10,7 @@ "keywords": ["oo", "oop", "propertydescriptor", "arch", "utility"], | ||
"engines": { | ||
"node": "0.5.x" | ||
"node": "0.6.x" | ||
}, | ||
"devDependencies": { | ||
"tester": "0.0.2" | ||
"tester": "0.0.4" | ||
} | ||
} |
@@ -22,12 +22,12 @@ # pd <a name="_pd" href="#_pd"><small><sup>link</sup></small></a> | ||
- [Doing Object Oriented JavaScript][3] | ||
- [Doing OO JS part 2][5] | ||
- [Doing OO JS part 3][6] | ||
## Examples <a name="Examples" href="#Examples"><small><sup>link</sup></small></a> | ||
More coming soon! | ||
- [vows-fluent annotated code][2] **OUTDATED uses pd 0.1.1** | ||
## Documentation <a name="Documentation" href="#Documentation"><small><sup>link</sup></small></a> | ||
See the [annotated source code][1] **OUTDATED shows pd 0.1.1** | ||
See the source >_> | ||
@@ -83,30 +83,16 @@ ### pd (obj) <a name="pd" href="#pd"><small><sup>link</sup></small></a> | ||
### pd.mixin (target, source) <a name="pd.mixin" href="#pd.mixin"><small><sup>link</sup></small></a> | ||
### pd.make (proto, props) <a name="pd.make" href="#pd.make"><small><sup>link</sup></small></a> | ||
pd.mixin is virtually the same as pd.extend however it only takes two arguments and it will overwrite | ||
the constructor property of target with the function composition of target.constructor and source.constructor. | ||
pd.make takes a prototype and some properties, it creates a new instance of the prototype and extends | ||
that instances with the properties. | ||
pd.mixin(SomeProto, { | ||
addThisMethod: function () { }, | ||
constructor: function () { | ||
// invoke this constructor code after SomeProto.constructor | ||
} | ||
pd.make(Proto, { | ||
prop: "42" | ||
}); | ||
### pd.extendNatives <a name="pd.extendNatives" href="#pd.extendNatives"><small><sup>link</sup></small></a> | ||
pd.extendNatives extends some native objects and then returns pd. | ||
### pd.beget (proto, args..) <a name="pd.beget" href="#pd.beget"><small><sup>link</sup</small></a> | ||
Specifically it will set the following if they don't exist | ||
pd.beget takes a prototype and creates a new instance of it. it will then call the constructor | ||
property of the new instance with the arguments and finally returns the object | ||
- Object.extend (same as pd.extend) | ||
- Object.getOwnPropertyDescriptors (same as pd) | ||
- Object.prototype.new (explained below) | ||
`Object.prototype.new` generates a new object by calling `Object.create(this)`, then invokes the constructor of that object if it exists and then returns it. | ||
This basically is meant as sugar to allow ["prototypes as classes"][4] | ||
Example: | ||
var Proto = { | ||
@@ -121,11 +107,33 @@ method: function () { | ||
var o = Proto.new(42); // "constructed", 42 | ||
var o = pd.new(Proto, 42); // "constructed", 42 | ||
console.log(Proto.isPrototypeOf(o)); // true | ||
o.method(); // "method" | ||
This basically is meant as sugar to allow ["prototypes as classes"][4] | ||
### pd.extendNatives <a name="pd.extendNatives" href="#pd.extendNatives"><small><sup>link</sup></small></a> | ||
pd.extendNatives extends some native objects and then returns pd. | ||
Specifically it will set the following if they don't exist | ||
- Object.extend (same as pd.extend) | ||
- Object.getOwnPropertyDescriptors (same as pd) | ||
- Object.prototype.new (same as pd.new) | ||
- Object.make (same as pd.make) | ||
An example of `.new` which only takes arguments | ||
Proto.new(42) | ||
### pd.Name <a name="pd.Name" href="#pdName"><small><sup>link</sup</small></a> | ||
pd.Name does shit, read the source \o/. I mean docs soon. | ||
[1]: http://raynos.github.com/pd/docs/pd.html | ||
[2]: http://raynos.github.com/vows-fluent/docs/vows-fluent.html | ||
[3]: http://raynos.org/blog/4/Doing-Object-Oriented-JavaScript | ||
[4]: http://www.2ality.com/2011/06/prototypes-as-classes.html | ||
[4]: http://www.2ality.com/2011/06/prototypes-as-classes.html | ||
[5]: http://raynos.org/blog/5/Doing-Object-Oriented-Javascript---part-2 | ||
[6]: http://raynos.org/blog/7/Doing-Object-Oriented-Javascript---part-3 |
248
src/pd.js
@@ -1,113 +0,171 @@ | ||
/* | ||
pd will return all the own propertydescriptors of the object | ||
!(function (exports) { | ||
"use strict"; | ||
@param Object obj - object to get pds from. | ||
/* | ||
pd will return all the own propertydescriptors of the object | ||
@return Object - A hash of key/propertyDescriptors | ||
*/ | ||
var pd = function _pd(obj) { | ||
var keys = Object.getOwnPropertyNames(obj); | ||
var o = {}; | ||
keys.forEach(function _each(key) { | ||
var pd = Object.getOwnPropertyDescriptor(obj, key); | ||
o[key] = pd; | ||
}); | ||
return o; | ||
}; | ||
@param Object obj - object to get pds from. | ||
/* | ||
Will extend native objects with utility methods | ||
@param Boolean prototypes - flag to indicate whether you want to extend | ||
prototypes as well | ||
*/ | ||
pd.extendNatives = function _extendNatives(prototypes) { | ||
if (!Object.getOwnPropertyDescriptors) { | ||
Object.defineProperty(Object, "getOwnPropertyDescriptors", { | ||
value: pd, | ||
configurable: true | ||
@return Object - A hash of key/propertyDescriptors | ||
*/ | ||
function pd(obj) { | ||
var keys = Object.getOwnPropertyNames(obj); | ||
var o = {}; | ||
keys.forEach(function _each(key) { | ||
var pd = Object.getOwnPropertyDescriptor(obj, key); | ||
o[key] = pd; | ||
}); | ||
return o; | ||
} | ||
if (!Object.extend) { | ||
Object.defineProperty(Object, "extend", { | ||
value: pd.extend, | ||
configurable: true | ||
/* | ||
Will extend native objects with utility methods | ||
@param Boolean prototypes - flag to indicate whether you want to extend | ||
prototypes as well | ||
*/ | ||
function extendNatives(prototypes) { | ||
if (!Object.getOwnPropertyDescriptors) { | ||
Object.defineProperty(Object, "getOwnPropertyDescriptors", { | ||
value: pd, | ||
configurable: true | ||
}); | ||
} | ||
if (!Object.extend) { | ||
Object.defineProperty(Object, "extend", { | ||
value: pd.extend, | ||
configurable: true | ||
}); | ||
} | ||
if (!Object.make) { | ||
Object.defineProperty(Object, "make", { | ||
value: pd.make, | ||
configurable: true | ||
}); | ||
} | ||
if (!Object.beget) { | ||
Object.defineProperty(Object, "beget", { | ||
value: beget, | ||
configurable: true | ||
}) | ||
} | ||
if (!Object.prototype["beget"] && prototypes) { | ||
Object.defineProperty(Object.prototype, "beget", { | ||
value: function _beget() { | ||
var o = Object.create(this); | ||
o.constructor && o.constructor.apply(o, arguments); | ||
return o; | ||
}, | ||
configurable: true | ||
}); | ||
} | ||
if (!Object.Name) { | ||
Object.defineProperty(Object, "Name", { | ||
value: Name, | ||
configurable: true | ||
}); | ||
} | ||
return pd; | ||
} | ||
/* | ||
Extend will extend the firat parameter with any other parameters | ||
passed in. Only the own property names will be extended into | ||
the object | ||
@param Object target - target to be extended | ||
@arguments Array [target, ...] - the rest of the objects passed | ||
in will extended into the target | ||
@return Object - the target | ||
*/ | ||
function extend(target) { | ||
var objs = Array.prototype.slice.call(arguments, 1); | ||
objs.forEach(function (obj) { | ||
var props = Object.getOwnPropertyNames(obj); | ||
props.forEach(function (key) { | ||
target[key] = obj[key]; | ||
}); | ||
}); | ||
return target; | ||
} | ||
if (!Object.prototype.new && prototypes) { | ||
Object.defineProperty(Object.prototype, "new", { | ||
value: function _new() { | ||
var o = Object.create(this); | ||
o.constructor && o.constructor.apply(o, arguments); | ||
return o; | ||
}, | ||
configurable: true | ||
/* | ||
beget will generate a new object from the proto, any other arguments | ||
will be passed to proto.constructor | ||
@param Object proto - the prototype to use for the new object | ||
@arguments Array [proto, ...] - the rest of the arguments will | ||
be passed into proto.constructor | ||
@return Object - the newly created object | ||
*/ | ||
function beget(proto) { | ||
var o = Object.create(proto); | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
proto.constructor && proto.constructor.apply(o, args); | ||
return o; | ||
} | ||
/* | ||
make will call Object.create with the proto and pd(props) | ||
@param Object proto - the prototype to inherit from | ||
@param Object props - properties to extend the new object with | ||
@return Object - the new object | ||
*/ | ||
function make (proto, props) { | ||
return Object.create(proto, pd(props)); | ||
} | ||
function defineNamespace(object, namespace) { | ||
var privates = Object.create(object), | ||
base = object.valueOf; | ||
Object.defineProperty(object, 'valueOf', { | ||
value: function valueOf(value) { | ||
if (value !== namespace || this !== object) { | ||
return base.apply(this, arguments); | ||
} else { | ||
return privates; | ||
} | ||
} | ||
}); | ||
return privates; | ||
} | ||
return pd; | ||
}; | ||
/* | ||
Mixin is similar to Extend except it will combine the two constructor | ||
functions to ensure that the mixin is also constructed. | ||
@param Object target - target which the source will be mixed into | ||
@param Object source - the mixin | ||
function Name() { | ||
var namespace = {}; | ||
@return Object - the target | ||
*/ | ||
pd.mixin = function _mixin(target, source) { | ||
var constructorTarget = target.constructor, | ||
constructorSource = source.constructor; | ||
pd.extend(target, source); | ||
if (constructorTarget && constructorSource) { | ||
target.constructor = function _constructor() { | ||
var ret = constructorTarget.apply(this, arguments); | ||
constructorSource.apply(this, arguments); | ||
return ret; | ||
return function name(object) { | ||
var privates = object.valueOf(namespace); | ||
if (privates !== object) { | ||
return privates; | ||
} else { | ||
return defineNamespace(object, namespace); | ||
} | ||
}; | ||
}; | ||
return target; | ||
}; | ||
/* | ||
Extend will extend the firat parameter with any other parameters | ||
passed in. Only the own property names will be extended into | ||
the object | ||
} | ||
@param Object target - target to be extended | ||
@arguments Array [target, ...] - the rest of the objects passed | ||
in will extended into the target | ||
@return Object - the target | ||
*/ | ||
pd.extend = function _extend(target) { | ||
var objs = Array.prototype.slice.call(arguments, 1); | ||
objs.forEach(function (obj) { | ||
var props = Object.getOwnPropertyNames(obj); | ||
props.forEach(function (key) { | ||
target[key] = obj[key]; | ||
}); | ||
extend(pd, { | ||
make: make, | ||
extend: extend, | ||
beget: beget, | ||
extendNatives: extendNatives, | ||
Name: Name | ||
}); | ||
return target; | ||
}; | ||
/* | ||
new will generate a new object from the proto, any other arguments | ||
will be passed to proto.constructor | ||
exports(pd); | ||
@param Object proto - the prototype to use for the new object | ||
@arguments Array [proto, ...] - the rest of the arguments will | ||
be passed into proto.constructor | ||
})(function (data) { | ||
if (typeof module !== "undefined" && module.exports) { | ||
module.exports = data; | ||
} else { | ||
window.pd = data; | ||
} | ||
}); | ||
@return Object - the newly created object | ||
*/ | ||
pd.new = function _new(proto) { | ||
var o = Object.create(proto); | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
proto.constructor && proto.constructor.apply(o, args); | ||
return o; | ||
}; | ||
if ("undefined" !== typeof module && module.exports) { | ||
module.exports = pd; | ||
} | ||
@@ -8,3 +8,5 @@ var pd = require("../src/pd.js").extendNatives(true), | ||
assert(pd); | ||
assert(pd.mixin); | ||
assert(pd.beget); | ||
assert(pd.make); | ||
assert(pd.Name); | ||
assert(pd.extend); | ||
@@ -16,3 +18,5 @@ assert(pd.extendNatives); | ||
assert.deepEqual(Object.extend, pd.extend); | ||
assert(Object.prototype.new); | ||
assert(Object.prototype.beget); | ||
assert(Object.beget); | ||
assert.deepEqual(Object.beget, pd.beget); | ||
assert(Object.getOwnPropertyDescriptors); | ||
@@ -67,3 +71,3 @@ assert.deepEqual(Object.getOwnPropertyDescriptors, pd); | ||
}, | ||
"test pd.mixin": function () { | ||
"test .beget": function () { | ||
var Proto = { | ||
@@ -78,36 +82,23 @@ method: function () { | ||
var Mixin = { | ||
otherMethod: function () { | ||
return 43; | ||
}, | ||
constructor: function () { | ||
this.b = true; | ||
} | ||
} | ||
pd.mixin(Proto, Mixin); | ||
var o = Proto.new(); | ||
var o = Proto.beget(); | ||
assert.equal(o.method(), 42); | ||
assert.equal(o.otherMethod(), 43); | ||
assert.equal(o.a, true); | ||
assert.equal(o.b, true); | ||
}, | ||
"test .new": function () { | ||
var Proto = { | ||
method: function () { | ||
return 42; | ||
}, | ||
constructor: function () { | ||
this.a = true; | ||
} | ||
} | ||
var o = Proto.new(); | ||
assert.equal(o.method(), 42); | ||
assert.equal(o.a, true); | ||
assert(Proto.isPrototypeOf(o)); | ||
var o = pd.new(Proto); | ||
var o = pd.beget(Proto); | ||
assert.equal(o.method(), 42); | ||
assert.equal(o.a, true); | ||
assert(Proto.isPrototypeOf(o)); | ||
}, | ||
"test make": function () { | ||
var Proto = {}; | ||
var o = pd.make(Proto, { | ||
"one": "two", | ||
"three": Proto | ||
}); | ||
assert(Proto.isPrototypeOf(o)); | ||
assert(o.hasOwnProperty("one")); | ||
assert(o.one === "two"); | ||
assert(o.three === Proto); | ||
assert(o.hasOwnProperty("three")); | ||
} | ||
@@ -114,0 +105,0 @@ }; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12411
247
136
0