luhn-generator
Version:
A generator of numbers that passes the validation of Luhn algorithm or Luhn formula, also known as the 'modulus 10' or 'mod 10' algorithm
47 lines (40 loc) • 1.36 kB
JavaScript
import getRole from '../aria/get-role';
import getElementSpec from '../standards/get-element-spec';
import nativeTextMethods from './native-text-methods';
/**
* Get the accessible text using native HTML methods only
* @param {VirtualNode} element
* @param {Object} context
* @property {Bool} debug Enable logging for formControlValue
* @return {String} Accessible text
*/
function nativeTextAlternative(virtualNode, context = {}) {
const { actualNode } = virtualNode;
if (
virtualNode.props.nodeType !== 1 ||
['presentation', 'none'].includes(getRole(virtualNode))
) {
return '';
}
const textMethods = findTextMethods(virtualNode);
// Find the first step that returns a non-empty string
let accName = textMethods.reduce((accName, step) => {
return accName || step(virtualNode, context);
}, '');
if (context.debug) {
axe.log(accName || '{empty-value}', actualNode, context);
}
return accName;
}
/**
* Get accessible text functions for a specific native HTML element
* @private
* @param {VirtualNode} element
* @return {Function[]} Array of native accessible name computation methods
*/
function findTextMethods(virtualNode) {
const elmSpec = getElementSpec(virtualNode);
const methods = elmSpec.namingMethods || [];
return methods.map(methodName => nativeTextMethods[methodName]);
}
export default nativeTextAlternative;