-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated source code to be better to minify. Added Name test
- Loading branch information
Raynos
committed
Jan 14, 2012
1 parent
07f7925
commit 55f3afd
Showing
2 changed files
with
98 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters