fix(utils/encode-decode): Use lodash replace

_.replace is harded against undefined/null
This commit is contained in:
Berkeley Martinez
2018-01-16 20:02:35 -08:00
committed by mrugesh mohapatra
parent f6e4bf74cb
commit e8dcc43e23
2 changed files with 95 additions and 33 deletions

View File

@@ -1,46 +1,38 @@
import flow from 'lodash/flow';
import _ from 'lodash/fp';
// we don't store loop protect disable key
export function removeNoprotect(val) {
return val.replace(/noprotect/gi, '');
}
export const removeNoprotect = _.replace(/noprotect/gi, '');
export function encodeScriptTags(val) {
return val
.replace(/<script>/gi, 'fccss')
.replace(/<\/script>/gi, 'fcces');
}
export const encodeScriptTags = _.flow(
_.replace(/<script>/gi, 'fccss'),
_.replace(/<\/script>/gi, 'fcces')
);
export function decodeScriptTags(val) {
return val
.replace(/fccss/gi, '<script>')
.replace(/fcces/gi, '</script>');
}
export const decodeScriptTags = _.flow(
_.replace(/fccss/gi, '<script>'),
_.replace(/fcces/gi, '</script>')
);
export function encodeFormAction(val) {
return val.replace(
// look for attributes in a form
/<form[^>]*>/,
// val is the string within the opening form tag
// look for an `action` attribute, replace it with a fcc tag
val => val.replace(/action(\s*?)=/, 'fccfaa$1=')
);
}
export const encodeFormAction = _.replace(
// look for attributes in a form
/<form[^>]*>/,
// val is the string within the opening form tag
// look for an `action` attribute, replace it with a fcc tag
_.replace(/action(\s*?)=/, 'fccfaa$1=')
);
export function decodeFormAction(val) {
return val.replace(
/<form[^>]*>/,
val => val.replace(/fccfaa(\s*?)=/, 'action$1=')
);
}
export const decodeFormAction = _.replace(
/<form[^>]*>/,
_.replace(/fccfaa(\s*?)=/, 'action$1=')
);
export const encodeFcc = flow([
export const encodeFcc = _.flow(
removeNoprotect,
encodeFormAction,
encodeScriptTags
]);
);
export const decodeFcc = flow([
export const decodeFcc = _.flow(
decodeFormAction,
decodeScriptTags
]);
);