Make parseBoolean idempotent

This commit is contained in:
Winnie Hellmann 2019-01-15 06:04:37 +00:00 committed by Kushal Pandya
parent 0e510780a1
commit 159c18221d
2 changed files with 19 additions and 7 deletions

View file

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

View file

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