diff --git a/package.json b/package.json index 3848008..5cf5477 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cache-control-parser", - "version": "2.0.5", + "version": "2.0.6", "description": "A humble cache-control parser", "homepage": "https://github.com/etienne-martin/cache-control-parser", "keywords": [ diff --git a/src/cache-control-parser.test.ts b/src/cache-control-parser.test.ts index 8bf40df..004dac0 100644 --- a/src/cache-control-parser.test.ts +++ b/src/cache-control-parser.test.ts @@ -6,11 +6,11 @@ import { const testSuite = (parser: typeof parse, stringifier: typeof stringify) => { describe("parse", () => { - test("should parse empty cache-control header", () => { + it("should parse empty cache-control header", () => { expect(parser("")).toEqual({}); }); - test("should parse both boolean and numeric directives", () => { + it("should parse both boolean and numeric directives", () => { expect( parser( "max-age=1, s-maxage=2, stale-while-revalidate=3, stale-if-error=4, public, private, no-store, no-cache, must-revalidate, proxy-revalidate, immutable, no-transform" @@ -31,7 +31,7 @@ const testSuite = (parser: typeof parse, stringifier: typeof stringify) => { }); }); - test("should trim directives", () => { + it("should trim directives", () => { expect( parser(" max-age = 60 , s-maxage = 3600 , public ") ).toEqual({ @@ -41,7 +41,7 @@ const testSuite = (parser: typeof parse, stringifier: typeof stringify) => { }); }); - test("should be case-insensitive", () => { + it("should be case-insensitive", () => { expect(parser("Max-Age=1, S-MAXAGE=2, Stale-While-Revalidate=3")).toEqual( { "max-age": 1, @@ -51,20 +51,20 @@ const testSuite = (parser: typeof parse, stringifier: typeof stringify) => { ); }); - test("should support directives that are not separated by spaces", () => { + it("should support directives that are not separated by spaces", () => { expect(parser("max-age=60,public")).toEqual({ "max-age": 60, public: true }); }); - test("should override previously declared directives", () => { + it("should override previously declared directives", () => { expect(parser("max-age=1, max-age=2")).toEqual({ "max-age": 2 }); }); - test("should ignore invalid directives", () => { + it("should ignore invalid directives", () => { expect( parser( "max-age=NaN, s-maxage=NaN, stale-while-revalidate=NaN, stale-if-error=NaN" @@ -72,27 +72,27 @@ const testSuite = (parser: typeof parse, stringifier: typeof stringify) => { ).toEqual({}); }); - test("should not override previously declared directives if the directive is invalid", () => { + it("should not override previously declared directives if the directive is invalid", () => { expect(parser("max-age=1, max-age=NaN")).toEqual({ "max-age": 1 }); }); - test("should ignore unknown directives", () => { + it("should ignore unknown directives", () => { expect(parser("unknown-directive")).toEqual({}); }); - test("should ignore unknown directives", () => { + it("should ignore unknown directives", () => { expect(parser("unknown-directive=value")).toEqual({}); }); }); describe("stringify", () => { - test("should stringify empty cache control", () => { + it("should stringify empty cache control", () => { expect(stringifier({})).toEqual(""); }); - test("should stringify both boolean and numeric directives", () => { + it("should stringify both boolean and numeric directives", () => { expect( stringifier({ "max-age": 1, @@ -113,7 +113,7 @@ const testSuite = (parser: typeof parse, stringifier: typeof stringify) => { ); }); - test("should leave out unsupported directives", () => { + it("should leave out unsupported directives", () => { expect( stringifier({ "max-age": 1, @@ -122,6 +122,17 @@ const testSuite = (parser: typeof parse, stringifier: typeof stringify) => { } as CacheControl) ).toEqual("max-age=1"); }); + + it("should not include falsy booleans", () => { + expect( + stringifier({ + "max-age": 1, + public: false, + private: false, + immutable: false + } as CacheControl) + ).toEqual("max-age=1"); + }); }); }; diff --git a/src/cache-control-parser.ts b/src/cache-control-parser.ts index 14c5602..20e2d3a 100644 --- a/src/cache-control-parser.ts +++ b/src/cache-control-parser.ts @@ -97,7 +97,7 @@ export const stringify = (cacheControl: CacheControl) => { switch (typeof value) { case "boolean": - directives.push(`${key}`); + value && directives.push(`${key}`); break; case "number": directives.push(`${key}=${value}`);