From ba74d6b8a6914d6b6246f7ce1c07c6160dac9bd6 Mon Sep 17 00:00:00 2001 From: Xu Liangzhan <xu_liangzhan@163.com> Date: Sun, 18 Mar 2018 21:18:06 +0800 Subject: [PATCH] update 1.5.8 --- README.md | 7 +++++++ dist/xe-utils.js | 17 +++++++++++++++-- dist/xe-utils.min.js | 2 +- src/browse/locat.js | 14 ++++++++++++-- src/browse/methods.js | 7 +++++-- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b44b121..def45be 100644 --- a/README.md +++ b/README.md @@ -635,6 +635,13 @@ import XEUtils from 'xe-utils' XEUtils.locat() // {hash: '', host: '', hostname: '', href: '', protocol: '', port: '', origin: '', query: {...}, params: {...}, ...} ``` +### getBaseURL ( ) 获取上下文路径 +```shell +import XEUtils from 'xe-utils' + +XEUtils.getBaseURL() // http://xuliangzhan.com/demo/ +``` + ### cookie ( ) Cookie操作函数 ```shell import XEUtils from 'xe-utils' diff --git a/dist/xe-utils.js b/dist/xe-utils.js index 907f7f3..ad6a95d 100644 --- a/dist/xe-utils.js +++ b/dist/xe-utils.js @@ -1300,6 +1300,16 @@ return result } + function getLocatOrigin () { + return $locat.origin || ($locat.protocol + '//' + $locat.host) + } + + function getBaseURL () { + var pathname = $locat.pathname + var lastIndex = lastIndexOf(pathname, '/') + 1 + return getLocatOrigin() + (lastIndex === pathname.length ? pathname : pathname.substring(0, lastIndex)) + } + /** * 获取地址栏信息 * @return Object @@ -1311,7 +1321,7 @@ host: $locat.host, hostname: $locat.hostname, protocol: $locat.protocol, - origin: $locat.origin, + origin: getLocatOrigin(), hash: hash(), query: parse($locat.hash), params: parse($locat.search) @@ -1320,8 +1330,11 @@ var browseMethods = { browse: browse, + cookie: cookie, - locat: locat + + locat: locat, + getBaseURL: getBaseURL } function XEUtils () { } diff --git a/dist/xe-utils.min.js b/dist/xe-utils.min.js index fa076e1..081ee4b 100644 --- a/dist/xe-utils.min.js +++ b/dist/xe-utils.min.js @@ -4,4 +4,4 @@ * ISC License. * @preserve */ -!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):n.XEUtils=t()}(this,function(){"use strict";function n(n){return"function"==typeof n}function t(n){return"string"==typeof n}function e(n){return"number"==typeof n}function r(n){return"object"==typeof n}function i(n){return!!n&&n.constructor===Object}function o(n){return!!n&&n.constructor===Date}function u(n){return"undefined"!=typeof Symbol&&Symbol.isSymbol?Symbol.isSymbol(n):"symbol"==typeof n}function a(n){return function(e,r){if(e){if(t(e)||W(e))return n(e,r);for(var i in e)if(r===e[i])return i}return-1}}function f(n,t){return-1!==z(n,t)}function c(n){var t=[];if(n){if(Object.keys)return Object.keys(n);y(n,function(n,e){t.push(e)})}return t}function s(n){if(Object.values)return n?Object.values(n):[];var t=[];return g(c(n),function(e){t.push(n[e])}),t}function l(n){if(Object.entries)return n?Object.entries(n):[];var t=[];return g(c(n),function(e){t.push([e,n[e]])}),t}function h(n){return s(n)[0]}function p(n){var t=s(n);return t[t.length-1]}function y(n,t,e){for(var r in n)n.hasOwnProperty(r)&&t.call(e||this,n[r],r,n)}function g(n,t,e){for(var r=0,i=n.length||0;r<i;r++)t.call(e||this,n[r],r,n)}function m(t,e,r){return t?W(t)?n(t.forEach)?t.forEach(e,r||this):g(t,e,r||this):y(t,e,r||this):t}function d(n){return i(n)?function(n){var t={};return m(n,function(n,e){t[e]=d(n)}),t}(n):W(n)?function(n){return O(n,function(n,t){return d(n)})}(n):n}function v(n){var t=[];return W(n)&&g(n,function(n){t.includes(n)||t.push(n)}),t}function b(){for(var n=[],t=0,e=arguments.length;t<e;t++)n=n.concat(arguments[t]);return v(n)}function S(t,e,r){return W(t)?t.sort(e?n(e)?e.bind(r||this):function(n,t){return n[e]>t[e]?1:-1}:function(n,t){return n>t?1:-1}):t}function M(n){for(var t=[],e=s(n),r=e.length-1;r>=0;r--){var i=r>0?R(0,r):0;t.push(e[i]),e.splice(i,1)}return t}function D(n,t){var e=M(n);return 1===arguments.length?e[0]:(t<e.length&&(e.length=t||0),e)}function w(n,t,e){if(n){if(W(n))return n.some(t,e||this);for(var r in n)if(n.hasOwnProperty(r)&&t.call(e||this,n[r],r,n))return!0}return!1}function x(n,t,e){if(n){if(W(n))return n.every(t,e||this);for(var r in n)if(n.hasOwnProperty(r)&&!t.call(e||this,n[r],r,n))return!1}return!0}function E(n,t,e){if(n){if(W(n))return n.filter(t,e||this);var r={};return m(n,function(i,o){t.call(e||this,i,o,n)&&(r[o]=i)}),r}return[]}function j(n,t,e){if(n){if(W(n))return n.find(t,e||this);for(var r in n)if(n.hasOwnProperty(r)&&t.call(e||this,n[r],r,n))return n[r]}}function O(n,t,e){var r=[];if(n){if(W(n))return n.map(t,e||this);m(n,function(){r.push(t.apply(e||this,arguments))})}return r}function T(n,e){if(n){if(o(n))return n;if(!C(n))return new Date(n);if(t(n)){e=e||"yyyy-MM-dd HH:mm:ss.SSS";var r=[];return g(yn,function(t){for(var i,o,u=0,a=t.rules,f=a.length;u<f;u++){if(i=a[u],(o=e.indexOf(i[0]))>-1){r.push(parseFloat(n.substring(o,o+i[1])||0)+(t.offset||0));break}u===f-1&&r.push(0)}}),new Date(r[0],r[1],r[2],r[3],r[4],r[5],r[6])}}return"Invalid Date"}function F(n,t,e){var r=T(n),i=t&&!C(t)?t:0,o=r.getHours(),u=r.getMinutes(),a=r.getSeconds(),f=r.getMilliseconds();if("first"===e){var c=r.getFullYear(),s=r.getMonth();return(s+=i)<0?new Date(c-Math.ceil((s=Math.abs(s))/12),12-(s%12||1),1,o,u,a,f):new Date(c+Math.floor(s/12),s%12,1,o,u,a,f)}if("last"===e)return new Date(F(r,i+1,"first").getTime()-864e5);var l=r.getDate(),h=F(r,i,"first"),p=h.getMonth();for(h.setDate(l);p<h.getMonth();)h.setDate(--l);return h}function I(n){var t=new RegExp("(?:"+c(n).join("|")+")","g");return function(e){return String(e||"").replace(t,function(t){return n[t]})}}function R(n,t){return n>=t?n:(n>>=0)+Math.round(Math.random()*((t||9)-n))}function H(t,e){return n(e)?S(O(t,e,this)):S(t,e)}function N(){return H.apply(this,arguments)[0]}function k(){return H.apply(this,arguments).reverse()[0]}function U(n,t,e){var i=[];if(W(n)?i=n:arguments.length>1?i=[X({name:n,value:t},e)]:r(n)&&(i=[n]),!(i.length>0)){var o={};return document.cookie&&g(document.cookie.split("; "),function(n){var t=n.split("=");o[decodeURIComponent(t[0])]=decodeURIComponent(t[1]||"")}),1===arguments.length?o[n]:o}g(i,function(n){var t=X({},n),e=[];t.name&&(e.push(encodeURIComponent(t.name)+"="+encodeURIComponent(JSON.stringify(t.value))),void 0!==t.expires&&(t.expires=new Date((new Date).getTime()+864e5*parseFloat(t.expires)).toUTCString()),g(["expires","path","domain","secure"],function(n){void 0!==t[n]&&e.push(n+"="+t[n])})),document.cookie=e.join("; ")})}function $(n){var t={},e=n.split("?")[1]||"";return e&&g(e.split("&"),function(n){var e=n.split("=");t[decodeURIComponent(e[0])]=decodeURIComponent(e[1]||"")}),t}function q(){}function A(n){Sn.objectEach(n,function(n,t){q[t]=Sn.isFunction(n)?function(){var t=n.apply(q.$context,arguments);return q.$context=null,t}:n})}var C=window.isNaN,J=window.isFinite,W=Array.isArray||function(n){return"[object Array]"===Object.prototype.toString.call(n)},P=Number.isInteger,Y=0,z=a(function(n,t){if(n.indexOf)return n.indexOf(t);for(var e=0,r=n.length;e<r;e++)if(t===n[e])return e}),B=a(function(n,t){if(n.lastIndexOf)return n.lastIndexOf(t);for(var e=n.length-1;e>=0;e--)if(t===n[e])return e;return-1}),L=f,X=Object.assign||function(n){if(n)for(var t,e=1,r=arguments.length;e<r;e++)t=arguments[e],g(c(arguments[e]),function(e){n[e]=t[e]});return n},K=X,V=X,G=c,Q=s,Z=l,_=h,nn=p,tn=v,en=b,rn=S,on=M,un=D,an=w,fn=x,cn=E,sn=j,ln=O,hn=Date.now||function(){return(new Date).getTime()},pn=hn,yn=[{rules:[["yyyy",4],["yyy",3],["yy",2]]},{rules:[["MM",2],["M",1]],offset:-1},{rules:[["dd",2],["d",1]]},{rules:[["HH",2],["H",1]]},{rules:[["mm",2],["m",1]]},{rules:[["ss",2],["s",1]]},{rules:[["SSS",3],["SS",2],["S",1]]}],gn=[["yyyy",31536e6],["MM",2592e6],["dd",864e5],["HH",36e5],["mm",6e4],["ss",1e3],["S",0]],mn={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},dn={};g(c(mn),function(n){dn[mn[n]]=n});var vn=I(mn),bn=I(dn),Sn={isNaN:C,isFinite:J,isFloat:function(n){return null!==n&&!C(n)&&!P(n)},isInteger:P,isFunction:n,isBoolean:function(n){return"boolean"==typeof n},isString:t,isRegExp:function(n){return!!n&&n.constructor===RegExp},isObject:r,isError:function(n){return!!n&&n.constructor===Error},isTypeError:function(n){return!!n&&n.constructor===TypeError},isEmpty:function(n){if(0===n||!e(n)){for(var t in n)return!1;return!0}return!1},isNull:function(n){return null===n},isSymbol:u,isArguments:function(n){return"[object Arguments]"===String(n)},isElement:function(n){return n&&t(n.nodeName)&&e(n.nodeType)},isDocument:function(n){return n&&9===n.nodeType},isWindow:function(n){return n&&n===n.window},isFormData:function(n){return"undefined"!=typeof FormData&&n instanceof FormData},isLeapYear:function(n){var t=(n?T(n):new Date).getFullYear();return t%4==0&&(t%100!=0||t%400==0)},getType:function(n){return null===n?""+n:u(n)?"symbol":o(n)?"date":W(n)?"array":typeof n},uniqueId:function(){return++Y},getSize:function(n){var e=0;return t(n)||W(n)?n.length:(m(n,function(){e++}),e)},lastIndexOf:B,includes:f,contains:L,objectAssign:X,assign:K,extend:V,stringToJson:function(n){if(r(n))return n;if(t(n))try{return JSON.parse(n)}catch(n){console.error(n)}return{}},jsonToString:function(n){if(r(n))try{return JSON.stringify(n)}catch(n){console.error(n)}return n?""+n:""},objectKeys:c,keys:G,objectValues:s,values:Q,objectEntries:l,entries:Z,arrayFirst:h,first:_,arrayLast:p,last:nn,objectEach:y,arrayEach:g,each:m,groupBy:function(e,r,i){var o,u,a={};return e&&(t(r)?(u=r,r=null):r=n(r)?r.bind(i||this):u=null,m(e,function(n,t){o=r?r(n,t,e):u?n[u]:n,a[o]?a[o].push(n):a[o]=[n]})),a},objectMap:function(n,t,e){var r={};return m(n,function(i,o){r[o]=t.call(e||this,i,o,n)}),r},clone:function(n,t){return t?d(n):X(i(n)?{}:[],n)},arrayUniq:v,uniq:tn,arrayUnion:b,union:en,arraySort:S,sort:rn,arrayShuffle:M,shuffle:on,arraySample:D,sample:un,arraySome:w,some:an,arrayEvery:x,every:fn,arrayFilter:E,filter:cn,arrayFind:j,find:sn,arrayMap:O,map:ln,timestamp:hn,now:pn,stringToDate:T,dateToString:function(n,t){if(n){if(n=T(n),o(n)){var e=t||"yyyy-MM-dd HH:mm:ss",r=["日","一","二","三","四","五","六"],i={"q+":Math.floor((n.getMonth()+3)/3),"M+":n.getMonth()+1,"E+":n.getDay(),"d+":n.getDate(),"H+":n.getHours(),"m+":n.getMinutes(),"s+":n.getSeconds(),S:n.getMilliseconds()};return/(y+)/.test(e)&&(e=e.replace(RegExp.$1,(""+n.getFullYear()).substr(4-RegExp.$1.length))),g(c(i),function(n){if(new RegExp("("+n+")").test(e)){var t=""+i[n];e=e.replace(RegExp.$1,"q+"===n||"E+"===n?r[t]:1===RegExp.$1.length?t:("00"+t).substr(t.length))}}),e}return n}return""},getWhatMonth:F,getWhatWeek:function(n,t,e){var r=Number(/^[0-7]$/.test(e)?e:0),i=T(n),o=i.getDay(),u=i.getTime()+864e5*((0===r?7:r)-(0===o?7:o));return t&&!C(t)&&(u+=6048e5*t),new Date(u)},getWhatDay:function(n,t){return new Date(T(n).getTime()+(t&&!C(t)?864e5*t:0))},getDaysOfMonth:function(n,t){return Math.floor((F(n,t,"last").getTime()-F(n,t,"first").getTime())/864e5)+1},getDateDiff:function(n,t,e){var r={},i=T(n).getTime(),o=t?T(t).getTime():new Date;if(i<o)for(var u,a=o-i,f=e&&e.length>0?e:gn,c=0,s=f.length;c<s;c++)a>=(u=f[c])[1]&&(c===s-1?a&&(r[u[0]]=a):(r[u[0]]=Math.floor(a/u[1]),a-=r[u[0]]*u[1]));return r},escape:vn,unescape:bn,getRandom:R,arrayMin:N,min:N,arrayMax:k,max:k};X(U,{setItem:function(n,t){U(n,t)},getItem:function(n){return U(n)},removeItem:function(n){U(n,null,{expires:-1})},getJSON:function(){return U()}});var Mn=location,Dn={browse:function(){var n={},t=document.body||document.documentElement;return g(["webkit","khtml","moz","ms","o"],function(e){n["-"+e]=!!t[e+"MatchesSelector"]}),n},cookie:U,locat:function(){return{port:Mn.port,href:Mn.href,host:Mn.host,hostname:Mn.hostname,protocol:Mn.protocol,origin:Mn.origin,hash:(Mn.hash.split("#")[1]||"").split("?")[0]||"",query:$(Mn.hash),params:$(Mn.search)}}};return Sn.objectAssign(q,{mixin:A,version:"1.5.7",$name:"XEUtils"}),A(Sn),A(Dn),q}); \ No newline at end of file +!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):n.XEUtils=t()}(this,function(){"use strict";function n(n){return"function"==typeof n}function t(n){return"string"==typeof n}function e(n){return"number"==typeof n}function r(n){return"object"==typeof n}function i(n){return!!n&&n.constructor===Object}function o(n){return!!n&&n.constructor===Date}function u(n){return"undefined"!=typeof Symbol&&Symbol.isSymbol?Symbol.isSymbol(n):"symbol"==typeof n}function a(n){return function(e,r){if(e){if(t(e)||P(e))return n(e,r);for(var i in e)if(r===e[i])return i}return-1}}function f(n,t){return-1!==L(n,t)}function s(n){var t=[];if(n){if(Object.keys)return Object.keys(n);g(n,function(n,e){t.push(e)})}return t}function c(n){if(Object.values)return n?Object.values(n):[];var t=[];return y(s(n),function(e){t.push(n[e])}),t}function l(n){if(Object.entries)return n?Object.entries(n):[];var t=[];return y(s(n),function(e){t.push([e,n[e]])}),t}function h(n){return c(n)[0]}function p(n){var t=c(n);return t[t.length-1]}function g(n,t,e){for(var r in n)n.hasOwnProperty(r)&&t.call(e||this,n[r],r,n)}function y(n,t,e){for(var r=0,i=n.length||0;r<i;r++)t.call(e||this,n[r],r,n)}function m(t,e,r){return t?P(t)?n(t.forEach)?t.forEach(e,r||this):y(t,e,r||this):g(t,e,r||this):t}function d(n){return i(n)?function(n){var t={};return m(n,function(n,e){t[e]=d(n)}),t}(n):P(n)?function(n){return O(n,function(n,t){return d(n)})}(n):n}function v(n){var t=[];return P(n)&&y(n,function(n){t.includes(n)||t.push(n)}),t}function b(){for(var n=[],t=0,e=arguments.length;t<e;t++)n=n.concat(arguments[t]);return v(n)}function S(t,e,r){return P(t)?t.sort(e?n(e)?e.bind(r||this):function(n,t){return n[e]>t[e]?1:-1}:function(n,t){return n>t?1:-1}):t}function M(n){for(var t=[],e=c(n),r=e.length-1;r>=0;r--){var i=r>0?R(0,r):0;t.push(e[i]),e.splice(i,1)}return t}function D(n,t){var e=M(n);return 1===arguments.length?e[0]:(t<e.length&&(e.length=t||0),e)}function w(n,t,e){if(n){if(P(n))return n.some(t,e||this);for(var r in n)if(n.hasOwnProperty(r)&&t.call(e||this,n[r],r,n))return!0}return!1}function x(n,t,e){if(n){if(P(n))return n.every(t,e||this);for(var r in n)if(n.hasOwnProperty(r)&&!t.call(e||this,n[r],r,n))return!1}return!0}function E(n,t,e){if(n){if(P(n))return n.filter(t,e||this);var r={};return m(n,function(i,o){t.call(e||this,i,o,n)&&(r[o]=i)}),r}return[]}function j(n,t,e){if(n){if(P(n))return n.find(t,e||this);for(var r in n)if(n.hasOwnProperty(r)&&t.call(e||this,n[r],r,n))return n[r]}}function O(n,t,e){var r=[];if(n){if(P(n))return n.map(t,e||this);m(n,function(){r.push(t.apply(e||this,arguments))})}return r}function T(n,e){if(n){if(o(n))return n;if(!J(n))return new Date(n);if(t(n)){e=e||"yyyy-MM-dd HH:mm:ss.SSS";var r=[];return y(yn,function(t){for(var i,o,u=0,a=t.rules,f=a.length;u<f;u++){if(i=a[u],(o=e.indexOf(i[0]))>-1){r.push(parseFloat(n.substring(o,o+i[1])||0)+(t.offset||0));break}u===f-1&&r.push(0)}}),new Date(r[0],r[1],r[2],r[3],r[4],r[5],r[6])}}return"Invalid Date"}function F(n,t,e){var r=T(n),i=t&&!J(t)?t:0,o=r.getHours(),u=r.getMinutes(),a=r.getSeconds(),f=r.getMilliseconds();if("first"===e){var s=r.getFullYear(),c=r.getMonth();return(c+=i)<0?new Date(s-Math.ceil((c=Math.abs(c))/12),12-(c%12||1),1,o,u,a,f):new Date(s+Math.floor(c/12),c%12,1,o,u,a,f)}if("last"===e)return new Date(F(r,i+1,"first").getTime()-864e5);var l=r.getDate(),h=F(r,i,"first"),p=h.getMonth();for(h.setDate(l);p<h.getMonth();)h.setDate(--l);return h}function I(n){var t=new RegExp("(?:"+s(n).join("|")+")","g");return function(e){return String(e||"").replace(t,function(t){return n[t]})}}function R(n,t){return n>=t?n:(n>>=0)+Math.round(Math.random()*((t||9)-n))}function H(t,e){return n(e)?S(O(t,e,this)):S(t,e)}function N(){return H.apply(this,arguments)[0]}function U(){return H.apply(this,arguments).reverse()[0]}function k(n,t,e){var i=[];if(P(n)?i=n:arguments.length>1?i=[K({name:n,value:t},e)]:r(n)&&(i=[n]),!(i.length>0)){var o={};return document.cookie&&y(document.cookie.split("; "),function(n){var t=n.split("=");o[decodeURIComponent(t[0])]=decodeURIComponent(t[1]||"")}),1===arguments.length?o[n]:o}y(i,function(n){var t=K({},n),e=[];t.name&&(e.push(encodeURIComponent(t.name)+"="+encodeURIComponent(JSON.stringify(t.value))),void 0!==t.expires&&(t.expires=new Date((new Date).getTime()+864e5*parseFloat(t.expires)).toUTCString()),y(["expires","path","domain","secure"],function(n){void 0!==t[n]&&e.push(n+"="+t[n])})),document.cookie=e.join("; ")})}function $(n){var t={},e=n.split("?")[1]||"";return e&&y(e.split("&"),function(n){var e=n.split("=");t[decodeURIComponent(e[0])]=decodeURIComponent(e[1]||"")}),t}function q(){return Dn.origin||Dn.protocol+"//"+Dn.host}function A(){}function C(n){Mn.objectEach(n,function(n,t){A[t]=Mn.isFunction(n)?function(){var t=n.apply(A.$context,arguments);return A.$context=null,t}:n})}var J=window.isNaN,W=window.isFinite,P=Array.isArray||function(n){return"[object Array]"===Object.prototype.toString.call(n)},Y=Number.isInteger,B=0,L=a(function(n,t){if(n.indexOf)return n.indexOf(t);for(var e=0,r=n.length;e<r;e++)if(t===n[e])return e}),z=a(function(n,t){if(n.lastIndexOf)return n.lastIndexOf(t);for(var e=n.length-1;e>=0;e--)if(t===n[e])return e;return-1}),X=f,K=Object.assign||function(n){if(n)for(var t,e=1,r=arguments.length;e<r;e++)t=arguments[e],y(s(arguments[e]),function(e){n[e]=t[e]});return n},V=K,G=K,Q=s,Z=c,_=l,nn=h,tn=p,en=v,rn=b,on=S,un=M,an=D,fn=w,sn=x,cn=E,ln=j,hn=O,pn=Date.now||function(){return(new Date).getTime()},gn=pn,yn=[{rules:[["yyyy",4],["yyy",3],["yy",2]]},{rules:[["MM",2],["M",1]],offset:-1},{rules:[["dd",2],["d",1]]},{rules:[["HH",2],["H",1]]},{rules:[["mm",2],["m",1]]},{rules:[["ss",2],["s",1]]},{rules:[["SSS",3],["SS",2],["S",1]]}],mn=[["yyyy",31536e6],["MM",2592e6],["dd",864e5],["HH",36e5],["mm",6e4],["ss",1e3],["S",0]],dn={"&":"&","<":"<",">":">",'"':""","'":"'","`":"`"},vn={};y(s(dn),function(n){vn[dn[n]]=n});var bn=I(dn),Sn=I(vn),Mn={isNaN:J,isFinite:W,isFloat:function(n){return null!==n&&!J(n)&&!Y(n)},isInteger:Y,isFunction:n,isBoolean:function(n){return"boolean"==typeof n},isString:t,isRegExp:function(n){return!!n&&n.constructor===RegExp},isObject:r,isError:function(n){return!!n&&n.constructor===Error},isTypeError:function(n){return!!n&&n.constructor===TypeError},isEmpty:function(n){if(0===n||!e(n)){for(var t in n)return!1;return!0}return!1},isNull:function(n){return null===n},isSymbol:u,isArguments:function(n){return"[object Arguments]"===String(n)},isElement:function(n){return n&&t(n.nodeName)&&e(n.nodeType)},isDocument:function(n){return n&&9===n.nodeType},isWindow:function(n){return n&&n===n.window},isFormData:function(n){return"undefined"!=typeof FormData&&n instanceof FormData},isLeapYear:function(n){var t=(n?T(n):new Date).getFullYear();return t%4==0&&(t%100!=0||t%400==0)},getType:function(n){return null===n?""+n:u(n)?"symbol":o(n)?"date":P(n)?"array":typeof n},uniqueId:function(){return++B},getSize:function(n){var e=0;return t(n)||P(n)?n.length:(m(n,function(){e++}),e)},lastIndexOf:z,includes:f,contains:X,objectAssign:K,assign:V,extend:G,stringToJson:function(n){if(r(n))return n;if(t(n))try{return JSON.parse(n)}catch(n){console.error(n)}return{}},jsonToString:function(n){if(r(n))try{return JSON.stringify(n)}catch(n){console.error(n)}return n?""+n:""},objectKeys:s,keys:Q,objectValues:c,values:Z,objectEntries:l,entries:_,arrayFirst:h,first:nn,arrayLast:p,last:tn,objectEach:g,arrayEach:y,each:m,groupBy:function(e,r,i){var o,u,a={};return e&&(t(r)?(u=r,r=null):r=n(r)?r.bind(i||this):u=null,m(e,function(n,t){o=r?r(n,t,e):u?n[u]:n,a[o]?a[o].push(n):a[o]=[n]})),a},objectMap:function(n,t,e){var r={};return m(n,function(i,o){r[o]=t.call(e||this,i,o,n)}),r},clone:function(n,t){return t?d(n):K(i(n)?{}:[],n)},arrayUniq:v,uniq:en,arrayUnion:b,union:rn,arraySort:S,sort:on,arrayShuffle:M,shuffle:un,arraySample:D,sample:an,arraySome:w,some:fn,arrayEvery:x,every:sn,arrayFilter:E,filter:cn,arrayFind:j,find:ln,arrayMap:O,map:hn,timestamp:pn,now:gn,stringToDate:T,dateToString:function(n,t){if(n){if(n=T(n),o(n)){var e=t||"yyyy-MM-dd HH:mm:ss",r=["日","一","二","三","四","五","六"],i={"q+":Math.floor((n.getMonth()+3)/3),"M+":n.getMonth()+1,"E+":n.getDay(),"d+":n.getDate(),"H+":n.getHours(),"m+":n.getMinutes(),"s+":n.getSeconds(),S:n.getMilliseconds()};return/(y+)/.test(e)&&(e=e.replace(RegExp.$1,(""+n.getFullYear()).substr(4-RegExp.$1.length))),y(s(i),function(n){if(new RegExp("("+n+")").test(e)){var t=""+i[n];e=e.replace(RegExp.$1,"q+"===n||"E+"===n?r[t]:1===RegExp.$1.length?t:("00"+t).substr(t.length))}}),e}return n}return""},getWhatMonth:F,getWhatWeek:function(n,t,e){var r=Number(/^[0-7]$/.test(e)?e:0),i=T(n),o=i.getDay(),u=i.getTime()+864e5*((0===r?7:r)-(0===o?7:o));return t&&!J(t)&&(u+=6048e5*t),new Date(u)},getWhatDay:function(n,t){return new Date(T(n).getTime()+(t&&!J(t)?864e5*t:0))},getDaysOfMonth:function(n,t){return Math.floor((F(n,t,"last").getTime()-F(n,t,"first").getTime())/864e5)+1},getDateDiff:function(n,t,e){var r={},i=T(n).getTime(),o=t?T(t).getTime():new Date;if(i<o)for(var u,a=o-i,f=e&&e.length>0?e:mn,s=0,c=f.length;s<c;s++)a>=(u=f[s])[1]&&(s===c-1?a&&(r[u[0]]=a):(r[u[0]]=Math.floor(a/u[1]),a-=r[u[0]]*u[1]));return r},escape:bn,unescape:Sn,getRandom:R,arrayMin:N,min:N,arrayMax:U,max:U};K(k,{setItem:function(n,t){k(n,t)},getItem:function(n){return k(n)},removeItem:function(n){k(n,null,{expires:-1})},getJSON:function(){return k()}});var Dn=location,wn={browse:function(){var n={},t=document.body||document.documentElement;return y(["webkit","khtml","moz","ms","o"],function(e){n["-"+e]=!!t[e+"MatchesSelector"]}),n},cookie:k,locat:function(){return{port:Dn.port,href:Dn.href,host:Dn.host,hostname:Dn.hostname,protocol:Dn.protocol,origin:q(),hash:(Dn.hash.split("#")[1]||"").split("?")[0]||"",query:$(Dn.hash),params:$(Dn.search)}},getBaseURL:function(){var n=Dn.pathname,t=z(n,"/")+1;return q()+(t===n.length?n:n.substring(0,t))}};return Mn.objectAssign(A,{mixin:C,version:"1.5.7",$name:"XEUtils"}),C(Mn),C(wn),A}); \ No newline at end of file diff --git a/src/browse/locat.js b/src/browse/locat.js index f336af6..9f2b3e3 100644 --- a/src/browse/locat.js +++ b/src/browse/locat.js @@ -1,4 +1,4 @@ -import { arrayEach } from '../core/base' +import { arrayEach, lastIndexOf } from '../core/base' var $locat = location @@ -18,6 +18,16 @@ function parse (uri) { return result } +function getLocatOrigin () { + return $locat.origin || ($locat.protocol + '//' + $locat.host) +} + +export function getBaseURL () { + var pathname = $locat.pathname + var lastIndex = lastIndexOf(pathname, '/') + 1 + return getLocatOrigin() + (lastIndex === pathname.length ? pathname : pathname.substring(0, lastIndex)) +} + /** * 获取地址栏信息 * @return Object @@ -29,7 +39,7 @@ export function locat () { host: $locat.host, hostname: $locat.hostname, protocol: $locat.protocol, - origin: $locat.origin, + origin: getLocatOrigin(), hash: hash(), query: parse($locat.hash), params: parse($locat.search) diff --git a/src/browse/methods.js b/src/browse/methods.js index df92220..9893d9b 100644 --- a/src/browse/methods.js +++ b/src/browse/methods.js @@ -1,9 +1,12 @@ import { browse } from './type' import { cookie } from './cookie' -import { locat } from './locat' +import { locat, getBaseURL } from './locat' export var browseMethods = { browse: browse, + cookie: cookie, - locat: locat + + locat: locat, + getBaseURL: getBaseURL }