This repository has been archived by the owner on Dec 1, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing util.format with compat.format to fix Issue#3 and have node…
… < 5.4 compatibility
- Loading branch information
Showing
4 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//var format = require('util').format; | ||
|
||
/** | ||
* Weak version of util.format supports only %s placeholders. | ||
* Fixes Issue#3 compatibility with node < 5.4 | ||
*/ | ||
exports.format = function (fmt, args) { | ||
if (fmt.indexOf('%d') != -1 || | ||
fmt.indexOf('%j') != -1) { | ||
throw "This format only supports %s placeholders"; | ||
} | ||
if (typeof arguments[0] != 'string') { | ||
throw "First argument to format, must be a format string"; | ||
} | ||
var i = 0; | ||
var params = arguments; | ||
return fmt.replace(/%s/g, function () { | ||
i++; | ||
if (params[i] === undefined) { | ||
throw "Number of arguments didn't match format placeholders."; | ||
} | ||
return params[i]; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var compat = require('../lib/compat'), | ||
format = compat.format, | ||
nodeunit = require('nodeunit'); | ||
|
||
exports.format = nodeunit.testCase({ | ||
"typical format": function (test) { | ||
test.equal(format("/%s%s", "foo", "/bar"), "/foo/bar"); | ||
test.equal(format("%s/%s", "foo", "bar"), "foo/bar"); | ||
test.equal(format("%s", "foo"), "foo"); | ||
test.equal(format("ID#%s", 32432), "ID#32432"); | ||
test.equal(format("foo"), "foo"); | ||
test.done(); | ||
}, | ||
"programmer error": function (test) { | ||
test.throws(function () { | ||
format(1, "foo", "/bar"); | ||
}); | ||
test.throws(function () { | ||
format("%d/%d", 1, 2); | ||
}); | ||
test.throws(function () { | ||
format("%s/%s"); | ||
}); | ||
test.done(); | ||
} | ||
}); |