header-field
Advanced tools
Comparing version 0.1.2 to 0.2.0
28
index.js
@@ -1,31 +0,11 @@ | ||
var hyphenReg = /-[a-z]/g; | ||
module.exports = field; | ||
/** | ||
* Converts hyphenated headers like `content-type` into `Content-Type`. | ||
* Converts hyphenated headers like `Content-Type` into `content-type`. | ||
*/ | ||
function field (str) { | ||
if (!str) return ""; | ||
var result = String(str); | ||
result = ( | ||
toUpperCase(str[0]) + | ||
str | ||
.slice(1) | ||
.toLowerCase() | ||
.replace(hyphenReg, toUpperCase) | ||
); | ||
// Special case to fix the broken http spec. | ||
if (result === "Referrer") result = "Referer"; | ||
return result; | ||
} | ||
field.lowerCase = function fieldLowerCase (str) { | ||
str = String(str).toLowerCase(); | ||
if (typeof str !== "string") throw new TypeError("Header Fields must be strings."); | ||
str = str.toLowerCase(); | ||
if (str === "referrer") str = "referer"; | ||
return str; | ||
}; | ||
/** | ||
* Simple utility to uppercase a string. | ||
*/ | ||
function toUpperCase (m) { return m.toUpperCase(); } | ||
}; |
{ | ||
"name": "header-field", | ||
"version": "0.1.2", | ||
"description": "Properly format a header field into a capitalized hyphenated string.", | ||
"version": "0.2.0", | ||
"description": "Properly format a header field into a lowercased hyphenated string.", | ||
"author": "Dylan Piercey <pierceydylan@gmail.com>", | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
# Header-Field | ||
Transform header fields into a properly formatted string, with uppercase characters and hyphens. | ||
Transform header fields into a properly formatted string, with lowercase characters and hyphens. | ||
@@ -16,11 +16,8 @@ # Installation | ||
field("content-type"); //-> "Content-Type" | ||
field("content-Length"); //-> "Content-Length" | ||
field("Content-Disposition"); //-> "Content-Disposition" | ||
field("content-type"); //-> "content-type" | ||
field("content-Length"); //-> "content-length" | ||
field("Content-Disposition"); //-> "content-disposition" | ||
// Special case: referrer -> referer. | ||
field("referrer"); //-> "Referer" | ||
// For lowercased headers. | ||
field.lowerCase("Referrer"); //-> "referer" | ||
field("Referrer"); //-> "referer" | ||
``` | ||
@@ -27,0 +24,0 @@ |
@@ -6,9 +6,10 @@ var assert = require("assert"); | ||
it("should work", function () { | ||
assert.equal(field("content-type"), "Content-Type"); | ||
assert.equal(field("content-Length"), "Content-Length"); | ||
assert.equal(field("Content-Disposition"), "Content-Disposition"); | ||
assert.equal(field("Content-DisPosition"), "Content-Disposition"); | ||
assert.equal(field("referrer"), "Referer"); | ||
assert.equal(field.lowerCase("Referrer"), "referer"); | ||
assert.equal(field("content-type"), "content-type"); | ||
assert.equal(field("content-Length"), "content-length"); | ||
assert.equal(field("Content-Disposition"), "content-disposition"); | ||
assert.equal(field("Content-DisPosition"), "content-disposition"); | ||
assert.equal(field("referrer"), "referer"); | ||
assert.equal(field("Referrer"), "referer"); | ||
assert.throws(function () { field({}); }) | ||
}); | ||
}); |
2304
33
29