Package Exports
- @trkm/http-cookies-ts
- @trkm/http-cookies-ts/src/index.ts
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@trkm/http-cookies-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@trkm/http-cookies-ts
Converts the Http cookie format (document.cookie) to a javascript object: the cookie name becomes the object property name.
Features
- Leverages
decodeURIComponent
for both cookie name and cookie value. - Alter the cookie name before using the name as an object property: lowerPascalCase, lowerCase, etc.
- 100% typescript
Usage
cookiesToObj
Converts a string of the format keyName01=keyValue01;keyName02=keyValue02
(the key/value pair format returned by document.cookie) into an object whose properties are the keyName
and value is the keyValue
. WARNING: Key names are case sensitive along with javascript object properties. See Http Cookies for details.
- @param keyValueString - A string containing zero or more key/value pairs of the format
keyName01=keyValue01;
. - @param options:
- mutateProperty - Lambda with a function signature of
(propertyName: string) => string;
used to convert the key name before using the key name as an object property.
- mutateProperty - Lambda with a function signature of
- @returns - An object whose properties are the properties of all keys in the keyValueString and values are the value of the keyValueString.
import { cookiesToObj } from '@trkm/http-cookies-ts';
const cookies = cookiesToObj(document.cookie);
// convert cookie name to lowercase
const toLowerCase = (input: string):string => {
return input.toLowerCase();
}
const cookies2 = cookiesToObj(
'ENABLED=true;updated=false;',
{
mutateProperty: toLowerCase,
}
);
console.log(cookies2);
// output
// {
// enabled: true,
// updated: false,
// }
Note: Ignores invalid cookie definitions: cookieName=cookieValue
Intent
- No Emitted Javascript - The intent is to import this typescript library into a typescript project: compiling to Javascript occurring within the project.
Development
See the monorepo readme.
License
Licensed under MIT.