前端工具库-Tool.js-v1.0

本文介绍了Tool.js1.0版本,一个JavaScript工具库,包含随机数生成、数组操作、浏览器检测、日期格式化、深拷贝、对象判断等实用函数。
摘要由CSDN通过智能技术生成

前段时间一直在写Tool.js1,没有写文章,非常抱歉
今天终于写完了2,里面有很多函数
tool-1.0.js

/* 
 * Tool.js - JavaScript Tools Library
 * version:1.0
 * Copyright (c) 2023 Robert L. Ryan
 * 
 * Date: 2023-12-20T14:00:00Z
 */
(function(global){
	var t = {} ;
	t.getRandom = function(upper, lower) {
		return Math.floor(Math.random() * (upper - lower + 1) + lower);
	};
	t.find = function(a, b) {
		return a.indexOf(b) + 1;
	};
	t.browser = navigator.userAgent.toLowerCase();
	t.toArray = function(data) { 
		return [].concat(data);
	};
	t.dataURLtoBlob = function(dataurl) {
		var arr = dataurl.split(',');
		var mime = arr[0].match(/:(.*?);/)[1];
		var bstr = atob(arr[1]);
		var n = bstr.length;
		var u8arr = new Uint8Array(n);
		while (n--) {
			u8arr[n] = bstr.charCodeAt(n);
		}
		return new Blob([u8arr], {type: mime});
	};
	t.formatDate = function(str, format) {
		if (!str) return '';
		var date = new Date(str);
		var map = {
			'': date.getFullYear(),
			'M': date.getMonth() + 1,
			'd': date.getDate(),
			'h': date.getHours(),
			'm': date.getMinutes(),
			's': date.getSeconds(),
			'q': Math.floor((date.getMonth() + 3) / 3),
			'S': date.getMilliseconds()
		};
		format = format.replace(/([yMdhmsqS])+/g,function(all, t) {
			var v = map[t];
			if (v !== undefined) {
				if (all.length > 1) {
					v = '0' + v;
					v = v.substr(v.length - 2);
				};
				return v;
			} else if (t === 'y') {
				return (date.getFullYear() + '').substr(4 - all.length);
			};
			return all;
		});
		return format + '';
	};
	t.deepCopy = function(source) {
		if (!isObject(source)) return source;
		var target = Array.isArray(source) ? [] : {};
		for (var k in source) {
			if (source.hasOwnProperty(k)) {
				if (typeof source[k] === 'object') {
					target[k] = deepCopy(source[k]);
				} else {
					target[k] = source[k];
				};
			};
		};
		return target;
	};
	t.isObject = function(obj) {
		return typeof obj === 'object' && obj !== null;
	};
	t.formatFloat = function(f, digit) {
		digit = digit || 2
		var m = Math.pow(10, digit);
		return (parseFloat(f * m, 10) / m).toFixed(digit);
	};
	
	t.sha1 = function (s) {
		function encodeUTF8(s) {
			var i, r = [],c,x;
			for (i = 0; i < s.length; i++) if ((c = s.charCodeAt(i)) < 0x80) r.push(c);
			else if (c < 0x800) r.push(0xC0 + (c >> 6 & 0x1F), 0x80 + (c & 0x3F));
			else {
				if ((x = c ^ 0xD800) >> 10 == 0) {
					c = (x << 10) + (s.charCodeAt(++i) ^ 0xDC00) + 0x10000;
					r.push(0xF0 + (c >> 18 & 0x7), 0x80 + (c >> 12 & 0x3F));
				} else {
					r.push(0xE0 + (c >> 12 & 0xF));
					r.push(0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
				};
			};
			return r;
		};
		var data = new Uint8Array(encodeUTF8(s));
		var i, j, t;
		var l = ((data.length + 8) >>> 6 << 4) + 16, s = new Uint8Array(l << 2);
		s.set(new Uint8Array(data.buffer));
		s = new Uint32Array(s.buffer);
		for (t = new DataView(s.buffer), i = 0; i < l; i++) s[i] = t.getUint32(i << 2);
		s[data.length >> 2] |= 0x80 << (24 - (data.length & 3) * 8);
		s[l - 1] = data.length << 3;
		var w = [], f = [function() {
			return m[1] & m[2] | ~m[1] & m[3];
		},
		function() {
			return m[1] ^ m[2] ^ m[3];
		},
		function() {
			return m[1] & m[2] | m[1] & m[3] | m[2] & m[3];
		},
		function() {
			return m[1] ^ m[2] ^ m[3];
		}], rol = function(n, c) {
			return n << c | n >>> (32 - c);
		}, k = [1518500249, 1859775393, -1894007588, -899497514], m = [1732584193, -271733879, null, null, -1009589776];
		m[2] = ~m[0], m[3] = ~m[1];
		for (i = 0; i < s.length; i += 16) {
			var o = m.slice(0);
			for (j = 0; j < 80; j++) w[j] = j < 16 ? s[i + j] : rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1),
			t = rol(m[0], 5) + f[j / 20 | 0]() + m[4] + w[j] + k[j / 20 | 0] | 0,
			m[1] = rol(m[1], 30);
			m.pop();
			m.unshift(t);
			for (j = 0; j < 5; j++) m[j] = m[j] + o[j] | 0;
		};
		t = new DataView(new Uint32Array(m).buffer);
		for (var i = 0; i < 5; i++) m[i] = t.getUint32(i << 2);
		var hex = Array.prototype.map.call(new Uint8Array(new Uint32Array(m).buffer),
		function(e) {
			return (e < 16 ? "0": "") + e.toString(16);
		}).join("");
		return hex;
	}
	global.tool = t;
} )( typeof window !== "undefined" ? window : this )

使用UglifyJS压缩之后
tool-1.0.min.js

/* Tool.js v1.0 | Copyright (c) 2023 Robert Rain */
!function(t){var n={getRandom:function(t,n){return Math.floor(Math.random()*(t-n+1)+n)},find:function(t,n){return t.indexOf(n)+1}};n.browser=navigator.userAgent.toLowerCase(),n.toArray=function(t){return[].concat(t)},n.dataURLtoBlob=function(t){for(var n=t.split(","),r=n[0].match(/:(.*?);/)[1],e=atob(n[1]),o=e.length,a=new Uint8Array(o);o--;)a[o]=e.charCodeAt(o);return new Blob([a],{type:r})},n.formatDate=function(t,n){if(!t)return"";var r=new Date(t),e={"":r.getFullYear(),M:r.getMonth()+1,d:r.getDate(),h:r.getHours(),m:r.getMinutes(),s:r.getSeconds(),q:Math.floor((r.getMonth()+3)/3),S:r.getMilliseconds()};return(n=n.replace(/([yMdhmsqS])+/g,function(t,n){var o=e[n];return void 0!==o?(t.length>1&&(o=(o="0"+o).substr(o.length-2)),o):"y"===n?(r.getFullYear()+"").substr(4-t.length):t}))+""},n.deepCopy=function(t){if(!isObject(t))return t;var n=Array.isArray(t)?[]:{};for(var r in t)t.hasOwnProperty(r)&&("object"==typeof t[r]?n[r]=deepCopy(t[r]):n[r]=t[r]);return n},n.isObject=function(t){return"object"==typeof t&&null!==t},n.formatFloat=function(t,n){n=n||2;var r=Math.pow(10,n);return(parseFloat(t*r,10)/r).toFixed(n)},n.sha1=function(t){var n,r,e=new Uint8Array(function(t){var n,r,e,o=[];for(n=0;n<t.length;n++)(r=t.charCodeAt(n))<128?o.push(r):r<2048?o.push(192+(r>>6&31),128+(63&r)):(e=55296^r)>>10==0?(r=(e<<10)+(56320^t.charCodeAt(++n))+65536,o.push(240+(r>>18&7),128+(r>>12&63))):(o.push(224+(r>>12&15)),o.push(128+(r>>6&63),128+(63&r)));return o}(t)),o=16+(e.length+8>>>6<<4);for((t=new Uint8Array(o<<2)).set(new Uint8Array(e.buffer)),t=new Uint32Array(t.buffer),r=new DataView(t.buffer),h=0;h<o;h++)t[h]=r.getUint32(h<<2);t[e.length>>2]|=128<<24-8*(3&e.length),t[o-1]=e.length<<3;var a=[],u=[function(){return l[1]&l[2]|~l[1]&l[3]},function(){return l[1]^l[2]^l[3]},function(){return l[1]&l[2]|l[1]&l[3]|l[2]&l[3]},function(){return l[1]^l[2]^l[3]}],i=function(t,n){return t<<n|t>>>32-n},f=[1518500249,1859775393,-1894007588,-899497514],l=[1732584193,-271733879,null,null,-1009589776];for(l[2]=~l[0],l[3]=~l[1],h=0;h<t.length;h+=16){var c=l.slice(0);for(n=0;n<80;n++)a[n]=n<16?t[h+n]:i(a[n-3]^a[n-8]^a[n-14]^a[n-16],1),r=i(l[0],5)+u[n/20|0]()+l[4]+a[n]+f[n/20|0]|0,l[1]=i(l[1],30);for(l.pop(),l.unshift(r),n=0;n<5;n++)l[n]=l[n]+c[n]|0}r=new DataView(new Uint32Array(l).buffer);for(var h=0;h<5;h++)l[h]=r.getUint32(h<<2);return Array.prototype.map.call(new Uint8Array(new Uint32Array(l).buffer),function(t){return(t<16?"0":"")+t.toString(16)}).join("")},t.tool=n}("undefined"!=typeof window?window:this);

后续会继续写,下一个版本1.1a还会多一些其他的函数


  1. 注意,Robert Ryan是我的英文名,不是别人,我没有抄袭 ↩︎

  2. 上次写的有亿点点bug,重新修改了一下,把jQuery删了 ↩︎

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值