UNPKG

@gravityforms/utils

Version:
31 lines (29 loc) 817 B
/** * @module convertElements * @description Highly efficient function to convert a nodelist into a standard array. * Allows you to run Array.forEach in older browsers/safari on a Nodelist. * * @since 1.0.0 * * @param {Element|NodeList|Array} elements to convert * * @return {Array} An array converted elements or empty if none passed. * * @example * import { convertElements } from "@gravityforms/utils"; * * function Example( convert = true ) { * let nodes = node.querySelectorAll( '.example' ); * if ( convert ) { * nodes = convertElements( nodes ); * } * return nodes; * } * */ export default function convertElements( elements = [] ) { const converted = []; let i = elements.length; for ( i; i--; converted.unshift( elements[ i ] ) ); // eslint-disable-line return converted; }