Comparing version 0.1.0 to 0.2.0
@@ -0,1 +1,7 @@ | ||
0.2.0 / 2014-06-15 | ||
================== | ||
* Add `deprecate.property(obj, prop, message)` | ||
* Remove `supports-color` dependency for node.js 0.8 | ||
0.1.0 / 2014-06-15 | ||
@@ -2,0 +8,0 @@ ================== |
83
index.js
@@ -13,3 +13,2 @@ /*! | ||
var relative = require('path').relative | ||
var supportsColor = require('supports-color') | ||
@@ -37,2 +36,24 @@ /** | ||
/** | ||
* Convert a data descriptor to accessor descriptor. | ||
*/ | ||
function convertDataDescriptorToAccessor(obj, prop, message) { | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, prop) | ||
var value = descriptor.value | ||
descriptor.get = function getter() { return value } | ||
if (descriptor.writable) { | ||
descriptor.set = function setter(val) { return value = val } | ||
} | ||
delete descriptor.value | ||
delete descriptor.writable | ||
Object.defineProperty(obj, prop, descriptor) | ||
return descriptor | ||
} | ||
/** | ||
* Create arguments string to keep arity. | ||
@@ -92,2 +113,3 @@ */ | ||
deprecate.function = wrapfunction | ||
deprecate.property = wrapproperty | ||
@@ -158,3 +180,3 @@ return deprecate | ||
// format and write message | ||
var format = supportsColor && process.stderr.isTTY | ||
var format = process.stderr.isTTY | ||
? formatColor | ||
@@ -282,2 +304,6 @@ : formatPlain | ||
function wrapfunction(fn, message) { | ||
if (typeof fn !== 'function') { | ||
throw new TypeError('argument fn must be a function') | ||
} | ||
var args = createArgumentsString(fn.length) | ||
@@ -299,2 +325,55 @@ var deprecate = this | ||
/** | ||
* Wrap property in a deprecation message. | ||
*/ | ||
function wrapproperty(obj, prop, message) { | ||
if (!obj || typeof obj !== 'object') { | ||
throw new TypeError('argument obj must be object') | ||
} | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, prop) | ||
if (!descriptor) { | ||
throw new TypeError('must call property on owner object') | ||
} | ||
if (!descriptor.configurable) { | ||
throw new TypeError('property must be configurable') | ||
} | ||
var deprecate = this | ||
var stack = getStack() | ||
var site = callSiteLocation(stack[1]) | ||
// set site name | ||
site.name = prop | ||
// convert data descriptor | ||
if ('value' in descriptor) { | ||
descriptor = convertDataDescriptorToAccessor(obj, prop, message) | ||
} | ||
var get = descriptor.get | ||
var set = descriptor.set | ||
// wrap getter | ||
if (typeof get === 'function') { | ||
descriptor.get = function getter() { | ||
log.call(deprecate, message, site) | ||
return get.apply(this, arguments) | ||
} | ||
} | ||
// wrap setter | ||
if (typeof set === 'function') { | ||
descriptor.set = function setter() { | ||
log.call(deprecate, message, site) | ||
return set.apply(this, arguments) | ||
} | ||
} | ||
Object.defineProperty(obj, prop, descriptor) | ||
} | ||
/** | ||
* Create DeprecationError for deprecation | ||
@@ -301,0 +380,0 @@ */ |
{ | ||
"name": "depd", | ||
"description": "Deprecate all the things", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
@@ -12,5 +12,2 @@ "license": "MIT", | ||
"repository": "dougwilson/nodejs-depd", | ||
"dependencies": { | ||
"supports-color": "0.2.0" | ||
}, | ||
"devDependencies": { | ||
@@ -17,0 +14,0 @@ "istanbul": "0.2.10", |
@@ -63,2 +63,14 @@ # depd | ||
### deprecate.property(obj, prop, message) | ||
Call this function to wrap a given property on object in a deprecation message | ||
on any accessing or setting of the property. An optional message can be supplied | ||
to provide a custom message. | ||
The method must be called on the object where the property belongs (not | ||
inherited from the prototype). | ||
If the property is a data descriptor, it will be converted to an accessor | ||
descriptor in order to display the deprecation message. | ||
### process.on('deprecation', fn) | ||
@@ -188,17 +200,9 @@ | ||
;(function () { | ||
var value = 'something' | ||
Object.defineProperty(exports, 'oldprop', { | ||
configurable: true, | ||
enumerable: true, | ||
get: function () { | ||
deprecate('get oldprop') | ||
return value | ||
}, | ||
set: function (val) { | ||
deprecate('set oldprop') | ||
return value = val | ||
} | ||
}) | ||
}()) | ||
exports.oldprop = 'something' | ||
// message automatically derives from property name | ||
deprecate.property(exports, 'oldprop') | ||
// explicit message | ||
deprecate.property(exports, 'oldprop', 'oldprop >= 0.10') | ||
``` | ||
@@ -205,0 +209,0 @@ |
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
18906
0
323
231
- Removedsupports-color@0.2.0
- Removedsupports-color@0.2.0(transitive)