@yurkimus/cookies
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "@yurkimus/cookies", | ||
"version": "0.0.1", | ||
"fmt": { | ||
"semiColons": false, | ||
"singleQuote": true | ||
}, | ||
"version": "0.0.2", | ||
"exports": "./source/index.js", | ||
"imports": { | ||
"@yurkimus/curry": "https://raw.githubusercontent.com/yurkimus/curry/main/source/index.js" | ||
"@yurkimus/curry": "https://raw.githubusercontent.com/yurkimus/curry/main/source/index.js", | ||
"@yurkimus/types": "https://raw.githubusercontent.com/yurkimus/types/main/source/index.js" | ||
} | ||
} |
{ | ||
"type": "module", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"name": "@yurkimus/cookies", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -9,5 +9,5 @@ # Cookies | ||
- [Exports](#exports) | ||
- [parseCookie](#parseCookie) | ||
- [readCookie](#readCookie) | ||
- [serializeCookie](#serializeCookie) | ||
- [parse](#parse) | ||
- [read](#read) | ||
- [serialize](#serialize) | ||
- [License](#license) | ||
@@ -39,3 +39,3 @@ | ||
### parseCookie | ||
### read | ||
@@ -45,3 +45,3 @@ #### Definition | ||
``` | ||
parseCookie :: string -> * | ||
read :: string -> string -> string | ||
``` | ||
@@ -52,6 +52,12 @@ | ||
```javascript | ||
parseCookie('name=John; age=30; role=admin') // => [['name', 'John'], ['age', '30'], ['role', 'admin']] | ||
let cookie = 'name=John; age=30; role=admin' | ||
let age = read('age', cookie) // => '30' | ||
let [age, name] = ['age', 'name'] | ||
.map(key => read(key)) | ||
.map(reader => reader(cookie)) // => ['30', 'John'] | ||
``` | ||
### readCookie | ||
### parse | ||
@@ -61,3 +67,3 @@ #### Definition | ||
``` | ||
readCookie :: * -> string -> * | ||
parse :: string -> object | ||
``` | ||
@@ -68,10 +74,6 @@ | ||
```javascript | ||
let cookie = 'name=John; age=30; role=admin' | ||
readCookie('age', cookie) // => '30' | ||
readCookie(['age', 'name'], cookie) // => ['30', 'John'] | ||
parse('name=John; age=30; role=admin') // => { name: 'John', age: '30', role: 'admin' } | ||
``` | ||
### serializeCookie | ||
### serialize | ||
@@ -81,3 +83,3 @@ #### Definition | ||
``` | ||
serializeCookie :: string -> * -> [(string, string)] -> string | ||
serialize :: string -> * -> [(string, string)] -> string | ||
``` | ||
@@ -93,3 +95,3 @@ | ||
serializeCookie('name', 'John', attributes) // => 'name=John; path=/; expires=Wed, 21 Oct 2026 07:28:00 GMT' | ||
serialize('name', 'John', attributes) // => 'name=John; path=/; expires=Wed, 21 Oct 2026 07:28:00 GMT' | ||
``` | ||
@@ -96,0 +98,0 @@ |
@@ -1,22 +0,15 @@ | ||
import { is, isLike } from '@yurkimus/types' | ||
import { curry } from '@yurkimus/curry' | ||
import { is } from '@yurkimus/types' | ||
/** | ||
* Parses a cookie string into a list of key-value pairs. | ||
* | ||
* @param {string} cookie | ||
* | ||
* @returns {[string, string][]} | ||
* | ||
* @example | ||
* ``` | ||
* let cookie = 'name=John; age=30; role=admin', | ||
* parsed = parseCookie(cookieString); // => [string, string][] | ||
* ``` | ||
*/ | ||
export var parseCookie = (cookie) => { | ||
if (!is('String', cookie)) { | ||
throw new TypeError('"cookie" must be a string!') | ||
} | ||
export var parse = value => { | ||
if (!is('String', value)) | ||
throw new TypeError(`Parameter 'value' must be a string.`) | ||
return cookie.split('; ').map((string) => string.split('=')) | ||
return value | ||
.split('; ') | ||
.map(string => string.split('=')) | ||
.reduce((object, [key, value]) => (object[key] = value, object), {}) | ||
} | ||
@@ -26,36 +19,15 @@ | ||
* Reads cookies from a cookie string using a provided list of keys. | ||
* | ||
* @param {string | string[]} keys | ||
* @param {string} cookie | ||
* | ||
* @returns {string | string[]} | ||
* | ||
* @example | ||
* ``` | ||
* let cookie = 'name=John; age=30; role=admin', | ||
* cookies1 = readCookie('age', cookie); // => '30' | ||
* cookies2 = readCookie(['age', 'name'], cookie); // => ['30', 'John'] | ||
* ``` | ||
*/ | ||
export var readCookie = (keys, cookie) => { | ||
if (!is('String', cookie)) { | ||
throw new TypeError('"cookie" must be a String!') | ||
} | ||
export var read = curry( | ||
(key, value) => { | ||
if (!is('String', value)) | ||
throw new TypeError(`Parameter 'value' must be a string.`) | ||
var cookies = Object.fromEntries(parseCookie(cookie)) | ||
return parse(value)[key] | ||
}, | ||
) | ||
if (isLike('Array')) { | ||
return Array.prototype.map.call(keys, (key) => cookies[key]) | ||
} | ||
return cookies[keys] | ||
} | ||
/** | ||
* Serializes a cookie from its components into a string. | ||
* | ||
* @param {string} name The name of the cookie. | ||
* @param {string} value The value of the cookie. | ||
* @param {[string, string][]} attributes An array containing additional attributes for the cookie. | ||
* | ||
* @example | ||
@@ -69,9 +41,8 @@ * ``` | ||
*/ | ||
export var serializeCookie = (name, value, attributes) => | ||
name + | ||
'=' + | ||
value + | ||
attributes.reduce( | ||
(string, attribute) => string + '; ' + attribute.join('='), | ||
'', | ||
) | ||
export var serialize = curry( | ||
(name, value, attributes) => | ||
name | ||
+ '=' | ||
+ value | ||
+ attributes.reduce((string, attribute) => string + '; ' + attribute.join('='), ''), | ||
) |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
6
94
4685
70
1