Skip to content

Instantly share code, notes, and snippets.

@iblueer
Created November 30, 2022 03:08
Show Gist options
  • Save iblueer/8fe3911fbddb5a5cb3a620bea07be6b6 to your computer and use it in GitHub Desktop.
Save iblueer/8fe3911fbddb5a5cb3a620bea07be6b6 to your computer and use it in GitHub Desktop.
从ancc.org.cn网站上发现的
/*
* zCool 0.1 String - Javascript
*
* Copyright (c) 2008 zhou BaiMin (zCool.cn)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* $Date: 2009-05-28 14:28:00 BeiJing $
* $Revision: 1 $
*/
// @import zCool.js
// 扩展字符串原型方法
(function(window, $, undefined){
var rNumber = /^\d+$/,
rInt = /^[-+]?\d+$/,
rFloat = /^[-+]?\d+(?:\.\d+)?$/,
reDoubleByte = /[^\x00-\xff]/,
rOnlyDoubleByte = /^[^\x00-\xff]+$/,
reDoubleByteG = /[^\x00-\xff]/g,
reDoubleBytesG = /([^\x00-\xff]+)/g,
rZh = /[\u4e00-\u9fa5\uf900-\ufa2d]/,
rOnlyZh = /^[\u4e00-\u9fa5\uf900-\ufa2d]+$/,
search = document.location.search,
hasOwnProperty = Object.prototype.hasOwnProperty;
// 扩展字符串类的原型方法
$.extend(String.prototype, {
// 遍历一个hash,用hash值替换掉匹配hash键的字符串
multiReplace : function(hash){
var str = this.toString(), key, value;
for(key in hash){
// 若为自身属性
if(!hasOwnProperty || hash.hasOwnProperty(key)){
// 若为字符串,直接hash值替换
// 否则,再用其作为一个hash遍历,尾递归执行该字符串的方法
str = typeof(value = hash[key]) == "string" ? str.replace(new RegExp(key, "g"), value) : str.multiReplace(value);
}
}
return str;
},
// 测试是否(只)包含双字节字符
hasDoubleByte :
function(only){
return only ? rOnlyDoubleByte.test(this) : reDoubleByte.test(this);
},
// // 返回字符串字节数(该字符串可能包含了双字节字符)
// getByteLength :
// function(){
// return this.replace(reDoubleByteG, "**").length;
// },
// 返回字符串字节数(该字符串可能包含了双字节字符)
getByteLength :
function(){
return this.replace(reDoubleBytesG, "$1$1").length;
},
// 测试字符串是否(只)包含中文字符
hasZh :
function(only){
return only ? rZh.test(this) : rOnlyZh.test(this);
},
// 按字节数截取字符串(该字符串可能包含了双字节字符)
// @ceil boolean 是否保留半个字节
cutByte :
function(cutLength, ceil){
var l = this.length, i = -1, L = 0;
while(++i < l && L < cutLength){
L += this.charCodeAt(i) > 127 ? 2 : 1;
}
return this.slice(0, L > cutLength && ceil ? i : i - 1);
},
// 是否全为数字
isNumber :
function(){
return rNumber.test( this.trim() );
},
// 是否为浮点类型
isFloat :
function(){
return rFloat.test( this.trim() );
},
// 是否为int类型
isInt :
function(){
return rInt.test( this.trim() );
},
// 从一个字符串中随机抽取指定长度的字符
getRandom :
function(length){
var l = this.length, s = '';
length = length >>> 0;
while( --length > -1){
s += this.charAt( parseInt(Math.random()*l) );
}
return s;
}
});
// 扩展静态方法
$.extend(String, {
// 查询url的search字段获取字段值 return string
// @query string
// @s string (document.location.search)
queryURIParam: function( query, s, undecode ){
typeof s === 'string' || ( s = search );
s.charAt(0) === '?' && (s = s.slice(1));
return s && new RegExp("&" + query + "=([^&]*)").
test( "&" + s ) ?
undecode ? RegExp.$1 : unescape(RegExp.$1) :
null;
},
// 查询url的search字段获取相同字段多个值的数组 return array
// @query string
// @s string (document.location.search)
queryURIParamAll: function( query, s, undecode ){
typeof s === 'string' || ( s = search );
s.charAt(0) === '?' && (s = s.slice(1));
if( s ){
var start = query.length + 2;
return ( "&" + s ).
match( new RegExp("&" + query + "=[^&]*", "g") ).
map( function(s){
return undecode ? RegExp.$1 : unescape(s.slice(start))
});
}
return [];
},
// 查询url的search字段获取相同字段多个值的数组 return array
// @query string
// @s string (document.location.search)
queryURIParams: function( s, undecode ){
var Params = {};
typeof s === 'string' || ( s = search );
if( s ){
s.charAt(0) === '?' && (s = s.slice(1));
s.split('&').
forEach(function(p){
var i = p.indexOf('='),
name = p.slice(0, i++),
value = undecode ? p.slice(i) : unescape(p.slice(i)),
oldValue = this[name];
if( oldValue === undefined ){
this[name] = value;
}
else if( typeof oldValue == 'string' ){
this[name] = [oldValue, value];
}
else{
oldValue.push(value);
}
}, Params);
}
return Params;
},
parseUrl: function( url ){
var r = /^\s*(?:#(\S*))?(?:\s+url\(([^)]*)\))?(?:\s+([\w-]*)(?:\/([\w-]*))?)?$/;
},
// 根据12(EAN-13)或13(ITF-14)位的条码(字符串),计算出并返回校验码
barcodeChecksum: function( barcode ){
var l = barcode.length, sum = 0;
if(typeof barcode !== 'string' || /\D/.test(barcode) || (l !== 12 && l !== 13)){
throw new Error('参数不是12(EAN-13)或13(ITF-14)位数字字符!');
//return;
}
barcode.split('').reverse().forEach(function(n, i){
sum += i%2 ? Number(n) : Number(n)*3;
});
return Math.ceil(sum/10)*10 - sum;
}
});
})(this, this.zCool);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment