mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
Allow users to define either converter.read or converter.write (#34231)
This commit is contained in:
parent
7631f52e7f
commit
e2476bf0eb
3
types/js-cookie/index.d.ts
vendored
3
types/js-cookie/index.d.ts
vendored
@ -3,6 +3,7 @@
|
||||
// Definitions by: Theodore Brown <https://github.com/theodorejb>
|
||||
// BendingBender <https://github.com/BendingBender>
|
||||
// Antoine Lépée <https://github.com/alepee>
|
||||
// Yuto Doi <https://github.com/yutod>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
@ -88,7 +89,7 @@ declare namespace Cookies {
|
||||
* will run the converter first for each cookie. The returned
|
||||
* string will be used as the cookie value.
|
||||
*/
|
||||
withConverter<TConv extends object>(converter: CookieReadConverter | { write: CookieWriteConverter<TConv>; read: CookieReadConverter; }): CookiesStatic<TConv>;
|
||||
withConverter<TConv extends object>(converter: CookieReadConverter | { write?: CookieWriteConverter<TConv>; read?: CookieReadConverter; }): CookiesStatic<TConv>;
|
||||
}
|
||||
|
||||
type CookieWriteConverter<T extends object> = (value: string | T, name: string) => string;
|
||||
|
||||
@ -53,3 +53,15 @@ const PHPCookies = Cookies.withConverter<object>({
|
||||
.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
|
||||
}
|
||||
});
|
||||
|
||||
const BlankConverterCookies = Cookies.withConverter({
|
||||
read(value, name) {
|
||||
if (name === 'hoge') {
|
||||
return value.replace('hoge', 'fuga');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
});
|
||||
|
||||
document.cookie = 'hoge=hogehoge';
|
||||
BlankConverterCookies.get('hoge');
|
||||
|
||||
Loading…
Reference in New Issue
Block a user