Skip to content

Commit

Permalink
Minor namespace lib fix. Added JSHint globals comments to gruntfiles.…
Browse files Browse the repository at this point in the history
… Files now display with path when reading/writing.
  • Loading branch information
cowboy committed Oct 19, 2011
1 parent c082f6a commit 34a9279
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion grunt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Config.
/*global config:true, task:true*/
config.init({
meta: {
name: 'grunt',
Expand Down
17 changes: 7 additions & 10 deletions lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ exports.mkdir = function(dirpath) {

// Write a file.
exports.write = function(filepath, contents) {
var basename = path.basename(filepath);
var nowrite = option('no-write');
verbose.write((nowrite ? 'Not actually writing ' : 'Writing ') + basename + '...');
verbose.write((nowrite ? 'Not actually writing ' : 'Writing ') + filepath + '...');
try {
if (!nowrite) {
// Create path, if necessary.
Expand All @@ -58,37 +57,35 @@ exports.write = function(filepath, contents) {
return true;
} catch(e) {
verbose.error();
throw {name: 'TaskError', message: 'Unable to write "' + basename + '" file (Error code: ' + e.code + ').'};
throw {name: 'TaskError', message: 'Unable to write "' + filepath + '" file (Error code: ' + e.code + ').'};
}
};

// Read a file, return its contents.
exports.read = function(filepath) {
var basename = path.basename(filepath);
var src;
verbose.write('Reading ' + basename + '...');
verbose.write('Reading ' + filepath + '...');
try {
src = fs.readFileSync(filepath, 'UTF-8');
verbose.ok();
return src;
} catch(e) {
verbose.error();
throw {name: 'TaskError', message: 'Unable to read "' + basename + '" file (Error code: ' + e.code + ').'};
throw {name: 'TaskError', message: 'Unable to read "' + filepath + '" file (Error code: ' + e.code + ').'};
}
};

// Read a file, parse its contents, return an object.
exports.readJson = function(filepath, requiredMessage) {
var basename = path.basename(filepath);
exports.readJson = function(filepath) {
var src = this.read(filepath);
var result;
verbose.write('Parsing ' + basename + '...');
verbose.write('Parsing ' + filepath + '...');
try {
result = JSON.parse(src);
verbose.ok();
return result;
} catch(e) {
verbose.error();
throw {name: 'TaskError', message: 'Unable to parse "' + basename + '" file (' + e.message + ').'};
throw {name: 'TaskError', message: 'Unable to parse "' + filepath + '" file (' + e.message + ').'};
}
};
2 changes: 1 addition & 1 deletion lib/util/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// on dot, then replace the placeholder character with a dot.
function getParts(str) {
return str.replace(/\\\./g, '\uffff').split('.').map(function(s) {
return s.replace('\uffff', '.');
return s.replace(/\uffff/g, '.');
});
}

Expand Down
2 changes: 1 addition & 1 deletion template/grunt_jquery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Config.
/*global config:true, task:true*/
config.init({
meta: {
name: 'project_name',
Expand Down
2 changes: 1 addition & 1 deletion template/grunt_node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Config.
/*global config:true, task:true*/
config.init({
meta: {
name: 'project_name',
Expand Down
12 changes: 6 additions & 6 deletions test/util/namespace_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ var namespace = require('../../lib/util/namespace.js');

exports.get = {
'no create': function(test) {
var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h': 2}}};
var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h.i': 2}}};
test.strictEqual(namespace.get(obj, 'a'), obj.a, 'should get immediate properties.');
test.strictEqual(namespace.get(obj, 'a.b'), obj.a.b, 'should get nested properties.');
test.strictEqual(namespace.get(obj, 'a.x'), undefined, 'should return undefined for nonexistent properties.');
test.strictEqual(namespace.get(obj, 'a.b.c'), 1, 'should return values.');
test.strictEqual(namespace.get(obj, 'a.b.d'), '', 'should return values.');
test.strictEqual(namespace.get(obj, 'a.b.e'), null, 'should return values.');
test.strictEqual(namespace.get(obj, 'a.b.f'), undefined, 'should return values.');
test.strictEqual(namespace.get(obj, 'a.b.g\\.h'), 2, 'literal backslash should escape period in property name.');
test.strictEqual(namespace.get(obj, 'a.b.g\\.h\\.i'), 2, 'literal backslash should escape period in property name.');
test.done();
},
'create': function(test) {
Expand All @@ -28,20 +28,20 @@ exports.set = function(test) {
test.strictEqual(obj.a, 1, 'should set property value.');
test.strictEqual(namespace.set(obj, 'b.c.d', 1), 1, 'should return nested property value.');
test.strictEqual(obj.b.c.d, 1, 'should set property value.');
test.strictEqual(namespace.set(obj, 'e\\.f', 1), 1, 'literal backslash should escape period in property name.');
test.strictEqual(obj['e.f'], 1, 'should set property value.');
test.strictEqual(namespace.set(obj, 'e\\.f\\.g', 1), 1, 'literal backslash should escape period in property name.');
test.strictEqual(obj['e.f.g'], 1, 'should set property value.');
test.done();
};

exports.exists = function(test) {
var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h': 2}}};
var obj = {a: {b: {c: 1, d: '', e: null, f: undefined, 'g.h.i': 2}}};
test.ok(namespace.exists(obj, 'a'), 'immediate property should exist.');
test.ok(namespace.exists(obj, 'a.b'), 'nested property should exist.');
test.ok(namespace.exists(obj, 'a.b.c'), 'nested property should exist.');
test.ok(namespace.exists(obj, 'a.b.d'), 'nested property should exist.');
test.ok(namespace.exists(obj, 'a.b.e'), 'nested property should exist.');
test.ok(namespace.exists(obj, 'a.b.f'), 'nested property should exist.');
test.ok(namespace.exists(obj, 'a.b.g\\.h'), 'literal backslash should escape period in property name.');
test.ok(namespace.exists(obj, 'a.b.g\\.h\\.i'), 'literal backslash should escape period in property name.');
test.equal(namespace.exists(obj, 'x'), false, 'nonexistent property should not exist.');
test.equal(namespace.exists(obj, 'a.x'), false, 'nonexistent property should not exist.');
test.equal(namespace.exists(obj, 'a.b.x'), false, 'nonexistent property should not exist.');
Expand Down

0 comments on commit 34a9279

Please sign in to comment.