Skip to content

Commit

Permalink
updated source code to be better to minify. Added Name test
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos committed Jan 14, 2012
1 parent 07f7925 commit 55f3afd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 127 deletions.
219 changes: 92 additions & 127 deletions lib/pd.js
Original file line number Diff line number Diff line change
@@ -1,137 +1,102 @@
var slice = [].slice;

extend(getOwnPropertyDescriptors, {
bindAll: bindAll,
extend: extend,
Name: Name
});

module.exports = getOwnPropertyDescriptors;

/*
pd will return all the own propertydescriptors of the object
@param Object object - object to get pds from.
@return Object - A hash of key/propertyDescriptors
*/
function getOwnPropertyDescriptors(object) {
var keys = Object.getOwnPropertyNames(object),
returnObj = {};

keys.forEach(getPropertyDescriptor);

return returnObj;

function getPropertyDescriptor(key) {
var pd = Object.getOwnPropertyDescriptor(object, key);
returnObj[key] = 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 = slice.call(arguments, 1);

objs.forEach(extendTargetWithProperties);

return target;

function extendTargetWithProperties(source) {
Object.getOwnPropertyNames(source).forEach(extendTarget);

function extendTarget(key) {
Object.defineProperty(target, key,
Object.getOwnPropertyDescriptor(source, key));
}
}
}

/*
bindAll binds all methods to have their context set to the object
@param Object obj - the object to bind methods on
@param Array methods - optional whitelist of methods to bind
@return Object - the bound object
*/
function bindAll(obj, whitelist) {
var keys = Object.keys(obj).filter(stripNonMethods);

(whitelist || keys).forEach(bindMethod);

function stripNonMethods(name) {
return typeof obj[name] === "function";
(function (Object, slice) {
extend(pd, {
bindAll: bindAll,
extend: extend,
Name: Name
});

typeof window !== "undefined" ? window.pd = pd : module.exports = pd;

/*
pd will return all the own propertydescriptors of the object
@param Object object - object to get pds from.
@return Object - A hash of key/propertyDescriptors
*/
function pd(obj, retObj) {
retObj = {};
Object.getOwnPropertyNames(obj).forEach(function(key) {
var pd = Object.getOwnPropertyDescriptor(obj, key);
retObj[key] = pd;
});
return retObj;
}

function bindMethod(name) {
obj[name] = obj[name].bind(obj);
/*
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) {
slice.call(arguments, 1).forEach(function(source) {
Object.defineProperties(target, pd(source));
});
return target;
}

return obj;
}

/*
defines a namespace object. This hides a "privates" object on object
under the "key" namespace
/*
defines a namespace object. This hides a "privates" object on object
under the "key" namespace
@param Object object - object to hide a privates object on
@param Object namespace - key to hide it under
@author Gozala : https://gist.github.com/1269991
@return Object privates
*/
function defineNamespace(object, namespace) {
var privates = Object.create(object),
base = object.valueOf;

Object.defineProperty(object, 'valueOf', {
value: valueOf,
writable: true
});
@param Object object - object to hide a privates object on
@param Object namespace - key to hide it under
return privates;
@author Gozala : https://gist.github.com/1269991
function valueOf(value) {
if (value !== namespace || this !== object) {
return base.apply(this, arguments);
} else {
return privates;
}
@return Object privates
*/
function namespace(obj, key) {
var store = Object.create(obj),
valueOf = obj.valueOf;

Object.defineProperty(obj, "valueOf", {
value: function(value) {
return value !== key ? valueOf.apply(this, arguments) : store;
},
writable: true
});

return store;
}
}

/*
Constructs a Name function, when given an object it will return a
privates object.
@author Gozala : https://gist.github.com/1269991
@return Function name
*/
function Name() {
var namespace = {};

return name;

function name(object) {
var privates = object.valueOf(namespace);
if (privates !== object) {
return privates;
} else {
return defineNamespace(object, namespace);

/*
Constructs a Name function, when given an object it will return a
privates object.
@author Gozala : https://gist.github.com/1269991
@return Function name
*/
function Name() {
var key = {};
return function name(obj) {
var store = obj.valueOf(key);
return store !== obj ? store : namespace(obj, key)
}
}
}

/*
bindAll binds all methods to have their context set to the object
@param Object obj - the object to bind methods on
@param Array methods - optional whitelist of methods to bind
@return Object - the bound object
*/
function bindAll(obj, whitelist) {
(whitelist || Object.keys(obj).filter(function(key) {
return obj[key].call;
})).forEach(function(name) {
obj[name] = obj[name].bind(obj)
});

return obj;
}
})(Object, [].slice);
6 changes: 6 additions & 0 deletions test/pd-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ suite("pd", function () {
assert(foo() === global);
assert(bar() === two);
});

test("Name", function () {
var name = pd.Name();
name(name).foo = 42;
assert(name(name).foo === 42);
});
});


0 comments on commit 55f3afd

Please sign in to comment.