@yurkimus/cookies 0.0.0 → 0.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 yurkimus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/deno.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "@yurkimus/cookies",
3
- "version": "0.0.0",
4
- "fmt": {
5
- "semiColons": false,
6
- "singleQuote": true
7
- },
3
+ "version": "0.0.2",
8
4
  "exports": "./source/index.js",
9
5
  "imports": {
10
- "@yurkimus/curry": "https://raw.githubusercontent.com/yurkimus/curry/main/source/index.js"
6
+ "@yurkimus/curry": "https://raw.githubusercontent.com/yurkimus/curry/main/source/index.js",
7
+ "@yurkimus/types": "https://raw.githubusercontent.com/yurkimus/types/main/source/index.js"
11
8
  }
12
9
  }
package/dprint.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "typescript": {
3
+ "semiColons": "asi",
4
+ "quoteStyle": "preferSingle",
5
+ "useBraces": "maintain"
6
+ },
7
+
8
+ "json": {
9
+ },
10
+
11
+ "markdown": {
12
+ },
13
+
14
+ "excludes": [
15
+ "**/node_modules",
16
+ "**/*-lock.json"
17
+ ],
18
+
19
+ "plugins": [
20
+ "https://plugins.dprint.dev/typescript-0.93.0.wasm",
21
+ "https://plugins.dprint.dev/json-0.19.3.wasm",
22
+ "https://plugins.dprint.dev/markdown-0.17.8.wasm"
23
+ ]
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
4
4
  "name": "@yurkimus/cookies",
5
5
  "author": {
6
6
  "name": "yurkimus",
package/readme.md CHANGED
@@ -1,53 +1,83 @@
1
1
  # Cookies
2
2
 
3
- A collection of utilities for handling cookies in JavaScript.
3
+ JavaScript-utilities to handle cookies everywhere.
4
4
 
5
- ## parseCookie
5
+ ## Table of Contents
6
6
 
7
- Parses a cookie string into a list of key-value pairs.
7
+ - [Installation](#installation)
8
+ - [Exports](#exports)
9
+ - [parse](#parse)
10
+ - [read](#read)
11
+ - [serialize](#serialize)
12
+ - [License](#license)
8
13
 
9
- | Parameter | Type | Description |
10
- | --------- | -------- | ------------- |
11
- | cookie | `string` | Cookie string |
14
+ ## Installation
12
15
 
13
- **Returns**: `[string, string][]`
16
+ ### npm
14
17
 
15
- **Example**:
18
+ ```
19
+ npm install @yurkimus/cookies
20
+ ```
21
+
22
+ ### urls
23
+
24
+ ```
25
+ "@yurkimus/cookies": "npm:@yurkimus/cookies"
26
+ ```
16
27
 
17
- ```javascript
18
- parseCookie('name=John; age=30; role=admin') // => [['name', 'John'], ['age', '30'], ['role', 'admin']]
28
+ ```
29
+ "@yurkimus/cookies": "github:yurkimus/cookies"
30
+ ```
31
+
32
+ ```
33
+ "@yurkimus/cookies": "https://raw.githubusercontent.com/yurkimus/cookies/main/source/index.js"
19
34
  ```
20
35
 
21
- ## readCookie
36
+ ## Exports
22
37
 
23
- Reads cookies from a cookie string using a provided list of keys.
38
+ ### read
24
39
 
25
- | Parameter | Type | Description |
26
- | --------- | ---------------------- | ---------------------------- |
27
- | keys | `string` or `string[]` | List of keys or a single key |
28
- | cookie | `string` | Cookie string |
40
+ #### Definition
41
+
42
+ ```
43
+ read :: string -> string -> string
44
+ ```
29
45
 
30
- **Example**:
46
+ #### Example
31
47
 
32
48
  ```javascript
33
49
  let cookie = 'name=John; age=30; role=admin'
34
50
 
35
- readCookie('age', cookie) // => '30'
51
+ let age = read('age', cookie) // => '30'
36
52
 
37
- readCookie(['age', 'name'], cookie) // => ['30', 'John']
53
+ let [age, name] = ['age', 'name']
54
+ .map(key => read(key))
55
+ .map(reader => reader(cookie)) // => ['30', 'John']
38
56
  ```
39
57
 
40
- ## serializeCookie
58
+ ### parse
41
59
 
42
- Serializes a cookie from its components into a string.
60
+ #### Definition
43
61
 
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 |
62
+ ```
63
+ parse :: string -> object
64
+ ```
49
65
 
50
- **Example**:
66
+ #### Example
67
+
68
+ ```javascript
69
+ parse('name=John; age=30; role=admin') // => { name: 'John', age: '30', role: 'admin' }
70
+ ```
71
+
72
+ ### serialize
73
+
74
+ #### Definition
75
+
76
+ ```
77
+ serialize :: string -> * -> [(string, string)] -> string
78
+ ```
79
+
80
+ #### Example
51
81
 
52
82
  ```javascript
53
83
  let attributes = [
@@ -55,5 +85,9 @@ let attributes = [
55
85
  ['expires', 'Wed, 21 Oct 2026 07:28:00 GMT'],
56
86
  ]
57
87
 
58
- serializeCookie('name', 'John', attributes) // => 'name=John; path=/; expires=Wed, 21 Oct 2026 07:28:00 GMT'
88
+ serialize('name', 'John', attributes) // => 'name=John; path=/; expires=Wed, 21 Oct 2026 07:28:00 GMT'
59
89
  ```
90
+
91
+ ## License
92
+
93
+ [MIT](LICENSE)
package/source/index.js CHANGED
@@ -1,62 +1,34 @@
1
- import { is, isLike } from '@yurkimus/types'
1
+ import { curry } from '@yurkimus/curry'
2
+ import { is } from '@yurkimus/types'
2
3
 
3
4
  /**
4
5
  * 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
6
  */
16
- export var parseCookie = (cookie) => {
17
- if (!is('String', cookie)) {
18
- throw new TypeError('"cookie" must be a string!')
19
- }
7
+ export var parse = value => {
8
+ if (!is('String', value))
9
+ throw new TypeError(`Parameter 'value' must be a string.`)
20
10
 
21
- return cookie.split('; ').map((string) => string.split('='))
11
+ return value
12
+ .split('; ')
13
+ .map(string => string.split('='))
14
+ .reduce((object, [key, value]) => (object[key] = value, object), {})
22
15
  }
23
16
 
24
17
  /**
25
18
  * 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
19
  */
39
- export var readCookie = (keys, cookie) => {
40
- if (!is('String', cookie)) {
41
- throw new TypeError('"cookie" must be a String!')
42
- }
20
+ export var read = curry(
21
+ (key, value) => {
22
+ if (!is('String', value))
23
+ throw new TypeError(`Parameter 'value' must be a string.`)
43
24
 
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
- }
25
+ return parse(value)[key]
26
+ },
27
+ )
52
28
 
53
29
  /**
54
30
  * Serializes a cookie from its components into a string.
55
31
  *
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
32
  * @example
61
33
  * ```
62
34
  * let name = 'name',
@@ -65,11 +37,10 @@ export var readCookie = (keys, cookie) => {
65
37
  * serialized = serializeCookie(name, value, attributes); // => 'name=John; path=/; expires=Wed, 21 Oct 2026 07:28:00 GMT'
66
38
  * ```
67
39
  */
68
- export var serializeCookie = (name, value, attributes) =>
69
- name +
70
- '=' +
71
- value +
72
- attributes.reduce(
73
- (string, attribute) => string + '; ' + attribute.join('='),
74
- '',
75
- )
40
+ export var serialize = curry(
41
+ (name, value, attributes) =>
42
+ name
43
+ + '='
44
+ + value
45
+ + attributes.reduce((string, attribute) => string + '; ' + attribute.join('='), ''),
46
+ )