Commit 47fad335 authored by Winnie Hellmann's avatar Winnie Hellmann

Add parseBoolean helper function

parent ea8f0f3b
......@@ -420,13 +420,29 @@ export const historyPushState = newUrl => {
window.history.pushState({}, document.title, newUrl);
};
/**
* Returns true for a String "true" and false otherwise.
* This is the opposite of Boolean(...).toString()
*
* @param {String} value
* @returns {Boolean}
*/
export const parseBoolean = value => value === 'true';
/**
* Converts permission provided as strings to booleans.
*
* @param {String} string
* @returns {Boolean}
*/
export const convertPermissionToBoolean = permission => permission === 'true';
export const convertPermissionToBoolean = permission => {
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.warn('convertPermissionToBoolean is deprecated! Please use parseBoolean instead.');
}
return parseBoolean(permission);
};
/**
* Back Off exponential algorithm
......
......@@ -346,6 +346,24 @@ describe('common_utils', () => {
});
});
describe('parseBoolean', () => {
it('returns true for "true"', () => {
expect(commonUtils.parseBoolean('true')).toEqual(true);
});
it('returns false for "false"', () => {
expect(commonUtils.parseBoolean('false')).toEqual(false);
});
it('returns false for "something"', () => {
expect(commonUtils.parseBoolean('something')).toEqual(false);
});
it('returns false for null', () => {
expect(commonUtils.parseBoolean(null)).toEqual(false);
});
});
describe('convertPermissionToBoolean', () => {
it('should convert a boolean in a string to a boolean', () => {
expect(commonUtils.convertPermissionToBoolean('true')).toEqual(true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment