有幸读了一下axios源码,一些utils的实现比较有趣
// begin with <>:// or //
function isAbsoluteUrl(url) {
// RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
// by any combination of letters, digits, plus, period, or hyphen.
const reg = /^([a-z][a-z\d\+\-\.]*:)\/\//i;
return reg.test(url);
}
// combine urls
function combineURLs(baseURL, relativeURL) {
return relativeURL
? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
: baseURL;
}
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
function calcWordNumberInStr(str, word){
const reg = new RegExp(`(${word})`, 'g');
return str.match(reg).length;
}
function isStandardBrowserEnv() {
if(typeof navigator !== 'undefined' && navigator.product === 'ReactNative'){
return false;
}
return (
typeof window !== 'undefined' &&
typeof document !== 'undefined'
);
}
function isURLSearchParams(val) {
return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
}
其中calcWordNumberInStr方法是我自己写的,主要用来判断字符串str中有多少个word!