/
parseNetscape.ts
36 lines (33 loc) · 1.01 KB
/
parseNetscape.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { pipe, ifElse, split, map, filter } from 'ramda';
import { isNotNilOrEmpty, lengthEq, stubNull } from 'ramda-adjunct';
import { ParsedCookie } from './interfaces';
import { arrayToCookie, splitString } from './utils';
/**
* Reduces a string like:
*
* ```
* .test.dev TRUE / FALSE 1629107116 key1 value1
* ```
*
* to a `ParsedCookie`
*/
export function parseNetscapeString(string: string): ParsedCookie {
return pipe(splitString('\t'), ifElse(lengthEq(7), arrayToCookie, stubNull))(string);
}
/**
* Reduces a file like:
*
* ```
* # Netscape HTTP Cookie File
* # https://curl.haxx.se/rfc/cookie_spec.html
* # This is a generated file! Do not edit.
* .test.dev TRUE / FALSE 1629107116 key1 value1
* .test.dev TRUE / FALSE 1629107116 key2 value2
* .test.dev TRUE / FALSE 1629107116 key3 value3
* ```
*
* to a `ParsedCookie` array
*/
export function parseNetscapeFile(buffer: Buffer): ParsedCookie[] {
return pipe(() => buffer.toString(), split('\n'), map(parseNetscapeString), filter(isNotNilOrEmpty))();
}