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