UNPKG

diet-yadda

Version:

A true BDD framework for JavaScript - slimmed down

88 lines (74 loc) 2.88 kB
/* * Copyright 2010 Acuminous Ltd / Energized Work Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* jslint node: true */ "use strict"; var $ = require('../Array'); var StringUtils = require('../StringUtils'); var COLUMN_SEPARATOR_REGEX = /[\|\u2506]/; var OUTER_BORDERS_REGEX = /^[\|\u2506]|[\|\u2506]$/g; var DASH_REGEX = /^[\\|\u2506]?-{3,}/; module.exports = function table_converter(value, next) { var rows = value.split(/\n/); var headings = parse_headings(rows.shift()); var handler = is_horizinal_separator(rows[0]) ? handle_multiline_row : handle_single_line_row; var table = $(); var watermark = 0; try { $(rows).each(handler); next(null, collapse(table)); } catch(err) { next(err); } function handle_single_line_row(row) { if (is_horizinal_separator(row)) throw new Error('Dashes are unexpected at this time'); start_new_row(); parse_fields(row); } function handle_multiline_row(row) { if (is_horizinal_separator(row)) return start_new_row(); parse_fields(row); } function parse_headings(row) { return $(row.replace(OUTER_BORDERS_REGEX, '').split(COLUMN_SEPARATOR_REGEX)).collect(function(value) { return { text: StringUtils.trim(value), indentation: StringUtils.indentation(value) }; }).naked(); } function is_horizinal_separator(row) { return DASH_REGEX.test(row); } function start_new_row() { table.push({}); } function parse_fields(row) { var fields = table.last(); $(row.replace(OUTER_BORDERS_REGEX, '').split(COLUMN_SEPARATOR_REGEX)).each(function(field, index) { var column = headings[index].text; var indentation = headings[index].indentation; var text = StringUtils.rtrim(field.substr(indentation)); if (StringUtils.isNotBlank(field) && StringUtils.indentation(field) < indentation) throw new Error('Indentation error'); fields[column] = (fields[column] || []).concat(text); }); } function collapse(table) { return table.collect(function(row) { var new_row = {}; for (var heading in row) { new_row[heading] = row[heading].join('\n'); } return new_row; }).naked(); } };