Comparing version 0.7.4 to 0.8.0
294
lib/pd.js
@@ -0,3 +1,5 @@ | ||
var slice = Array.prototype.slice | ||
/* | ||
pd(obj) -> propertyDescriptorsOfObject { | ||
pd { | ||
bindAll: function that binds all the methods of an object to the object | ||
@@ -9,190 +11,172 @@ extend: function that extends the first argument with the rest | ||
*/ | ||
;(function (Object, slice) { | ||
"use strict" | ||
pd.bindAll = bindAll | ||
pd.extend = extend | ||
pd.Name = Name | ||
pd.memoize = asyncMemoize | ||
typeof module !== "undefined" ? module.exports = pd : window.pd = pd | ||
module.exports = { | ||
bindAll: bindAll, | ||
extend: extend, | ||
Name: Name, | ||
memoize: asyncMemoize | ||
} | ||
/* | ||
pd will return all the own propertydescriptors of the object | ||
/* | ||
Extend will extend the first parameter with any other parameters | ||
passed in. Only the own property names will be extended into | ||
the object | ||
@param Object object - object to get pds from. | ||
@param Object target - target to be extended | ||
@arguments Array [target, ...] - the rest of the objects passed | ||
in will extended into the target | ||
@return Object - A hash of key/propertyDescriptors | ||
*/ | ||
function pd(obj) { | ||
var pds = {} | ||
Object.getOwnPropertyNames(obj).forEach(function(key) { | ||
pds[key] = Object.getOwnPropertyDescriptor(obj, key) | ||
@return Object - the target | ||
*/ | ||
function extend(target) { | ||
slice.call(arguments, 1).forEach(function(source) { | ||
Object.getOwnPropertyNames(source).forEach(function (name) { | ||
target[name] = source[name] | ||
}) | ||
return pds | ||
} | ||
}) | ||
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 | ||
/* | ||
defines a namespace object. This hides a "privates" object on object | ||
under the "key" namespace | ||
@param Object target - target to be extended | ||
@arguments Array [target, ...] - the rest of the objects passed | ||
in will extended into the target | ||
@param Object object - object to hide a privates object on | ||
@param Object key - key to hide it under | ||
@return Object - the target | ||
*/ | ||
function extend(target) { | ||
slice.call(arguments, 1).forEach(function(source) { | ||
Object.defineProperties(target, pd(source)) | ||
}); | ||
return target | ||
} | ||
@author Gozala : https://gist.github.com/1269991 | ||
/* | ||
defines a namespace object. This hides a "privates" object on object | ||
under the "key" namespace | ||
@return Object privates | ||
*/ | ||
function namespace(object, key) { | ||
var privates = Object.create(object), | ||
valueOf = object.valueOf | ||
Object.defineProperty(object, "valueOf", { | ||
value: function(value) { | ||
return value !== key ? valueOf.apply(this, arguments) : privates | ||
}, | ||
writable: true, | ||
configurable: true | ||
}) | ||
return privates | ||
} | ||
@param Object object - object to hide a privates object on | ||
@param Object key - key to hide it under | ||
/* | ||
Constructs a Name function, when given an object it will return a | ||
privates object. | ||
@author Gozala : https://gist.github.com/1269991 | ||
@author Gozala : https://gist.github.com/1269991 | ||
@return Object privates | ||
*/ | ||
function namespace(object, key) { | ||
var privates = Object.create(object), | ||
valueOf = object.valueOf | ||
Object.defineProperty(object, "valueOf", { | ||
value: function(value) { | ||
return value !== key ? valueOf.apply(this, arguments) : privates | ||
}, | ||
writable: true, | ||
configurable: true | ||
}) | ||
return privates | ||
@return Function name | ||
*/ | ||
function Name() { | ||
var key = {} | ||
return name | ||
function name(object) { | ||
var privates = object.valueOf(key) | ||
return privates !== object ? privates : namespace(object, key) | ||
} | ||
/* | ||
Constructs a Name function, when given an object it will return a | ||
privates object. | ||
} | ||
@author Gozala : https://gist.github.com/1269991 | ||
/* | ||
bindAll binds all methods to have their context set to the object | ||
@return Function name | ||
*/ | ||
function Name() { | ||
var key = {} | ||
return name | ||
function name(object) { | ||
var privates = object.valueOf(key) | ||
return privates !== object ? privates : namespace(object, key) | ||
} | ||
@param Object obj - the object to bind methods on | ||
@arguments Array [target, ...] - the rest of the objects passed | ||
in will extended into the obj | ||
@return Object - the bound object | ||
*/ | ||
function bindAll(obj) { | ||
extend.apply(null, arguments) | ||
Object.keys(obj).filter(isMethod).forEach(bindMethods) | ||
return obj | ||
function isMethod(name) { | ||
return obj[name] && obj[name].bind === isMethod.bind | ||
} | ||
/* | ||
bindAll binds all methods to have their context set to the object | ||
@param Object obj - the object to bind methods on | ||
@arguments Array [target, ...] - the rest of the objects passed | ||
in will extended into the obj | ||
@return Object - the bound object | ||
*/ | ||
function bindAll(obj) { | ||
pd.extend.apply(null, arguments) | ||
Object.keys(obj).filter(isMethod).forEach(bindMethods) | ||
return obj | ||
function isMethod(name) { | ||
return obj[name] && obj[name].bind === isMethod.bind | ||
} | ||
function bindMethods(name) { | ||
obj[name] = obj[name].bind(obj) | ||
} | ||
function bindMethods(name) { | ||
obj[name] = obj[name].bind(obj) | ||
} | ||
} | ||
/* | ||
default hasher for memoize. Takes the first arguments and returns it | ||
if it's a string, otherwise returns the string "void" | ||
/* | ||
default hasher for memoize. Takes the first arguments and returns it | ||
if it's a string, otherwise returns the string "void" | ||
@param Any x - argument to hash on | ||
@param Any x - argument to hash on | ||
@return String - a hash key | ||
*/ | ||
function defaultHasher(x) { | ||
if (typeof x === "object" || typeof x === "function" || | ||
typeof x === "undefined" | ||
) { | ||
return "void" | ||
} | ||
return x.toString() | ||
@return String - a hash key | ||
*/ | ||
function defaultHasher(x) { | ||
if (typeof x === "object" || typeof x === "function" || | ||
typeof x === "undefined" | ||
) { | ||
return "void" | ||
} | ||
return x.toString() | ||
} | ||
/* | ||
memoizes asynchronous functions. The asynchronous function must have | ||
a callback as a last argument, and that callback must be called. | ||
Memoization means that the function you pass in will only be called once | ||
for every different type of argument. If the async function only | ||
has a callback argument then it will only be called once. The | ||
results of invocation are cached | ||
/* | ||
memoizes asynchronous functions. The asynchronous function must have | ||
a callback as a last argument, and that callback must be called. | ||
Memoization means that the function you pass in will only be called once | ||
for every different type of argument. If the async function only | ||
has a callback argument then it will only be called once. The | ||
results of invocation are cached | ||
@param Function fn - function to memoize | ||
@param Object context - optional context for the function | ||
@param Function hasher - optional custom hasher function. This will | ||
be called on the arguments of the memoized function. The result | ||
of the hasher will be the key the cached data will be stored under. | ||
@param Function fn - function to memoize | ||
@param Object context - optional context for the function | ||
@param Function hasher - optional custom hasher function. This will | ||
be called on the arguments of the memoized function. The result | ||
of the hasher will be the key the cached data will be stored under. | ||
@return Function - the memoized function | ||
*/ | ||
function asyncMemoize(fn, context, hasher) { | ||
var caches = callProxy.cache = {}, | ||
callbackLists = {} | ||
@return Function - the memoized function | ||
*/ | ||
function asyncMemoize(fn, context, hasher) { | ||
var caches = callProxy.cache = {}, | ||
callbackLists = {} | ||
if (typeof context === "function") { | ||
hasher = context | ||
context = null | ||
} | ||
if (typeof context === "function") { | ||
hasher = context | ||
context = null | ||
} | ||
if (typeof hasher === "undefined") { | ||
hasher = defaultHasher | ||
} | ||
if (typeof hasher === "undefined") { | ||
hasher = defaultHasher | ||
} | ||
return callProxy | ||
return callProxy | ||
function callProxy() { | ||
var args = [].slice.call(arguments), | ||
cb = args.pop(), | ||
key = hasher.apply(null, args) | ||
function callProxy() { | ||
var args = [].slice.call(arguments), | ||
cb = args.pop(), | ||
key = hasher.apply(null, args) | ||
if (caches[key]) { | ||
return typeof cb === "function" && cb.apply(null, caches[key]) | ||
} else if (callbackLists[key]) { | ||
return callbackLists[key].push(cb) | ||
} | ||
if (caches[key]) { | ||
return typeof cb === "function" && cb.apply(null, caches[key]) | ||
} else if (callbackLists[key]) { | ||
return callbackLists[key].push(cb) | ||
} | ||
callbackLists[key] = [cb] | ||
callbackLists[key] = [cb] | ||
args.push(callbackProxy) | ||
args.push(callbackProxy) | ||
fn.apply(context, args) | ||
fn.apply(context, args) | ||
function callbackProxy() { | ||
caches[key] = arguments | ||
var list = callbackLists[key] | ||
delete callbackLists[key] | ||
// it might undefined >_< if the callback is blocking | ||
list && list.forEach(function (cb) { | ||
typeof cb === "function" && cb.apply(this, caches[key]) | ||
}, this) | ||
} | ||
function callbackProxy() { | ||
caches[key] = arguments | ||
var list = callbackLists[key] | ||
delete callbackLists[key] | ||
// it might undefined >_< if the callback is blocking | ||
list && list.forEach(function (cb) { | ||
typeof cb === "function" && cb.apply(this, caches[key]) | ||
}, this) | ||
} | ||
} | ||
})(Object, [].slice) | ||
} |
{ | ||
"name": "pd", | ||
"version": "0.7.4", | ||
"description": "Manage propertyDescriptors, an OO utility", | ||
"keywords": ["oo", "oop", "propertydescriptor", "arch", "utility"], | ||
"author": "Jake Verbaten <raynos2@gmail.com>", | ||
"version": "0.8.0", | ||
"description": "an ES5 / OO utility", | ||
"keywords": ["oo", "oop", "arch", "utility", "util"], | ||
"author": "Raynos <raynos2@gmail.com>", | ||
"repository": "git://github.com/Raynos/pd.git", | ||
"main": "lib/pd", | ||
"engines": { | ||
"node": ">=0.4" | ||
}, | ||
"main": "index", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"homepage": "https://github.com/Raynos/pd", | ||
"contributors": [{ | ||
"name": "Jake Verbaten" | ||
}], | ||
"bugs": { | ||
"url" : "https://github.com/Raynos/pd/issues", | ||
"email" : "raynos2@gmail.com" | ||
}, | ||
"licenses": [{ | ||
"type": "MIT", | ||
"url": "http://github.com/raynos/pd/raw/master/LICENSE" | ||
}], | ||
"engines": { | ||
"node": ">=0.4" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "0.8.1" | ||
"mocha": "1.0.3" | ||
} | ||
} |
@@ -18,3 +18,3 @@ # <a href="#pd" name="pd">pd</a> [![Build Status][1]][2] | ||
nyan: function () { ... }, | ||
constructor: function () { | ||
initialize: function () { | ||
this.lives = 9 | ||
@@ -25,3 +25,3 @@ return this; | ||
var cat = extend({}, Cat).constructor() | ||
var cat = extend({}, Cat).initialize() | ||
@@ -43,21 +43,2 @@ ## <a href="#mov" nane="mov">Motivation</a> | ||
### <a name="pd.pd" href="#pd.pd">pd (obj)</a> | ||
pd converts all the values of your objects properties into property descriptors of those values. | ||
pd({ | ||
"foo": "bar" | ||
}) | ||
is the same as | ||
{ | ||
"foo": { | ||
"value": "bar", | ||
"enumerable": true, | ||
"writable": true, | ||
"configurable": true | ||
} | ||
} | ||
### <a name="pd.extend" href="#pd.extend">pd.extend (obj..)</a> | ||
@@ -64,0 +45,0 @@ |
@@ -12,29 +12,3 @@ var pd = require("../lib/pd.js"), | ||
test("pd", function () { | ||
var obj = { | ||
"foo": "foobar", | ||
"baz": /something/, | ||
"faz": ["one", "two", "three"], | ||
"obj": { | ||
"baz": "booz" | ||
}, | ||
get thing() { | ||
return 42 | ||
}, | ||
set thingtwo(v) { | ||
this._thingtwo = v | ||
}, | ||
get thingthree() { | ||
return 42 | ||
}, | ||
set thingthree(v) { | ||
this._thingthree = v | ||
} | ||
} | ||
var pds = pd(obj) | ||
Object.getOwnPropertyNames(pds).forEach(function (name) { | ||
assert.deepEqual(pds[name], | ||
Object.getOwnPropertyDescriptor(obj, name)) | ||
}) | ||
}) | ||
@@ -41,0 +15,0 @@ test("pd.extend", function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
1
1
24010
280
155