Commit a9137659 authored by Mark Florian's avatar Mark Florian

Simplify data attribute checking

parent 815a177b
import { has } from 'lodash';
import { isInIssuePage, isInMRPage, isInEpicPage } from './common_utils'; import { isInIssuePage, isInMRPage, isInEpicPage } from './common_utils';
import { dasherize, convertToSnakeCase } from './text_utility';
export const addClassIfElementExists = (element, className) => { export const addClassIfElementExists = (element, className) => {
if (element) { if (element) {
...@@ -41,10 +41,9 @@ export const toggleContainerClasses = (containerEl, classList) => { ...@@ -41,10 +41,9 @@ export const toggleContainerClasses = (containerEl, classList) => {
* @param {string[]} names - The dataset (i.e., camelCase) names to inspect * @param {string[]} names - The dataset (i.e., camelCase) names to inspect
* @returns {Object.<string, boolean>} * @returns {Object.<string, boolean>}
*/ */
export const parseBooleanDataAttributes = (element, names) => export const parseBooleanDataAttributes = ({ dataset }, names) =>
names.reduce((acc, name) => { names.reduce((acc, name) => {
const attributeName = `data-${dasherize(convertToSnakeCase(name))}`; acc[name] = has(dataset, name);
acc[name] = element.hasAttribute(attributeName);
return acc; return acc;
}, {}); }, {});
...@@ -140,13 +140,23 @@ describe('DOM Utils', () => { ...@@ -140,13 +140,23 @@ describe('DOM Utils', () => {
it('correctly parses boolean-like data attributes', () => { it('correctly parses boolean-like data attributes', () => {
expect( expect(
parseBooleanDataAttributes(element, ['fooBar', 'foobar', 'baz', 'qux', 'doesNotExist']), parseBooleanDataAttributes(element, [
'fooBar',
'foobar',
'baz',
'qux',
'doesNotExist',
'toString',
]),
).toEqual({ ).toEqual({
fooBar: true, fooBar: true,
foobar: false, foobar: false,
baz: true, baz: true,
qux: true, qux: true,
doesNotExist: false, doesNotExist: false,
// Ensure prototype properties aren't false positives
toString: false,
}); });
}); });
}); });
......
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