Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added truthy check for boolean values #28

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
37 changes: 24 additions & 13 deletions src/cache-control-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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({
Expand All @@ -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,
Expand All @@ -51,48 +51,48 @@ 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"
)
).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,
Expand All @@ -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,
Expand All @@ -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");
});
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/cache-control-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
Loading