Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit d0b32e237d0bee268522c1ac100e0b3d61830115 @brianc committed Mar 7, 2013
Showing with 162 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +48 −0 README.md
  3. +28 −0 index.js
  4. +21 −0 package.json
  5. +64 −0 test/index.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,48 @@
+# deprecate
+
+Mark a method as deprecated. Write a message to a stream the first time the deprecated method is called.
+
+## api
+
+`var deprecate = require('deprecate');`
+
+### deprecate([string message1 [, string message2 [,...]]])
+
+Call `deprecate` within a function you are deprecating. It will spit out all the messages to the console the first time _and only the first time_ the method is called.
+
+```js
+var deprecate = require('deprecate');
+
+var someDeprecatedFunction = function() {
+ deprecate('someDeprecatedFunction() is deprecated');
+};
+
+someDeprecatedFunction();
+someDeprecatedFunction();
+someDeprecatedFunction();
+console.log('end');
+
+//program output:
+
+WARNING!!
+someDeprecatedFunction() is deprecated
+
+
+end
+```
+
+### deprecate.color
+
+Set to `false` to not output a color. Defaults to `'\x1b[31;1m'` which is red.
+
+### deprecate.silence
+
+Do nothing at all when the deprecate method is called.
+
+### deprecate.stream
+
+The to which output is written. Defaults to `process.stderr`
+
+## license
+
+MIT
@@ -0,0 +1,28 @@
+var os = require('os');
+
+var hits = {
+};
+var deprecate = module.exports = function(methodName, message) {
+ if(deprecate.silence) return;
+ if(hits[deprecate.caller]) return;
+ hits[deprecate.caller] = true;
+ deprecate.stream.write(os.EOL);
+ if(deprecate.color) {
+ deprecate.stream.write(deprecate.color);
+ }
+ deprecate.stream.write('WARNING!!');
+ deprecate.stream.write(os.EOL);
+ for(var i = 0; i < arguments.length; i++) {
+ deprecate.stream.write(arguments[i]);
+ deprecate.stream.write(os.EOL);
+ }
+ if(deprecate.color) {
+ deprecate.stream.write('\x1b[0m');
+ }
+ deprecate.stream.write(os.EOL);
+ deprecate.stream.write(os.EOL);
+};
+
+deprecate.stream = process.stderr;
+deprecate.silence = false;
+deprecate.color = '\x1b[31;1m';
@@ -0,0 +1,21 @@
+{
+ "name": "deprecate",
+ "version": "0.1.0",
+ "description": "Mark methods as deprecated and warn the user when they're called",
+ "main": "index.js",
+ "scripts": {
+ "test": "mocha"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/brianc/node-deprecate.git"
+ },
+ "keywords": [
+ "deprecate"
+ ],
+ "author": "Brian M. Carlson",
+ "license": "MIT",
+ "devDependencies": {
+ "mocha": "~1.8.1"
+ }
+}
@@ -0,0 +1,64 @@
+var assert = require('assert');
+
+var deprecate = require(__dirname + '/../');
+
+var output = {
+ _text: [],
+ _clear: function() {
+ this._text = [];
+ },
+ write: function(message) {
+ this._text.push(message);
+ }
+}
+
+describe('deprecate', function() {
+ beforeEach(function() {
+ output._clear();
+ deprecate.stream = output;
+ });
+
+ it('does nothing if silence is turned on', function() {
+ deprecate.silence = true;
+ deprecate('this method is deprecated and will be removed');
+ assert.equal(output._text.length, 0);
+ });
+
+ it('prints to output if silence is turned off', function() {
+ deprecate.silence = false;
+ deprecate('line1', 'line2', 'line3');
+ var text = output._text.join(' ');
+ assert(text.indexOf('WARNING') > 0, 'should have contained the string "warning"');
+ assert(text.indexOf('line1') > 0, 'should have contained the string "line1"');
+ assert(text.indexOf('line2') > 0, 'should have contained the string "line2"');
+ assert(text.indexOf('line2') > text.indexOf('line1'), 'line 2 should come after line 1');
+ assert(text.indexOf(deprecate.color) > 0, 'should have color');
+ });
+
+
+ it('does not print color if color turned off', function() {
+ deprecate.color = false;
+ deprecate('test');
+ var text = output._text.join(' ');
+ assert.equal(text.indexOf(deprecate.color), -1, 'should not have color string');
+ assert.equal(text.indexOf('\x1b[0m'), -1, 'should not have reset color char ');
+ });
+
+ it('only prints once for each function deprecated', function() {
+ var someDeprecatedMethod = function() {
+ deprecate('first');
+ }
+ var someOtherDeprecatedMethod = function() {
+ deprecate('second');
+ }
+ assert.equal(output._text.length, 0);
+ someDeprecatedMethod();
+ var length = output._text.length;
+ assert(length > 0, "should have printed deprecation warning");
+ someDeprecatedMethod();
+ assert.equal(length, output._text.length, "should not have warned again");
+ someOtherDeprecatedMethod();
+ assert(output._text.length > length);
+ });
+
+});

0 comments on commit d0b32e2

Please sign in to comment.