/* jshint node:true */
'use strict';

// Escape the textDelimiters contained in the field
/*(https://tools.ietf.org/html/rfc4180)
   7.  If double-quotes are used to enclose fields, then a double-quote
   appearing inside a field must be escaped by preceding it with
   another double quote.
   For example: "aaa","b""bb","ccc"
*/

module.exports = function escapedDelimiters(textDelimiter, rowDelimiter) {
  let endOfLine = '\n';

  if (typeof textDelimiter !== 'string') {
    throw new TypeError('Invalid param "textDelimiter", must be a string.');
  }

  if (typeof rowDelimiter !== 'string') {
    throw new TypeError('Invalid param "rowDelimiter", must be a string.');
  }

  let textDelimiterRegex = new RegExp("\\" + textDelimiter, 'g');
  let escapedDelimiter = textDelimiter + textDelimiter;

  return function(value) {
    if (!value.replace) return value;
    // Escape the textDelimiters contained in the field
    var newValue = value.replace(textDelimiterRegex, escapedDelimiter);
    // Escape the whole field if it contains a rowDelimiter or a linebreak
    if (newValue.indexOf(rowDelimiter) >= 0 || newValue.indexOf(endOfLine) >= 0) {
      newValue = textDelimiter + newValue + textDelimiter;
    }

    return newValue;
  };
};

/* jshint node:true */
'use strict';

// Escape the textDelimiters contained in the field
/*(https://tools.ietf.org/html/rfc4180)
   7.  If double-quotes are used to enclose fields, then a double-quote
   appearing inside a field must be escaped by preceding it with
   another double quote.
   For example: "aaa","b""bb","ccc"
*/

module.exports = function escapedDelimiters(textDelimiter, rowDelimiter) {
  let endOfLine = '\n';

  if (typeof textDelimiter !== 'string') {
    throw new TypeError('Invalid param "textDelimiter", must be a string.');
  }

  if (typeof rowDelimiter !== 'string') {
    throw new TypeError('Invalid param "rowDelimiter", must be a string.');
  }

  let textDelimiterRegex = new RegExp("\\" + textDelimiter, 'g');
  let escapedDelimiter = textDelimiter + textDelimiter;

  const enclosingCondition = (textDelimiter === '"') ? 
    (value) => (value.indexOf(rowDelimiter) >= 0 || 
        value.indexOf(endOfLine) >= 0 ||
        value.indexOf('"') >= 0)
    : (value) => (value.indexOf(rowDelimiter) >= 0 || 
        value.indexOf(endOfLine) >= 0);

  return function(value) {
    if (!value.replace) return value;
    // Escape the textDelimiters contained in the field
    var newValue = value.replace(textDelimiterRegex, escapedDelimiter);
    // Escape the whole field if it contains a rowDelimiter or a linebreak or double quote
    if (enclosingCondition(value)) {
      newValue = textDelimiter + newValue + textDelimiter;
    }

    return newValue;
  };
};

fix for https://tools.ietf.org/html/rfc4180#page-3 paragraph 7