php 拓展推荐,微插件推荐系列:常用类型拓展

Array类型拓展

forEach

/** * @name 对数组中的每个元素在指定的上下文中执行给定的回调函数. * @param handler: 每个元素执行的句柄函数. * @param scope:函数执行的上下文. * @return undefined * */ add("forEach", function(handler, scope) { scope = scope || window; for( var i = 0; i < this.length; i++) handler.call(scope, this[i], i, this); });

every

/** * @name 判断所有的数组元素是否能通过测试 * @param handler: 每个元素执行的句柄函数. * @param scope:函数执行的上下文. * @return Boolean */ add("every", function(handler, scope) { scope = scope || window; for( var i = 0; i < this.length; i++) if( !handler.call(scope, this[i], i, this) ) return false; return true; });

some

/** * 判断数组中的至少有一个元素通过测试 * @param handler: 每个元素执行的句柄函数. * @param scope:函数执行的上下文. * @return Boolean */ add("some", function(handler, scope) { scope = scope || window; for( var i = 0; i < this.length; i++) if( handler.call(scope, this[i], i, this) ) return true; return false; });

map

/** * @name 对数组内的元素进行包装后返回新的数组 * @param handler: 每个元素执行的句柄函数. * @param scope:函数执行的上下文. * @type Array */ add("map", function(handler, scope) { scope = scope || window; var r = []; for( var i = 0; i < this.length; i++) r[r.length] = handler.call(scope, this[i], i, this); return r; });

filter

/** * @name : 过滤通过测试的数组元素,并返回所有通过测试的元素组成的数组 * @param handler: 每个元素执行的句柄函数. * @param scope:函数执行的上下文. * @return Array */ add("filter", function(handler, scope) { scope = scope || window; var r = []; for( var i = 0; i < this.length; i++) if( handler.call(scope, this[i], i, this) ) r[r.length] = this[i]; return r; });

indexOf

/** * 查找给定元素在指定数组中的位置 * @param subject:要查找的数组元素 * @param offset:数组元素开始的偏移量,即从第几个开始查找该元素 * @return Array */ add("indexOf", function(subject, offset) { for( var i = offset || 0; i < this.length; i++) if ( this[i] === subject ) return i; return -1; });

unique

/** *@name 去除数组元素中重复的元素 * @return Array */ add("unique", function() { return this.filter(function(element, index, array) { return array.indexOf(element) >= index; }); });

Date类型拓展

isLeapYear

/** * @name 判断指定年份是否是闰年 * @return Boolean */ add("isLeapYear", function () { var y = this.getFullYear(); return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; });

isWeekend

/** * @name 判断指定日期是否是周末(即周六或者周日) * @return Boolean */ add("isWeekend", function () { return this.getDay() == 0 || this.getDay() == 6; });

isWeekDay

/** * @name 判断是否是工作日 * @return Boolean */ add("isWeekDay", function () { return !this.isWeekend(); });

getDaysInMonth

/** * @name 获取指定月的总天数 * @return Number */ add("getDaysInMonth", function () { return [31, (this.isLeapYear() ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][this.getMonth()]; });

getDayName

/** * @name 判断指定的日期是星期几. * @param abbreviated 设置是否以简称的形式呈现 * @return String */ add("getDayName", function (abbreviated) { return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()]; });

getMonthName

/** * @name 获取指定日期的月份名称. * @param abbreviated 设置是否以简称的形式呈现 * @return String */ add("getMonthName", function (abbreviated) { return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()]; });

getDayOfYear

/** * @name 指定年已使用的天数 * @return Number */ add("getDayOfYear", function () { var tmpdtm = new Date("1/1/" + this.getFullYear()); return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000); });

getWeekOfYear

/** * @name 获取指定日期已经使用的周数 * @return Number */ add("getWeekOfYear", function () { return Math.ceil(this.getDayOfYear() / 7); });

setDayOfYear

/** * @name 设置指定时间的日期数 * @return Date */ add("setDayOfYear", function (day) { this.setMonth(0); this.setDate(day); return this; });

addYears

/** * @name 添加年数. * @return Date */ add("addYears", function (num) { this.setFullYear(this.getFullYear() + num); return this; });

addMonths

/** * @name 添加月份数 * @return Date */ add("addMonths", function (num) { var tmpdtm = this.getDate(); this.setMonth(this.getMonth() + num); if (tmpdtm > this.getDate()) this.addDays(-this.getDate()); return this; });

addDays

/** * @name 添加天数 * @return Date */ add("addDays", function (num) { var timezoneOffsetBefore = this.getTimezoneOffset(), timezoneOffsetAfter; this.setTime(this.getTime() + (num * 86400000)); timezoneOffsetAfter = this.getTimezoneOffset(); if (timezoneOffsetAfter !== timezoneOffsetBefore) { this.setTime(this.getTime() + ((timezoneOffsetAfter - timezoneOffsetBefore) * 60 * 1000)); } return this; });

addHours

/** * @name 添加小时 * @return Date */ add("addHours", function (num) { this.setHours(this.getHours() + num); return this; });

addMinutes

/** * @name 添加分钟 * @return Date */ add("addMinutes", function (num) { this.setMinutes(this.getMinutes() + num); return this; });

addSeconds

/** * @name 添加秒钟 * @name addSeconds * @return Date */ add("addSeconds", function (num) { this.setSeconds(this.getSeconds() + num); return this; });

zeroTime

/** * @name 零区的设置 * @return Date */ add("zeroTime", function () { this.setMilliseconds(0); this.setSeconds(0); this.setMinutes(0); this.setHours(0); return this; });

asString

/** * @name 转换为指定格式的日期 * @return Date */ add("asString", function (format) { var r = format || Date.format; return r .split('yyyy').join(this.getFullYear()) .split('yy').join((this.getFullYear() + '').substring(2)) .split('dd').join(_zeroPad(this.getDate())) .split('d').join(this.getDate()) .split('DD').join(this.getDayName(false)) .split('D').join(this.getDayName(true)) .split('mmmm').join(this.getMonthName(false)) .split('mmm').join(this.getMonthName(true)) .split('mm').join(_zeroPad(this.getMonth() + 1)) .split('hh').join(_zeroPad(this.getHours())) .split('min').join(_zeroPad(this.getMinutes())) .split('ss').join(_zeroPad(this.getSeconds())); });

String类型拓展

trim

/** * @name 去掉字符串中的左右两边的空格 * @return String */ add("trim", function () { return this.replace(/(^/s+|/s+$)/g, ""); });

camelize

/** * @name 讲一个字符串转换成驼峰格式的字符串 * @return String */ add("camelize", function () { return this.replace(/[-_]([a-z])/ig, function (z, b) { return b.toUpperCase(); }); });

startsWith

/** * @name 判断一个字符串是否以指定的字符前缀和偏移量开头. * @return Boolean * @param 要测试的字符前缀 * @param 字符前缀的偏移量 */ add("startsWith", function (prefix, offset) { var offset = offset || 0; if (offset < 0 || offset > this.length) return false; return this.substring(offset, offset + prefix.length) == prefix; });

endsWith

/** * @name 测试字符串是否以指定的字符后缀结尾 * @return Boolean * @param 被测试的字符后缀 */ add("endsWith", function (suffix) { return this.substring(this.length - suffix.length) == suffix; });

truncate

/** * @name 字符串的截取功能 * @return String * @param legth: 截取后字符串的最大长度 * @param suffix:后缀 */ add("truncate", function (length, suffix) { length = length || 30; suffix = suffix === undefined ? "..." : suffix; return this.length > length ? this.slice(0, length - suffix.length) + suffix : this; });

stripTags

/** * @name 去掉html中的标签元素 * @return String */ add("stripTags", function () { return this.replace(//?[^>]+>/gi, ''); });

源码地址:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值