/**
 * 原型扩展库
 */
export default function () {

	Date.prototype.clone = function () {
		return new Date(this.valueOf());
	}

	function formatInt(str, split = ',') {
		const len = str.length;
		return len <= 3 ? str : formatInt(str.slice(0, len - 3), split) + split + str.slice(len - 3, len);
	}

	/**
	 * 使用split分割格式化数字字符
	 * @param {*} digits 
	 * @param {*} split 
	 */
	Number.prototype.format = function (digits = 2, split = ',') {
		if (isFinite(this)) {
			let str = this.toFixed(digits)
			if (digits < 1) {
				return formatInt(str, split);
			} else {
				let index = str.indexOf(".");
				return formatInt(str.slice(0, index), split) + str.slice(index);
			}
		} else {
			return "-";
		}
	}

	/**
	 * 将日期对象格式化成字符串
	 * @param {String} format 格式支持：yyyy-MM-dd HH:mm:ss.S 默认：yyyy-MM-dd
	 */
	Date.prototype.format = function (format) {
		format = format == null ? "yyyy-MM-dd" : format;
		let o = {
			"M+": this.getMonth() + 1, //month
			"d+": this.getDate(), //day
			"H+": this.getHours(), //hour
			"m+": this.getMinutes(), //minute
			"s+": this.getSeconds(), //second
			"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
			"S": this.getMilliseconds() //millisecond
		}

		if (/(y+)/.test(format))
			format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (var k in o)
			if (new RegExp("(" + k + ")").test(format))
				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k])
					.length));
		return format;
	}

	Number.prototype.toDate = function () {
		return new Date(this);
	}

	Number.prototype.toChina = function () {
		let number = this.toString();
		if (number.match(/\D/) || number.length >= 14) {
			throw new Error("只能是正整数,且不能超过14位");
		}
		let zhArray = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十']; // 数字对应中文
		let baseArray = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万']; //进位填充字符，第一位是 个位，可省略
		let string = String(number).split('').reverse().map((item, index) => { // 把数字切割成数组并倒序排列，然后进行遍历转成中文
			// 如果当前位为0，直接输出数字， 否则输出 数字 + 进位填充字符
			item = Number(item) == 0 ? zhArray[Number(item)] : zhArray[Number(item)] + baseArray[index];
			return item;
		}).reverse().join(''); // 倒叙回来数组，拼接成字符串
		string = string.replace(/^一十/, '十'); // 如果以 一十 开头，可省略一
		string = string.replace(/零+/, '零'); // 如果有多位相邻的零，只写一个即可
		return string;
	}

	Number.prototype.cny = function () {
		return this.toFixed(2).cny();
	}

	String.prototype.cny = function () {
		let n = this;
		if (n == 0) {
			return "零元整";
		}
		if (!/^(\+|-)?(0|[1-9]\d*)(\.\d+)?$/.test(n))
			return "数据非法";
		let unit = "仟佰拾亿仟佰拾万仟佰拾元角分", str = "";
		n += "00";
		let a = parseFloat(n);
		if (a < 0) {
			n = n.substr(1);
		}
		let p = n.indexOf('.');
		if (p >= 0) {
			n = n.substring(0, p) + n.substr(p + 1, 2);
		}

		unit = unit.substr(unit.length - n.length);

		for (var i = 0; i < n.length; i++)
			str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
		if (a > 0) {
			return str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");

		} else {

			return "负" + str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
		}

	}
	/**
	 * 将字符串格式化成日期对象
	 * @param {String} format 格式支持：yyyy-MM-dd HH:mm:ss.S 默认：yyyy-MM-dd
	 */
	String.prototype.toDate = function (format) {
		if (/^\d+$/.test(this)) {
			return new Date(Number(this));
		}
		format = format == null ? "yyyy-MM-dd" : format;
		let d = new Date(0);
		d.setMinutes(d.getTimezoneOffset());
		let o = {
			"y+": d.setFullYear, //year
			"M+": d.setMonth, //month
			"d+": d.setDate, //day
			"H+": d.setHours, //hour
			"m+": d.setMinutes, //minute
			"s+": d.setSeconds, //second
			"S+": d.setMilliSeconds //millisecond
		}
		let regx = format
		for (let k in o) {
			if (new RegExp("(" + k + ")").test(format))
				regx = regx.replace(new RegExp(k), "[0-9]+");
		}

		if (!new RegExp("^" + regx + "$").test(this)) {
			throw new Error("日期格式不正确:" + format);
		}

		for (var k in o) {
			if (new RegExp("(" + k + ")").test(format)) {
				regx = format;
				for (let temp in o) {
					if (temp == k) {
						regx = regx.replace(new RegExp(temp), "([0-9]+)");
					} else {
						regx = regx.replace(new RegExp(temp), "[0-9]+");
					}

				}
				let val = Number(this.replace(new RegExp("^" + regx + "$"), "$1"));
				if (k == "M+") {
					--val;
				}
				Reflect.apply(o[k], d, [val])
			}
		}
		return d;
	}

	String.prototype.toTime = function () {
		if (!/^(\d{2,}):(\d{2}):(\d{2})$/.test(this)) {
			throw new Error("含时分秒的字符串需符合格式:(\\d{2,}):(\\d{2}):(\\d{2})");
		}
		let ts = this.split(":");
		return parseInt(ts[0]) * 3600 + parseInt(ts[1]) * 60 + parseInt(ts[2]);
		// return eval(this.replace(/^(\d{2}):(\d{2}):(\d{2})$/, "$1*3600+$2*60+$3"));
	}

	Number.prototype.toTime = function () {
		let ss = this % 60;
		let temp = (this - ss) / 60;
		let mm = temp % 60;
		let hh = (temp - mm) / 60;

		return `${hh.toString().padStart(2, "0")}:${mm.toString().padStart(2, "0")}:${ss.toString().padStart(2, "0")}`
	}


	Number.prototype.toFileSize = function () {
		let value = this;
		if (null == value || value == '') {
			return "0 Bytes";
		}
		var unitArr = new Array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
		var index = 0;
		var srcsize = parseFloat(value);
		index = Math.floor(Math.log(srcsize) / Math.log(1024));
		var size = srcsize / Math.pow(1024, index);
		size = size.toFixed(2);//保留的小数位数
		return size + unitArr[index];
	}
};