Used to work before JavaScript More , Summarize the tool class , Share with you , Please forgive me for the shortcomings !!

 1.  Array utility class (arrayUtils)
var arrayUtils = {};
(function (object) {
//arguments parameter
object.concat = function (arr) {
if (arguments.length > 0) {
for (i = 1;i < arguments.length; i++) {
arr=arr.concat(arguments[i]);
}
}
return arr;
};
object.join = function (arr,separator) {
return arr.join(separator);
};
object.push = function (arr) {
if (arguments.length > 0) {
for (i = 1;i < arguments.length; i++) {
arr.push(arguments[i]);
}
}
return arr;
};
object.slice = function (arr,start,end) {
return arr.slice(start,end);
};
object.sort = function (arr,isDesc) {
if(isDesc){
arr.sort(function (a,b) {
return a<b?1:-1;
});
}else{
arr.sort(function (a,b) {
return a<b?-1:1;
});
}
return arr;
};
object.unshift = function (arr) {
if (arguments.length > 0) {
for (i = 1;i < arguments.length; i++) {
arr.unshift(arguments[i]);
}
}
return arr;
};
object.inArray=function (arr,targetValue) {
var hasTargetValue=false;
$.each(arr,function (index,item) {
if(item==targetValue){
hasTargetValue=true;
}
});
return hasTargetValue;
};
// Difference set
object.except=function (sourceArray,targetArray) {
var self=this;
var exceptArray=[];
$.each(sourceArray,function (index,item) {
if(!self.inArray(targetArray,item)){
exceptArray.push(item);
}
});
$.each(targetArray,function (index,item) {
if(!self.inArray(sourceArray,item)){
exceptArray.push(item);
}
});
return exceptArray;
};
// intersection
object.intersect=function (sourceArray,targetArray) {
var self=this;
var intersectArray=[];
$.each(sourceArray,function (index,item) {
if(self.inArray(targetArray,item)){
intersectArray.push(item);
}
});
return intersectArray;
};
})(arrayUtils); 2.Cookie Tool class (cookieUtils)
var cookieUtils={};
(function (object) {
object.setCookie=function (key,value,expires) {
$.cookie(key, JSON.stringify(data), { expires: expires, path: '/' });
};
object.getCookie=function (key) {
return $.cookie(key);
};
object.getServerCookie=function (cookieName,key) {
var cookieValue = $.cookie(cookieName);
var cookieItems = cookieValue.split('&');
var itemValue = '';
$.each(cookieItems, function (index, item) {
if (item.indexOf(key) > -1) {
itemValue = item.match(/=.*/g)[0].replace(/=/, '');
}
});
return itemValue;
};
object.clearCookie=function (key,expires) {
$.cookie(key, null, { expires: expires, path: '/' });
};
})(cookieUtils); 3. Date tool class (dateTimeUtils)
/**
* Created by JerryChen on 2016/12/30.
*/
var dateTimeUtils = {};
(function (object) {
object.now = function () {
return new Date();
};
object.toString = function (value, format) {
var returnValue = '';
var placeHolder = '00';
var separator = '';
var formatArray = ['y', 'M', 'd', 'h', 'm', 's'];
var yearFormat = format.match(/y+/g);
var formatLength = 0;
var isContains = false;
if (yearFormat != null && yearFormat[0].length <= 4) {
formatLength += yearFormat[0].length;
var yearValue = value.getFullYear().toString();
separator = format.substr(formatLength, 1);
isContains = inArray(formatArray, separator);
if (isContains) {
separator = "";
} else {
formatLength += 1;
}
returnValue += yearValue.substr(4 - yearFormat[0].length, yearFormat[0].length) + separator;
}
var monthFormat = format.match(/M+/g);
if (monthFormat != null && monthFormat[0].length <= 2) {
formatLength += monthFormat[0].length;
var monthValue = (value.getMonth() + 1).toString();
separator = format.substr(formatLength, 1);
isContains = inArray(formatArray, separator);
if (isContains) {
separator = "";
} else {
formatLength += 1;
}
returnValue += placeHolder.substr(0, 2 - monthFormat[0].length) + monthValue + separator;
}
var dayFormat = format.match(/d+/g);
if (dayFormat != null && dayFormat[0].length <= 2) {
formatLength += dayFormat[0].length;
var dayValue = value.getDate().toString();
separator = format.substr(formatLength, 1);
isContains = inArray(formatArray, separator);
if (isContains) {
separator = " ";
} else {
formatLength += 1;
}
returnValue += placeHolder.substr(0, 2 - dayFormat[0].length) + dayValue + separator;
}
var hourFormat = format.match(/h+/g);
if (hourFormat != null && hourFormat[0].length <= 2) {
formatLength += hourFormat[0].length;
var hourValue = value.getHours().toString();
separator = format.substr(formatLength, 1);
isContains = inArray(formatArray, separator);
if (isContains) {
separator = "";
} else {
formatLength += 1;
}
returnValue += placeHolder.substr(0, 2 - hourFormat[0].length) + hourValue + separator;
}
var minutesFormat = format.match(/m+/g);
if (minutesFormat != null && minutesFormat[0].length <= 2) {
formatLength += minutesFormat[0].length;
var minutesValue = value.getMinutes().toString();
separator = format.substr(formatLength, 1);
isContains = inArray(formatArray, separator);
if (isContains) {
separator = "";
} else {
formatLength += 1;
}
returnValue += placeHolder.substr(0, 2 - minutesFormat[0].length) + minutesValue + separator;
}
secondsFormat = format.match(/s+/g);
if (secondsFormat != null && secondsFormat[0].length <= 2) {
var secondsValue = value.getSeconds();
returnValue += placeHolder.substr(0, 2 - secondsFormat[0].length) + secondsValue;
}
return returnValue;
};
function inArray(array, targetValue) {
var flag = false;
$.each(array, function (index, item) {
if (targetValue == item) {
flag = true;
}
});
return flag;
}
object.dateDiff = function (startDate, endDate, format) {
if (format == "dd") {
return (endDate.getTime()-startDate.getTime())/86400000;
} else if (format == "hh") {
return Math.ceil((endDate.getTime()-startDate.getTime())/3600000);
} else if (format == "mm") {
return Math.ceil((endDate.getTime()-startDate.getTime())/60000);
} else if (format == "ss") {
return Math.ceil((endDate.getTime()-startDate.getTime())/1000);
}
};
object.addDays = function (value, days) {
return new Date(value.getTime() + days * 86400000);
};
object.addHours = function (value, hours) {
return new Date(value.getTime() + hours * 3600000);
};
object.addMinutes = function (value, minutes) {
return new Date(value.getTime() + minutes * 60000);
};
object.addSeconds = function (value, seconds) {
return new Date(value.getTime() + seconds * 1000);
};
})(dateTimeUtils); 4. Enumerate utility classes (enumUtils)
/**
* Created by JerryChen on 2016/12/31.
*/
var enumUtils={};
(function (object) {
object.getEnumValue=function (enumSource,key) {
return enumSource[key];
};
object.getEnumData=function (enumSource) {
var data=[];
for (item in enumSource){
data.push({"Key":item,"Value":enumSource[item]})
}
return data;
}; })(enumUtils); 5.Json Tool class (jsonUtils)
/**
* Created by JerryChen on 2016/12/31.
*/
var jsonUtils={};
(function (object) {
object.grepJsonArray=function (dataSource,filterRule) {
//var defaultRule=[{"FilterField":"","Value":"","Operator":""}];
if(filterRule!=undefined&&filterRule!=null&&filterRule.length>0){
$.each(filterRule,function (index,item) {
if (item.Operator=="="){
dataSource= $.grep(dataSource, function (item, index) {
return item.FilterField == item.Value;
}, false);
}
if (item.Operator=="!="){
dataSource= $.grep(dataSource, function (item, index) {
return item.FilterField != item.Value;
}, false);
}
});
}
return dataSource;
};
/**
* @return {boolean}
*/
object.isNotNullOrEmpty=function (dataSource) {
return dataSource!=undefined&&dataSource!=null&&dataSource.length>0; };
object.isNotEmptyObject=function (dataSource) {
return !$.isEmptyObject(dataSource);
};
object.getObjectProperty=function (dataSource,propertyName) {
return dataSource[propertyName];
};
object.removeObjectProperty=function (dataSource,propertyName) {
delete dataSource[propertyName];
return dataSource;
} })(jsonUtils); 6.Mapper Tool class (mapperUtils)
/**
* Created by JerryChen on 2016/12/31.
*/
var mapperUtils={};
(function (object) {
object.extend=function (dataSource,dataTarget) {
$.extend(true,dataTarget,dataSource);
return dataTarget;
};
})(mapperUtils); 7.Number Tool class (numberUtils)
/**
* Created by JerryChen on 2016/12/30.
*/
var numberUtils = {};
(function (object) {
object.isNumber = function (value) {
if (/\d+/.test(value)) {
return true;
}
return false;
};
object.toInt = function (value) {
if (this.isNumber(value)) {
return parseInt(value);
} else {
return value;
}
};
object.toDouble = function (value, number) {
return value.toFixed(number);
};
object.toFloat = function (value) {
return parseFloat(value);
};
object.max = function () {
var maxValue;
if (arguments.length > 0) {
if (arguments.length == 1) {
arguments[0].sort(function (a, b) {
return a > b ? -1 : 1;
});
maxValue = arguments[0][0];
} else {
var arr=[];
for(i=0;i<arguments.length;i++){
arr.push(arguments[i]);
}
arr.sort(function (a, b) {
return a > b ? -1 : 1;
});
maxValue = arr[0];
}
}
return maxValue;
};
object.min = function () {
var minValue;
if (arguments.length > 0) {
if (arguments.length == 1) {
arguments[0].sort(function (a, b) {
return a > b ? 1 : -1;
});
minValue = arguments[0][0];
} else {
var arr=[];
for(i=0;i<arguments.length;i++){
arr.push(arguments[0]);
}
arr.sort(function (a, b) {
return a > b ? 1 : -1;
});
maxValue = arr[0];
}
}
return minValue;
}
})(numberUtils); 8.String Tool class (stringUtils)
/**
* Created by JerryChen on 2016/12/30.
*/
var stringUtils={};
(function (object) {
object.subString=function (value,start,length) {
return value.substr(start,length);
};
object.truncate=function (value,maxLength,suffix) {
if(value!=undefined&&value.length>maxLength){
value=value.substr(0,maxLength)+suffix;
}
return value;
};
object.length=function (value) {
if(value!=undefined){
return value.length;
}else {
return 0;
}
};
object.endTrim=function (value) {
if(value!=undefined){
value=value.substr(0,value.length-1);
}
return value;
};
object.isNotNull=function (value) {
return value!=undefined&&value!=null;
};
object.isNotNullOrEmpty=function (value) {
return value!=undefined&&value!=null&&value!="";
};
object.isEqual=function (value,compareWith) {
return value==compareWith;
};
object.indexOf=function (value,targetValue) {
return value.indexOf(targetValue);
}
})(stringUtils); 9.Url Tool class (urlUtils)
/**
* Created by JerryChen on 2016/12/31.
*/
var urlUtils={};
(function (object) {
object.getParam= function (search,paramName) {
var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)");
var result = search.substr(1).match(reg);
if (result != null) {
return decodeURIComponent(result[2]);
} else {
return undefined;
}
};
object.getHost=function () {
return window.location.host;
}
})(urlUtils); 10.StringBuilder
function StringBuilder() {
this._string = new Array();
};
StringBuilder.prototype.append = function (str) {
this._string.push(str);
return this;
};
StringBuilder.prototype.appendLine = function (str) {
this._string.push(str + "\n");
return this;
};
StringBuilder.prototype.appendFormat = function (value) {
for (var i = 1; i < arguments.length; i++) {
var placeHolder = "\\{" + (i - 1) + "\\}";
var reg = new RegExp(placeHolder, "g");
value = value.replace(reg, arguments[i]);
}
this._string.push(value);
return this;
};
StringBuilder.prototype.toString = function () {
return this._string.join("");
}
 

github Address :https://github.com/jerrychen0705/JS.Utilities

JS More articles on tools

  1. Share very useful front-end pagination js Tool class flexible Simple and easy to understand

    Share your encapsulated front-end pagination js Tool class   Here is a screenshot of the default style effect Can be changed at will js And css Very flexible /** * pageSize, Number of displays per page * pageIndex, The current number of pages * pageCount total ...

  2. Rhino+envjs-1.2.js stay java Run the website js Tool class

    java The crawler encountered something encrypted on the page , I found some materials to study Made a java function js Tool class of , I hope you found that useful , It uses client( obtain js) You can change it into your own client. Mainly used Rhino Namely Java ...

  3. js Encapsulation of tool class

    common.js Native js Most of the tools and methods implemented will be in common In file Layout rem.js,vue When developing , We just need to put rem.js Again main.js in import Can be introduced (function(win, ...

  4. Some are generic js Tool class , Add custom plug-ins

    common_t.js /** * General tool components Encapsulate the original tools , User defined method for unified processing <br> * ^_^ * * Author: em.D * Date: 2016-05-17 * ...

  5. js Tools

    /********** Date processing function *********/<script type="text/javascript" src="${springMacroRequ ...

  6. js Tool class ---- Regular

    function(value){  if(value){   var reg=new RegExp("^[a-zA-Z0-9_-]+$");   return reg.test(v ...

  7. JS Tool class

    Encapsulates common operations in development And add some extension methods to call var util = { // obtain Url Parameters in ( No Chinese support ) getParams: function() { var url = location ...

  8. html The parameter js Tool class

    var QueryUtils = { GetQueryString: function (name) { var reg = new RegExp("(^|&)" + na ...

  9. js Time stamping yyyy-MM-dd HH-mm-ss Tool class

    Reprinted from :https://blog.csdn.net/shan1774965666/article/details/55049819 stay web In development , We often need to use js Turn the timestamp to yyyy-MM-dd H ...

Random recommendation

  1. ssl and https protocol

    from :https://cuiyongxiu.com/201102/24157.html ssl I won't talk more about the origin and history of the agreement , That's it. Netscape Netscape developed , Its main function is to provide a secure transmission ...

  2. PS Too big GIMP You can use

    Image processing Photoshop Use a lot of , But the software is too bloated , It's also very complicated to use , For general image processing , Is there any other tool to choose from ? GIMP yes GNU Image Manipulation Progra ...

  3. crontab Timed operation git command Update the code base

    Q:  http://stackoverflow.com/questions/7994663/git-push-via-cron    I'm trying to run a git push fro ...

  4. Bootstrap course

    Bootstrap course Bootstrap course

  5. bzoj3131

    This is a very good and boring comprehensive problem …… First of all, we must put f(i) There's no doubt about it We ask that the number product be now The number of , The first is about space It's going to blow up space , It's not hard to find out The prime factor of the product is only 2,3,5,7, also ...

  6. lol The main content of the box

    //AFN function  - imageview Load web images and get images , Get it and store it on your phone   [image setImageWithURLRequest:[NSURLRequest requestWithU ...

  7. java&#160; Realization Excel Compressed into Zip export

    1 summary stay web A common scenario in a project is to export a file as Excel, But when you need to export multiple Excel when , Users will operate frequently , This seriously reduces the friendly interaction and ease of use of the project , So how can we solve this problem gracefully ? The author today ...

  8. On java Some of the basic problems of ( Part 1 )

    To make a problem difficult , There are basically two ways , Extreme refinement and high abstraction . The study of any language , A good foundation is essential , This article , It's going to be extremely detailed To parse a few java Fundamental problems in , These problems are also the weakness of most programmers ...

  9. win10 Activate tool Re-LoaderByR@1n.exe

    Name is Re-LoaderByR@1n.exe No advertisement , https://pan.baidu.com/s/1MBix1cZxKpUTA6MUgL7uLQ If it becomes the education version , You can use the following activation code to turn it into a professional version :W ...

  10. asp.net Realize the way of background asynchronous processing

    Problem description : Just developed a page , The use of NET Of MVC4.5 frame , Because of the slow loading of page data, it needs to be optimized , On the page 8 A request , But requests are not interdependent . Front end use ajax asynchronous , But the background doesn't follow the asynchronous program . Explain ...