'use strict';
var stringWidth = require('string-width');
var repeating = require('repeating');
var chalk = require('chalk');
var objectAssign = require('object-assign');
var widestLine = require('widest-line');
var filledArray = require('filled-array');
var borderChars = require('./border-characters');

var getObject = function (detail) {
	var obj;

	if (typeof detail === 'number') {
		obj = {
			top: detail,
			right: detail * 3,
			bottom: detail,
			left: detail * 3
		};
	} else {
		obj = objectAssign({
			top: 0,
			right: 0,
			bottom: 0,
			left: 0
		}, detail);
	}

	return obj;
};

var getBorderChars = function (borderStyle) {
	var sides = [
		'topLeft',
		'topRight',
		'bottomRight',
		'bottomLeft',
		'vertical',
		'horizontal'
	];

	var chars;

	if (typeof borderStyle === 'string') {
		chars = borderChars[borderStyle];

		if (!chars) {
			throw new TypeError('Invalid border style: ' + borderStyle);
		}
	} else {
		sides.forEach(function (key) {
			if (!borderStyle[key] || typeof borderStyle[key] !== 'string') {
				throw new TypeError('Invalid border style: ' + key);
			}
		});

		chars = borderStyle;
	}

	return chars;
};

module.exports = function (text, opts) {
	opts = objectAssign({
		padding: 0,
		borderStyle: 'single'
	}, opts);

	if (opts.borderColor && !chalk[opts.borderColor]) {
		throw new Error(opts.borderColor + ' is not a valid borderColor');
	}

	var chars = getBorderChars(opts.borderStyle);
	var padding = getObject(opts.padding);
	var margin = getObject(opts.margin);

	var colorizeBorder = function (x) {
		return opts.borderColor ? chalk[opts.borderColor](x) : x;
	};

	var lines = text.split('\n');

	if (padding.top > 0) {
		lines = filledArray('', padding.top).concat(lines);
	}

	if (padding.bottom > 0) {
		lines = lines.concat(filledArray('', padding.bottom));
	}

	var contentWidth = widestLine(text) + padding.left + padding.right;
	var horizontal = repeating(chars.horizontal, contentWidth);
	var top = colorizeBorder(repeating('\n', margin.top) + repeating(' ', margin.left) + chars.topLeft + horizontal + chars.topRight);
	var bottom = colorizeBorder(repeating(' ', margin.left) + chars.bottomLeft + horizontal + chars.bottomRight + repeating('\n', margin.bottom));
	var side = colorizeBorder(chars.vertical);

	var middle = lines.map(function (line) {
		var paddingLeft = repeating(' ', padding.left);
		var paddingRight = repeating(' ', contentWidth - stringWidth(line) - padding.left);

		return repeating(' ', margin.left) + side + paddingLeft + line + paddingRight + side;
	}).join('\n');

	return top + '\n' + middle + '\n' + bottom;
};

@@ -1612,16 +1612,32 @@
 x;%0A%09%7D;%0A%0A
+%09var NL = '%5Cn';%0A
 %09var lin
@@ -1652,20 +1652,18 @@
 t.split(
-'%5Cn'
+NL
 );%0A%0A%09if 
@@ -1897,16 +1897,17 @@
 .right;%0A
+%0A
 %09var hor
@@ -1987,36 +1987,34 @@
 order(repeating(
-'%5Cn'
+NL
 , margin.top) + 
@@ -2210,20 +2210,18 @@
 peating(
-'%5Cn'
+NL
 , margin
@@ -2552,20 +2552,18 @@
 %7D).join(
-'%5Cn'
+NL
 );%0A%0A%09ret
@@ -2572,20 +2572,18 @@
 n top + 
-'%5Cn'
+NL
  + middl
@@ -2586,20 +2586,18 @@
 iddle + 
-'%5Cn'
+NL
  + botto

Add and use a variable for newline character