Commit 2abbfda3 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'winh-idempotent-parseboolean' into 'master'

Make parseBoolean idempotent

See merge request gitlab-org/gitlab-ce!24365
parents 0e510780 159c1822
...@@ -430,13 +430,14 @@ export const historyPushState = newUrl => { ...@@ -430,13 +430,14 @@ export const historyPushState = newUrl => {
}; };
/** /**
* Returns true for a String "true" and false otherwise. * Returns true for a String value of "true" and false otherwise.
* This is the opposite of Boolean(...).toString() * This is the opposite of Boolean(...).toString().
* `parseBoolean` is idempotent.
* *
* @param {String} value * @param {String} value
* @returns {Boolean} * @returns {Boolean}
*/ */
export const parseBoolean = value => value === 'true'; export const parseBoolean = value => (value && value.toString()) === 'true';
/** /**
* Converts permission provided as strings to booleans. * Converts permission provided as strings to booleans.
......
...@@ -347,20 +347,31 @@ describe('common_utils', () => { ...@@ -347,20 +347,31 @@ describe('common_utils', () => {
}); });
describe('parseBoolean', () => { describe('parseBoolean', () => {
const { parseBoolean } = commonUtils;
it('returns true for "true"', () => { it('returns true for "true"', () => {
expect(commonUtils.parseBoolean('true')).toEqual(true); expect(parseBoolean('true')).toEqual(true);
}); });
it('returns false for "false"', () => { it('returns false for "false"', () => {
expect(commonUtils.parseBoolean('false')).toEqual(false); expect(parseBoolean('false')).toEqual(false);
}); });
it('returns false for "something"', () => { it('returns false for "something"', () => {
expect(commonUtils.parseBoolean('something')).toEqual(false); expect(parseBoolean('something')).toEqual(false);
}); });
it('returns false for null', () => { 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