remove common/util (no longer needed)

This commit is contained in:
Jeff Williams 2012-12-23 10:24:49 -08:00
parent d79d930a1b
commit 3932bc8f5f
5 changed files with 22 additions and 221 deletions

View File

@ -1,137 +0,0 @@
/**
@module common/util
*/
var hasOwnProp = Object.prototype.hasOwnProperty;
exports.print = function() {
for (var i = 0, len = arguments.length; i < len; ++i) {
java.lang.System.out.print(String(arguments[i]));
}
};
exports.puts = function() {
for (var i = 0, len = arguments.length; i < len; ++i) {
java.lang.System.out.println(arguments[i] + '\n');
}
};
exports.debug = function(x) {
exports.puts('DEBUG: ' + x + '\n');
};
var error = exports.error = function(x) {
for (var i = 0, len = arguments.length; i < len; ++i) {
exports.puts(arguments[i] + '\n');
}
};
exports.format = {
stylize: function(str, styleType) {
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
var styles =
{ 'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'inverse' : [7, 27],
'white' : [37, 39],
'grey' : [90, 39],
'black' : [30, 39],
'blue' : [34, 39],
'cyan' : [36, 39],
'green' : [32, 39],
'magenta' : [35, 39],
'red' : [31, 39],
'yellow' : [33, 39] };
var style =
{ 'special': 'cyan',
'number': 'blue',
'boolean': 'yellow',
'undefined': 'grey',
'null': 'bold',
'string': 'green',
'date': 'magenta',
// "name": intentionally not styling
'regexp': 'red' }[styleType];
if (style) {
return '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm';
} else {
return str;
}
}
};
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
/**
* Create a timestamp string.
* @returns {string} Like 26 Feb 2011 16:19:34
*/
exports.timestamp = function() {
var d = new Date();
var time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
return [d.getDate(), months[d.getMonth()], d.getFullYear(), time].join(' ');
};
exports.log = function(msg) {
exports.puts(exports.timestamp() + ' - ' + msg.toString());
};
/**
* Inherit the prototype methods from one constructor into another.
* @param {function} ctor Constructor function which needs to inherit the prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
*/
exports.inherits = function(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: { value: ctor, enumerable: false }
});
};
/**
* Mix in the members of a source object over a target object.
* @param {object} target
* @param {object} source
*/
exports.mixin = function(target, source /*...*/){
var sourceProperty;
for (var i = 1, len = arguments.length; i < len; i++) {
source = arguments[i];
for (sourceProperty in source) {
if ( hasOwnProp.call(source, sourceProperty) ) {
target[sourceProperty] = source[sourceProperty]; // overwrites target property
}
}
}
return target;
};
exports.mergeRecurse = function mergeRecurse(target, source) {
for (var p in source) {
if ( hasOwnProp.call(source, p) ) {
if ( source[p].constructor === Object ) {
if ( !target[p] ) { target[p] = {}; }
mergeRecurse(target[p], source[p]);
}
else {
target[p] = source[p];
}
}
}
return target;
};

View File

@ -6,11 +6,25 @@
/**
@module jsdoc/config
@requires common/util
*/
// used to do recursive merge
var util = require('common/util');
var hasOwnProp = Object.prototype.hasOwnProperty;
function mergeRecurse(target, source) {
for (var p in source) {
if ( hasOwnProp.call(source, p) ) {
if ( source[p].constructor === Object ) {
if ( !target[p] ) { target[p] = {}; }
mergeRecurse(target[p], source[p]);
}
else {
target[p] = source[p];
}
}
}
return target;
}
// required config values, override these defaults in your config.json if necessary
const defaults = {
@ -36,7 +50,7 @@ const defaults = {
*/
function Config(json) {
json = JSON.parse( (json || "{}") );
this._config = util.mergeRecurse(defaults, json);
this._config = mergeRecurse(defaults, json);
}
module.exports = Config;

View File

@ -4,6 +4,9 @@
@author Michael Mathews <micmath@gmail.com>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/
var _ = require('underscore');
var hasOwnProp = Object.prototype.hasOwnProperty;
/**
@ -102,8 +105,7 @@ ArgParser.prototype.help = function() {
provided, or `true` if the option accepts no value.
*/
ArgParser.prototype.parse = function(args, defaults) {
var util = require('common/util'),
result = defaults && util.mixin({}, defaults) || {};
var result = defaults && _.defaults({}, defaults) || {};
result._ = [];
for (var i = 0, leni = args.length; i < leni; i++) {

View File

@ -1,7 +1,6 @@
/*global Packages: true */
/**
* @module jsdoc/src/parser
* @requires common/util
* @requires fs
* @requires events
*/

View File

@ -1,77 +0,0 @@
describe("common/util", function() {
var common = {util: require('common/util')};
it('should exist', function() {
expect(common.util).toBeDefined();
expect(typeof common.util).toEqual("object");
});
it('should export a "inherits" function.', function() {
expect(common.util.inherits).toBeDefined();
expect(typeof common.util.inherits).toEqual("function");
});
it('should export a "mixin" function.', function() {
expect(common.util.mixin).toBeDefined();
expect(typeof common.util.mixin).toEqual("function");
});
describe("common/util.mixin", function() {
it('should take a target object and return it.', function() {
var target = {a:1},
returned;
returned = common.util.mixin(target); // mixing nothing in
expect(returned).toEqual(target);
});
it('it should mix a source object into the target.', function() {
var target = {a: 1, b: 2},
source = {c: 3};
common.util.mixin(target, source); // modify the target object
expect(target).toEqual({a: 1, b: 2, c: 3});
});
describe("overwriting properties in the target", function() {
var target = {a: 1, b: 2},
source = {b: 3, c: 4};
common.util.mixin(target, source);
it("should leave properties in the target with unique keys alone", function() {
expect(target.a).toEqual(1);
});
it ('should overwrite existing properties in the target with same-named keys', function() {
expect(target.b).toEqual(source.b);
});
it ('should add properties in the source with unique keys to the target', function() {
expect(target.c).toEqual(source.c);
});
});
describe("mixing several objects into the target", function() {
var target = {},
source1 = {a: 1, b: 2},
source2 = {b: 7, c: 4},
source3 = {b: 3, d: 5},
returned;
returned = common.util.mixin(target, source1, source2, source3); // use a dummy target and the return value to avoid modifying the real target (source1)
it ('should not modify the source objects being mixed in', function() {
expect(source1).toEqual({a: 1, b: 2});
});
it ('should return an object with the properties of all the sources', function() {
expect(returned).toEqual({a: 1, b: 3, c: 4, d: 5});
});
});
});
});