Skip to content

Commit

Permalink
Add serializeCookieString
Browse files Browse the repository at this point in the history
  • Loading branch information
squaremarco committed Jan 21, 2021
1 parent 6294f87 commit 0d36ba8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ npm run build
## TODO list

- [x] ~~Documentation~~.
- [ ] `ParsedCookie` serialization.
- [ ] Check edge cases.
- [ ] Add examples.
- [ ] Migrate tests to typescript when I resolve some issues with typescript and custom jest matchers.
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { ParsedCookie, SplitStringOptions } from './interfaces';
export { parseNetscapeFile, parseNetscapeString } from './parseNetscape';
export { parseCookieString } from './parseCookie';
export { ParsedCookie, SplitStringOptions } from './interfaces';
export { serializeCookieString as serializeToCookieString } from './serializeCookie';
4 changes: 2 additions & 2 deletions src/parseCookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ParsedCookie } from './interfaces';
import { nameValuePairToCookie, splitString } from './utils';

/**
* Reduces a semicolon separated key=value sequence like:
* Deserializes a semicolon separated key=value sequence like:
*
* ```
* 'a=b; c=d; e=f;'
Expand All @@ -18,7 +18,7 @@ export function parseCookieString(string: string): ParsedCookie[] {
}

/**
* Reduces a set-cookie header string like:
* Deserializes a set-cookie header string like:
*
* ```
* 'a=b; Domain=test.dev; Path=/; Secure; SameSite=Lax; Expires=Wed, 09 Jun 2021 10:18:14 GMT;'
Expand Down
16 changes: 16 additions & 0 deletions src/serializeCookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { join, map, pipe } from 'ramda';
import { isArray } from 'ramda-adjunct';

import { ParsedCookie } from './interfaces';
import { cookieToNameValuePair } from './utils';

/**
* Serializes a single or a `ParsedCookie` array to a separated key=value sequence like:
*
* ```
* 'a=b; c=d; e=f;'
* ```
*/
export function serializeCookieString(cookie: ParsedCookie | ParsedCookie[]): string {
return isArray(cookie) ? pipe(map(cookieToNameValuePair), join(';'))(cookie) : cookieToNameValuePair(cookie);
}
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export function nameValuePairToCookie(tuple: NameValueTuple): ParsedCookie {
return { name, value: ifElse(isNotNil, identity, always(name))(value) } as ParsedCookie;
}

/**
* Takes a ParsedCookie and maps it to a valid [name, value] pair
* @internal
*/
export function cookieToNameValuePair(cookie: ParsedCookie): string {
return !cookie.value || cookie.name === cookie.value ? cookie.name : `${cookie.name}=${cookie.value}`;
}

/**
* Takes a split netscape string and maps it to a valid Cookie
* @internal
Expand Down
17 changes: 17 additions & 0 deletions tests/serializeCookie.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { serializeToCookieString } from '../src';

describe('Parse Cookie String', () => {
const parsedCookies = [
{ name: 'a', value: 'b' },
{ name: 'c', value: 'd' }
];

it('Should generate a valid `name=value` from ParsedCookie', () => {
expect(serializeToCookieString(parsedCookies[0])).toBe('a=b');
expect(serializeToCookieString(parsedCookies[1])).toBe('c=d');
});

it('Should generate a valid `name=value` pairs from a ParsedCookie array', () => {
expect(serializeToCookieString(parsedCookies)).toBe('a=b;c=d');
});
});
13 changes: 12 additions & 1 deletion tests/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { splitString, nameValuePairToCookie, arrayToCookie } from '../src/utils';
import { splitString, nameValuePairToCookie, arrayToCookie, cookieToNameValuePair } from '../src/utils';

describe('splitTrimLine', () => {
it('Should generate a valid array from a string without extra whitespace', () => {
Expand Down Expand Up @@ -79,3 +79,14 @@ describe('nameValuePairToCookie', () => {
expect(nameValuePairToCookie(['a'])).toStrictEqual({ name: 'a', value: 'a' });
});
});

describe('cookieToNameValuePair', () => {
it('Should generate a valid name=value pair from a ParsedCookie', () => {
expect(cookieToNameValuePair({ name: 'a', value: 'b' })).toStrictEqual('a=b');
});

it('Should generate a name-only cookie from a ParsedCookie', () => {
expect(cookieToNameValuePair({ name: 'a', value: 'a' })).toStrictEqual('a');
expect(cookieToNameValuePair({ name: 'a' })).toStrictEqual('a');
});
});

0 comments on commit 0d36ba8

Please sign in to comment.