SoFunction
Updated on 2025-04-10

Common JavaScript function library based on prototype extension


/**
2 * Retrieve array elements (prototype extension or overload)
3 * @param {o} The retrieved element value
4 * @type int
5 * @returns Element Index
6 */
7 = function(o) {
8 var index = -1;
9 for(var i=0;i<;i++){if(this[i]==o){index = i;break;}}
return index;
}

/**
* Date formatting (prototype extension or overloading)
* Format YYYY/yyyy/YY/yy indicates year
* MM/M month
* W/w Week
* dd/DD/d/D Date
* hh/HH/h/H Time
* mm/m minute
*ss/SS/s/S seconds
* @param {formatStr} format template
* @type string
* @returns Date String
*/
= function(formatStr){
var str = formatStr;
var Week = ['day','one','two','three','four','five','six'];
str=(/yyyy|YYYY/,());
str=(/yy|YY/,(() % 100)>9?(() % 100).toString():'0' + (() % 100));
str=(/MM/,(()+1)>9?(()+1).toString():'0' + (()+1));
str=(/M/g,());
str=(/w|W/g,Week[()]);
str=(/dd|DD/,()>9?().toString():'0' + ());
str=(/d|D/g,());
str=(/hh|HH/,()>9?().toString():'0' + ());
str=(/h|H/g,());
str=(/mm/,()>9?().toString():'0' + ());
str=(/m/g,());
str=(/ss|SS/,()>9?().toString():'0' + ());
str=(/s|S/g,());
return str;
}

/**
* Comparison date difference (prototype extension or overload)
* @param {strInterval} Date type: 'y, m, d, h, n, s, w'
* @param {dtEnd} format is date type or valid date format string
* @type int
* @returns Compare results
*/
= function(strInterval, dtEnd) {
var dtStart = this;
if (typeof dtEnd == 'string' ) { // If it is a string converted to a date type
dtEnd = StringToDate(dtEnd);
}
switch (strInterval) {
case 's' :return parseInt((dtEnd - dtStart) / 1000);
case 'n' :return parseInt((dtEnd - dtStart) / 60000);
case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
case 'm' :return (()+1)+((()-())*12) - (()+1);
case 'y' :return () - ();
}
}

/**
* Date calculation (prototype extension or overload)
* @param {strInterval} Date type: 'y, m, d, h, n, s, w'
* @param {Number} Quantity
* @type Date
* @returns Calculated date
*/
= function(strInterval, Number) {
var dtTmp = this;
switch (strInterval) {
case 's' :return new Date((dtTmp) + (1000 * Number));
case 'n' :return new Date((dtTmp) + (60000 * Number));
case 'h' :return new Date((dtTmp) + (3600000 * Number));
case 'd' :return new Date((dtTmp) + (86400000 * Number));
case 'w' :return new Date((dtTmp) + ((86400000 * 7) * Number));
case 'q' :return new Date((), (()) + Number*3, (), (), (), ());
case 'm' :return new Date((), (()) + Number, (), (), (), ());
case 'y' :return new Date((() + Number), (), (), (), (), ());
}
}

/**
* Get date data information (prototype extension or reload)
* @param {interval} Date type: 'y, m, d, h, n, s, w'
* @type int
* @returns The specified date part
*/
= function(interval){
var myDate = this;
var partStr='';
var Week = ['day','one','two','three','four','five','six'];
switch (interval)
{
case 'y' :partStr = ();break;
case 'm' :partStr = ()+1;break;
case 'd' :partStr = ();break;
case 'w' :partStr = Week[()];break;
case 'ww' :partStr = ();break;
case 'h' :partStr = ();break;
case 'n' :partStr = ();break;
case 's' :partStr = ();break;
}
return partStr;
}

/**
* Split dates into arrays (prototype extension or overload)
* @type array
* @returns Date array
*/
= function() {
var myDate = this;
var myArray = Array();
myArray[0] = ();
myArray[1] = ()+1;
myArray[2] = ();
myArray[3] = ();
myArray[4] = ();
myArray[5] = ();
return myArray;
}

/**
* Get the number of days in the current month (prototype extension or reload)
* @type int
* @returns days
*/
= function(){
var myDate = this;
var ary = ();
var date1 = (new Date(ary[0],ary[1]+1,1));
var date2 = ('m',1);
var result = daysDiff(('yyyy-MM-dd'),('yyyy-MM-dd'));
return result;
}

/**
* Judge leap years (prototype extension or overload)
* @type boolean
* @returns is a leap year true/false
*/
= function() {
return (0==()%4&&((()%100!=0)||(()%400==0)));
}

/**
* Comparison of the difference in the number of days between two dates (custom)
* @param {DateOne} Date Monday
* @param {DateOne} Date Tuesday
* @type int
* @returns Compare results
*/
function daysDiff(DateOne,DateTwo)
{
var OneMonth = (5, ('-'));
var OneDay = (, ('-')+1);
var OneYear = (0, ('-'));

var TwoMonth = (5, ('-'));
var TwoDay = (, ('-')+1);
var TwoYear = (0, ('-'));

var cha=(((OneMonth+'/'+OneDay+'/'+OneYear)- (TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
return (cha);
}

/**
* Date calculation (customized)
* @param {strInterval} Date type: 'y, m, d, h, n, s, w'
* @param {Number} Quantity
* @param {prmDate} Original date
* @type Date
* @returns Calculated date
*/
function dateAdd(interval,number,prmDate){
number = parseInt(number);
if (typeof(prmDate)=="string"){
prmDate = (/\D/);
--prmDate[1];
eval("var prmDate = new Date("+(",")+")");
}
if (typeof(prmDate)=="object"){
var prmDate = prmDate
}
switch(interval){
case "y": (()+number); break;
case "m": (()+number); break;
case "d": (()+number); break;
case "w": (()+7*number); break;
case "h": (()+number); break;
case "n": (()+number); break;
case "s": (()+number); break;
case "l": (()+number); break;
}
return prmDate;
}

/**
* Get string length (prototype extension or overload)
* @type int
* @returns string length
*/
= function() {
var arr=(/[^\x00-\xff]/ig);
return +(arr==null?0:);
}

/**
* The string takes the specified number of characters (prototype extension or overload)
* @param {num} Get the number
* @type string
* @returns matching string
*/
= function(num,mode) {
if(!/\d+/.test(num)) return(this);
var str = (0,num);
if(!mode) return str;
var n = () - ;
num = num - parseInt(n/2);
return (0,num);
}

/**
* The string takes the specified number of characters (prototype extension or overload)
* @param {num} Get the number
* @type string
* @returns matching string
*/
= function(num,mode) {
if(!/\d+/.test(num)) return(this);
var str = (-num);
if(!mode) return str;
var n = () - ;
num = num - parseInt(n/2);
return (-num);
}

/**
* String contains (prototype extension or overload)
* @param {string: str} Substring to search
* @param {bool: mode} Whether to ignore case
* @type int
* @returns The number of matches
*/
= function(str,mode) {
return eval("(/("+str+")/g"+(mode?"i":"")+").length");
}

/**
* Remove left and right spaces (prototype extension or overload)
* @type string
* @returns Processed string
*/
= function() {
return (/(^\s*)|(\s*$)/g,"");
}

/**
* Remove left space (prototype extension or overload)
* @type string
* @returns Processed string
*/
= function() {
return (/(^\s*)/g, "");
}

/**
* Remove right space (prototype extension or overload)
* @type string
* @returns Processed string
*/
= function() {
return (/(\s*$)/g, "");
}

/**
* Convert string to date type (prototype extension or overload)
* @type Date
* @returns date
*/
= function() {
var converted = (this);
var myDate = new Date(converted);
if (isNaN(myDate)) {var arys= ('-'); myDate = new Date(arys[0],--arys[1],arys[2]); }
return myDate;
}