Skip to content

Commit ef49662

Browse files
Adjust docs
1 parent d4318a9 commit ef49662

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ Cookies.getJSON(); // => { name: { foo: 'bar' } }
104104

105105
This project is [RFC 6265](http://tools.ietf.org/html/rfc6265#section-4.1.1) compliant.
106106
All special characters that are not allowed in the cookie-value or cookie-name are encoded with each UTF-8 Hex equivalent.
107-
The only character in cookie-name and cookie-value that is allowed and still encoded is the percent `%` character, it is escaped in order to interpret the input as literal.
107+
The only character in cookie-name and cookie-value that is allowed and still encoded is the percent `%` character, it is escaped in order to interpret the input as literal.
108+
To override the default cookie decoding you need to use a [converter](#converter).
108109

109110
## Cookie Attributes
110111

@@ -173,24 +174,34 @@ Cookies.get('name'); // => 'value' (if already in secure protocol)
173174
Cookies.remove('name', { secure: true });
174175
```
175176

176-
## Converters
177+
## Converter
177178

178-
Provide a conversion function as optional second argument for reading, in order to change the cookie's value
179-
to a different representation on the fly.
179+
Create a new instance of the api that overrides the default decoding implementation.
180+
All methods that rely in a proper decoding to work, such as `Cookies.remove()` and `Cookies.get()`, will run the converter first for each cookie.
181+
The returning String will be used as the cookie value.
180182

181-
Example for parsing the value from a cookie generated with PHP's `setcookie()` method:
183+
Example from reading one of the cookies that can only be decoded using the `escape` function:
182184

183185
```javascript
184-
// 'cookie+with+space' => 'cookie with space'
185-
Cookies.get('foo', function (value) {
186-
return value.replace(/\+/g, ' ');
186+
document.cookie = 'escaped=%u5317';
187+
document.cookie = 'default=%E5%8C%97';
188+
var cookies = Cookies.withConverter(function (value, name) {
189+
if ( name === 'escaped' ) {
190+
return unescape(value);
191+
}
187192
});
193+
cookies.get('escaped'); // 北
194+
cookies.get('default'); // 北
195+
cookies.get(); // { escaped: '北', default: '北' }
188196
```
189197

190-
Dealing with cookies that have been encoded using `escape` (3rd party cookies):
198+
Example for parsing the value from a cookie generated with PHP's `setcookie()` method:
191199

192200
```javascript
193-
Cookies.get('foo', unescape);
201+
// 'cookie+with+space' => 'cookie with space'
202+
Cookies.withConverter(function (value) {
203+
return value.replace(/\+/g, ' ');
204+
}).get('foo');
194205
```
195206

196207
## Contributing

0 commit comments

Comments
 (0)