Move b64EncodeJSON to Utils

This commit is contained in:
djmaze 2021-12-07 12:40:55 +01:00
parent 148913ea32
commit e6b6531248
2 changed files with 11 additions and 2 deletions

View file

@ -49,6 +49,15 @@ export const
} }
}, },
// unescape(encodeURIComponent()) makes the UTF-16 DOMString to an UTF-8 string
b64EncodeJSON = data => btoa(unescape(encodeURIComponent(JSON.stringify(data)))),
/* // Without deprecated 'unescape':
b64EncodeJSON = data => btoa(encodeURIComponent(JSON.stringify(data)).replace(
/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode('0x' + p1)
)),
*/
b64EncodeJSONSafe = data => b64EncodeJSON(data).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''),
settingsSaveHelperSimpleFunction = (koTrigger, context) => settingsSaveHelperSimpleFunction = (koTrigger, context) =>
iError => { iError => {
koTrigger.call(context, iError ? SaveSettingsStep.FalseResult : SaveSettingsStep.TrueResult); koTrigger.call(context, iError ? SaveSettingsStep.FalseResult : SaveSettingsStep.TrueResult);

View file

@ -2,7 +2,7 @@ import ko from 'ko';
import { AbstractModel } from 'Knoin/AbstractModel'; import { AbstractModel } from 'Knoin/AbstractModel';
import { FilterModel } from 'Model/Filter'; import { FilterModel } from 'Model/Filter';
import { arrayLength, pString } from 'Common/Utils'; import { arrayLength, pString, b64EncodeJSON } from 'Common/Utils';
const SIEVE_FILE_NAME = 'rainloop.user'; const SIEVE_FILE_NAME = 'rainloop.user';
@ -215,7 +215,7 @@ function filtersToSieveScript(filters)
'/*', '/*',
'BEGIN:FILTER:' + filter.id, 'BEGIN:FILTER:' + filter.id,
'BEGIN:HEADER', 'BEGIN:HEADER',
btoa(unescape(encodeURIComponent(JSON.stringify(filter.toJson())))).match(split).join(eol) + 'END:HEADER', b64EncodeJSON(filter.toJson()).match(split).join(eol) + 'END:HEADER',
'*/', '*/',
filter.enabled() ? '' : '/* @Filter is disabled ', filter.enabled() ? '' : '/* @Filter is disabled ',
filterToString(filter, require), filterToString(filter, require),