@yurkimus/cookies 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/deno.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@yurkimus/cookies",
3
+ "version": "0.0.0",
4
+ "fmt": {
5
+ "semiColons": false,
6
+ "singleQuote": true
7
+ },
8
+ "exports": "./source/index.js",
9
+ "imports": {
10
+ "@yurkimus/curry": "https://raw.githubusercontent.com/yurkimus/curry/main/source/index.js"
11
+ }
12
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "type": "module",
3
+ "version": "0.0.0",
4
+ "name": "@yurkimus/cookies",
5
+ "author": {
6
+ "name": "yurkimus",
7
+ "email": "yurkimus@gmail.com"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/yurkimus/cookies.git"
12
+ },
13
+ "exports": "./source/index.js",
14
+ "dependencies": {
15
+ "@yurkimus/types": "@yurkimus/types"
16
+ }
17
+ }
package/readme.md ADDED
@@ -0,0 +1,59 @@
1
+ # Cookies
2
+
3
+ A collection of utilities for handling cookies in JavaScript.
4
+
5
+ ## parseCookie
6
+
7
+ Parses a cookie string into a list of key-value pairs.
8
+
9
+ | Parameter | Type | Description |
10
+ | --------- | -------- | ------------- |
11
+ | cookie | `string` | Cookie string |
12
+
13
+ **Returns**: `[string, string][]`
14
+
15
+ **Example**:
16
+
17
+ ```javascript
18
+ parseCookie('name=John; age=30; role=admin') // => [['name', 'John'], ['age', '30'], ['role', 'admin']]
19
+ ```
20
+
21
+ ## readCookie
22
+
23
+ Reads cookies from a cookie string using a provided list of keys.
24
+
25
+ | Parameter | Type | Description |
26
+ | --------- | ---------------------- | ---------------------------- |
27
+ | keys | `string` or `string[]` | List of keys or a single key |
28
+ | cookie | `string` | Cookie string |
29
+
30
+ **Example**:
31
+
32
+ ```javascript
33
+ let cookie = 'name=John; age=30; role=admin'
34
+
35
+ readCookie('age', cookie) // => '30'
36
+
37
+ readCookie(['age', 'name'], cookie) // => ['30', 'John']
38
+ ```
39
+
40
+ ## serializeCookie
41
+
42
+ Serializes a cookie from its components into a string.
43
+
44
+ | Parameter | Type | Description |
45
+ | ---------- | -------------------- | -------------------------------------------------------- |
46
+ | name | `string` | The name of the cookie |
47
+ | value | `string` | The value of the cookie |
48
+ | attributes | `[string, string][]` | An array containing additional attributes for the cookie |
49
+
50
+ **Example**:
51
+
52
+ ```javascript
53
+ let attributes = [
54
+ ['path', '/'],
55
+ ['expires', 'Wed, 21 Oct 2026 07:28:00 GMT'],
56
+ ]
57
+
58
+ serializeCookie('name', 'John', attributes) // => 'name=John; path=/; expires=Wed, 21 Oct 2026 07:28:00 GMT'
59
+ ```
@@ -0,0 +1,75 @@
1
+ import { is, isLike } from '@yurkimus/types'
2
+
3
+ /**
4
+ * Parses a cookie string into a list of key-value pairs.
5
+ *
6
+ * @param {string} cookie
7
+ *
8
+ * @returns {[string, string][]}
9
+ *
10
+ * @example
11
+ * ```
12
+ * let cookie = 'name=John; age=30; role=admin',
13
+ * parsed = parseCookie(cookieString); // => [string, string][]
14
+ * ```
15
+ */
16
+ export var parseCookie = (cookie) => {
17
+ if (!is('String', cookie)) {
18
+ throw new TypeError('"cookie" must be a string!')
19
+ }
20
+
21
+ return cookie.split('; ').map((string) => string.split('='))
22
+ }
23
+
24
+ /**
25
+ * Reads cookies from a cookie string using a provided list of keys.
26
+ *
27
+ * @param {string | string[]} keys
28
+ * @param {string} cookie
29
+ *
30
+ * @returns {string | string[]}
31
+ *
32
+ * @example
33
+ * ```
34
+ * let cookie = 'name=John; age=30; role=admin',
35
+ * cookies1 = readCookie('age', cookie); // => '30'
36
+ * cookies2 = readCookie(['age', 'name'], cookie); // => ['30', 'John']
37
+ * ```
38
+ */
39
+ export var readCookie = (keys, cookie) => {
40
+ if (!is('String', cookie)) {
41
+ throw new TypeError('"cookie" must be a String!')
42
+ }
43
+
44
+ var cookies = Object.fromEntries(parseCookie(cookie))
45
+
46
+ if (isLike('Array')) {
47
+ return Array.prototype.map.call(keys, (key) => cookies[key])
48
+ }
49
+
50
+ return cookies[keys]
51
+ }
52
+
53
+ /**
54
+ * Serializes a cookie from its components into a string.
55
+ *
56
+ * @param {string} name The name of the cookie.
57
+ * @param {string} value The value of the cookie.
58
+ * @param {[string, string][]} attributes An array containing additional attributes for the cookie.
59
+ *
60
+ * @example
61
+ * ```
62
+ * let name = 'name',
63
+ * value = 'John',
64
+ * attributes = [['path', '/'], ['expires', 'Wed, 21 Oct 2026 07:28:00 GMT']],
65
+ * serialized = serializeCookie(name, value, attributes); // => 'name=John; path=/; expires=Wed, 21 Oct 2026 07:28:00 GMT'
66
+ * ```
67
+ */
68
+ export var serializeCookie = (name, value, attributes) =>
69
+ name +
70
+ '=' +
71
+ value +
72
+ attributes.reduce(
73
+ (string, attribute) => string + '; ' + attribute.join('='),
74
+ '',
75
+ )