/** * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /** * @param {Number} columns * @param {Array.} tableData * @param {Object} [attributes] * * @returns {String} */ export function modelTable( columns, tableData, attributes ) { const tableRows = tableData .map( cellData => `${ cellData }` ) .reduce( ( table, tableCell, index ) => { if ( index % columns === 0 ) { table += ''; } table += tableCell; if ( index % columns === columns - 1 ) { table += ''; } return table; }, '' ); let attributesString = ''; if ( attributes ) { const entries = Object.entries( attributes ); attributesString = ' ' + entries.map( entry => `${ entry[ 0 ] }="${ entry[ 1 ] }"` ).join( ' ' ); } return `${ tableRows }`; } export function formatModelTable( tableString ) { return tableString .replace( //g, '\n\n ' ) .replace( /<\/tableRow>/g, '\n' ) .replace( /<\/table>/g, '\n' ); } export function formattedModelTable( columns, tableData, attributes ) { const tableString = modelTable( columns, tableData, attributes ); return formatModelTable( tableString ); }