"use strict";

/**
 * Trims given characters of both ends of a string
 * 
 */

String.prototype.trim = function (charlist) {
	//  discuss at: http://phpjs.org/functions/trim/
	// original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// improved by: mdsjack (http://www.mdsjack.bo.it)
	// improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
	// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// improved by: Steven Levithan (http://blog.stevenlevithan.com)
	// improved by: Jack
	//    input by: Erkekjetter
	//    input by: DxGx
	// bugfixed by: Onno Marsman
	//   example 1: trim('    Kevin van Zonneveld    ');
	//   returns 1: 'Kevin van Zonneveld'
	//   example 2: trim('Hello World', 'Hdle');
	//   returns 2: 'o Wor'
	//   example 3: trim(16, 1);
	//   returns 3: 6

	var whitespace,
	    l = 0,
	    i = 0;
	var str = this;

	if (!charlist) {
		// default list
		whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	} else {
		// preg_quote custom list
		charlist += '';
		whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
	}

	l = str.length;
	for (i = 0; i < l; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}

	l = str.length;
	for (i = l - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}

	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
};

String.prototype.rtrim = function (char) {
	var regex = new RegExp(char + '+$', 'i');
	return this.replace(regex, '');
};

/**
 * Makes the first character upper case
 * 
 */
String.prototype.upperCaseFirst = function () {
	return this.charAt(0).toUpperCase() + this.slice(1);
};

/**
 * Makes the first character lower case
 * 
 */
String.prototype.lowerCaseFirst = function () {
	return this.charAt(0).toLowerCase() + this.slice(1);
};

/**
 * Encode a string as base 64
 * 
 */
String.prototype.base64Encode = function () {
	return new Buffer(this.toString()).toString('base64');
};

/**
 * Decode a string from base 64
 * 
 */
String.prototype.base64Decode = function () {
	return new Buffer(this.toString(), 'base64').toString('ascii');
};

/**
 * Split a string, not including empty items
 * 
 */
String.prototype.splitNoEmpties = function (char) {
	let parts = this.split(char);
	for (let i = parts.length; i >= 0; i--) {
		if (!parts[i]) {
			parts.splice(i);
		}
	}
	return parts;
};

/**
 * Convert to spaces
 * 
 */
String.prototype.toSpaces = function () {
	return normalize(this).join(' ');
};

/**
 * Convert to camel case
 * 
 */
String.prototype.toCamelCase = function () {
	let parts = normalize(this);
	for (let [i, part] of parts.entries()) {
		parts[i] = part.charAt(0).toUpperCase() + part.slice(1);
	}
	return parts.join();
};

/**
 * Convert to dasherized label
 * 
 */
String.prototype.toDashes = function () {
	return normalize(this).join('-');
};

/**
 * Convert to Titlecase
 * 
 */
String.prototype.toTitleCase = function () {
	return this.replace(/\w\S*/g, function (txt) {
		return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
	});
};

/**
 * Normalises a multi-word string into an array of strings (i.e. camelcase, dasherized, etc.)
 * Relies on splitNoEmpties() function also added to the String object prototype
 */
function normalize(str) {

	if (str.indexOf(' ') >= 0) {
		// ------------------------------ Is spaces
		return str.splitNoEmpties(' ');
	} else if (str.indexOf('_') >= 0) {
		// ------------------------------ Is underscores
		return str.splitNoEmpties('_');
	} else if (str.indexOf('-') >= 0) {
		// ------------------------------ Is dashes
		return str.splitNoEmpties('-');
	} else {
		// ------------------------------ Assume camelcase
		return str.replace(/([A-Z])/g, " $1").splitNoEmpties(' ');
	}
}

/**
 * Convert to check if vowel
 * 
 */
String.prototype.isVowel = function () {
	let c = this.charAt(0);
	return ['a', 'e', 'i', 'o', 'u'].includes(c.toLowerCase());
};

/**
 * Convert to titlecase
 * 
 */
String.prototype.aOrAn = function () {
	return this.isVowel() ? 'an' : 'a';
};

/**
 * Convert to singular as best we can
 * 
 */
String.prototype.singular = function () {
	if (this.substr(-3) === 'ies') {
		return this.substr(0, this.length - 3) + 'y';
	} else {
		return this.rtrim('s');
	}
};
//# sourceMappingURL=string.js.map