Allow users to define either converter.read or converter.write (#34231)

This commit is contained in:
yutod 2019-03-28 08:10:16 +09:00 committed by Ron Buckton
parent 7631f52e7f
commit e2476bf0eb
2 changed files with 14 additions and 1 deletions

View File

@ -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;

View File

@ -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');