Commit 159c1822 authored by Winnie Hellmann's avatar Winnie Hellmann Committed by Kushal Pandya

Make parseBoolean idempotent

parent 0e510780
......@@ -430,13 +430,14 @@ export const historyPushState = newUrl => {
};
/**
* Returns true for a String "true" and false otherwise.
* This is the opposite of Boolean(...).toString()
* Returns true for a String value of "true" and false otherwise.
* This is the opposite of Boolean(...).toString().
* `parseBoolean` is idempotent.
*
* @param {String} value
* @returns {Boolean}
*/
export const parseBoolean = value => value === 'true';
export const parseBoolean = value => (value && value.toString()) === 'true';
/**
* Converts permission provided as strings to booleans.
......
......@@ -347,20 +347,31 @@ describe('common_utils', () => {
});
describe('parseBoolean', () => {
const { parseBoolean } = commonUtils;
it('returns true for "true"', () => {
expect(commonUtils.parseBoolean('true')).toEqual(true);
expect(parseBoolean('true')).toEqual(true);
});
it('returns false for "false"', () => {
expect(commonUtils.parseBoolean('false')).toEqual(false);
expect(parseBoolean('false')).toEqual(false);
});
it('returns false for "something"', () => {
expect(commonUtils.parseBoolean('something')).toEqual(false);
expect(parseBoolean('something')).toEqual(false);
});
it('returns false for null', () => {
expect(commonUtils.parseBoolean(null)).toEqual(false);
expect(parseBoolean(null)).toEqual(false);
});
it('is idempotent', () => {
const input = ['true', 'false', 'something', null];
input.forEach(value => {
const result = parseBoolean(value);
expect(parseBoolean(result)).toBe(result);
});
});
});
......
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