/** 字符扩展 */

export default () => {

	//#region Base64加解密
	// 将标准base64中的等号换成减号，用于网址
	var r = '-',
		n = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

	function t(t, i) {
		var r = n.indexOf(t.charAt(i));
		if (r === -1) throw 'Cannot decode';
		return r
	};

	function i(n, t) {
		var i = n.charCodeAt(t);
		if (i > 255) throw 'INVALID_CHARACTER_ERR: DOM Exception 5';
		return i
	};

	function encode(t) {
		t = String(t);
		var f, u, e = [],
			o = t.length - t.length % 3;
		if (t.length === 0) return t;
		for (f = 0; f < o; f += 3) u = i(t, f) << 16 | i(t, f + 1) << 8 | i(t, f + 2), e.push(n.charAt(u >> 18)), e.push(n.charAt(u >> 12 & 63)), e.push(n.charAt(u >> 6 & 63)), e.push(n.charAt(u & 63));
		switch (t.length - o) {
			case 1:
				u = i(t, f) << 16;
				e.push(n.charAt(u >> 18) + n.charAt(u >> 12 & 63) + r + r);
				break;
			case 2:
				u = i(t, f) << 16 | i(t, f + 1) << 8;
				e.push(n.charAt(u >> 18) + n.charAt(u >> 12 & 63) + n.charAt(u >> 6 & 63) + r);
		};
		return e.join('');
	};

	function decode(n) {
		n = String(n);
		var o = 0,
			i, u, f = n.length,
			e = [];
		if (n = String(n), f === 0) return n;
		if (f % 4 != 0) throw 'Cannot decode';
		for (n.charAt(f - 1) === r && (o = 1, n.charAt(f - 2) === r && (o = 2), f -= 4), i = 0; i < f; i += 4) u = t(n, i) << 18 | t(n, i + 1) << 12 | t(n, i + 2) << 6 | t(n, i + 3), e.push(String.fromCharCode(u >> 16, u >> 8 & 255, u & 255));
		switch (o) {
			case 1:
				u = t(n, i) << 18 | t(n, i + 1) << 12 | t(n, i + 2) << 6;
				e.push(String.fromCharCode(u >> 16, u >> 8 & 255));
				break;
			case 2:
				u = t(n, i) << 18 | t(n, i + 1) << 12;
				e.push(String.fromCharCode(u >> 16));
		};
		return e.join('');
	};
	//#endregion
	
	if (!String.prototype.format) {
		/**
		 * 格式化字符串
		 * @param {Stringp[]} args 要格式化的替换值数组
		 * @return {String} 格式化结果 
		 * @example '一二三{0}五，上山打{1}'.format('四','老虎') 结果：'一二三四五，上山打老虎'
		 */
		String.prototype.format = function (args) {
			let result = this;
			if (arguments.length > 0) {
				if (arguments.length == 1 && typeof (args) == 'object') {
					for (let key in args) {
						if (args[key] != undefined) {
							let reg = new RegExp('({)' + key + '(})', 'g');
							result = result.replace(reg, args[key]);
						};
					};
				} else {
					for (let i = 0; i < arguments.length; i++) {
						if (arguments[i] != undefined) {
							let reg = new RegExp('({)' + i + '(})', 'g');
							result = result.replace(reg, arguments[i]);
						};
					};
				};
			};

			return result;
		};
	};

	if (!String.prototype.finance) {
		/**
		 * 转为财务表示法字符串
		 * @param {Number} n 保留小数位
		 * @return {String} 转换结果
		 * @example '12345678.18264'.finance(2) 结果：'12,345,678.18'
		 */
		String.prototype.finance = function (n = 2) {
			if (!Number(this)) return this;

			return (Number(this).toFix(n) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
		};
	};

	if (!String.prototype.unfinance) {
		/**
		 * 从财务表示法还原为数字
		 * @return {Number} 转换结果
		 * @example '12,345,678.18'.unfinance() 结果：12345678.18
		 */
		String.prototype.unfinance = function () {
			return Number(this.replace(/,/gi, ''));
		};
	};

	if (!String.prototype.utf8Encode) {
		/**
		 * utf8编码
		 * @return {String}
		 */
		String.prototype.utf8Encode = function () {
			return unescape(encodeURIComponent(this));
		};
	};

	if (!String.prototype.utf8Decode) {
		/**
		 * utf8解码
		 * @return {String}
		 */
		String.prototype.utf8Decode = function () {
			try {
				return decodeURIComponent(escape(this));
			} catch (e) {
				return this;
			};
		};
	};

	if (!String.prototype.base64Encode) {
		/**
		 * 非标准Base64编码
		 * @return {String}
		 */
		String.prototype.base64Encode = function () {
			return encode(this.utf8Encode());
		};
	};

	if (!String.prototype.base64Decode) {
		/**
		 * 非标准Base64解码
		 * @return {String}
		 */
		String.prototype.base64Decode = function () {
			return decode(this).utf8Decode();
		};
	};

	if (!String.prototype.isEnStr) {
		/**
		 * 是否为加密字符串
		 * @return {Boolean}
		 */
		String.prototype.isEnStr = function () {
			return this.slice(0, 10) === 'Xm6Ek7ebm0';
		};
	};
};
