UNPKG

2.99 kBJavaScriptView Raw
1/* @flow */
2
3// can we use __proto__?
4export const hasProto = '__proto__' in {}
5
6// Browser environment sniffing
7export const inBrowser = typeof window !== 'undefined'
8export const inWeex = typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform
9export const weexPlatform = inWeex && WXEnvironment.platform.toLowerCase()
10export const UA = inBrowser && window.navigator.userAgent.toLowerCase()
11export const isIE = UA && /msie|trident/.test(UA)
12export const isIE9 = UA && UA.indexOf('msie 9.0') > 0
13export const isEdge = UA && UA.indexOf('edge/') > 0
14export const isAndroid = (UA && UA.indexOf('android') > 0) || (weexPlatform === 'android')
15export const isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA)) || (weexPlatform === 'ios')
16export const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge
17export const isPhantomJS = UA && /phantomjs/.test(UA)
18export const isFF = UA && UA.match(/firefox\/(\d+)/)
19
20// Firefox has a "watch" function on Object.prototype...
21export const nativeWatch = ({}).watch
22
23export let supportsPassive = false
24if (inBrowser) {
25 try {
26 const opts = {}
27 Object.defineProperty(opts, 'passive', ({
28 get () {
29 /* istanbul ignore next */
30 supportsPassive = true
31 }
32 }: Object)) // https://github.com/facebook/flow/issues/285
33 window.addEventListener('test-passive', null, opts)
34 } catch (e) {}
35}
36
37// this needs to be lazy-evaled because vue may be required before
38// vue-server-renderer can set VUE_ENV
39let _isServer
40export const isServerRendering = () => {
41 if (_isServer === undefined) {
42 /* istanbul ignore if */
43 if (!inBrowser && !inWeex && typeof global !== 'undefined') {
44 // detect presence of vue-server-renderer and avoid
45 // Webpack shimming the process
46 _isServer = global['process'] && global['process'].env.VUE_ENV === 'server'
47 } else {
48 _isServer = false
49 }
50 }
51 return _isServer
52}
53
54// detect devtools
55export const devtools = inBrowser && window.__VUE_DEVTOOLS_GLOBAL_HOOK__
56
57/* istanbul ignore next */
58export function isNative (Ctor: any): boolean {
59 return typeof Ctor === 'function' && /native code/.test(Ctor.toString())
60}
61
62export const hasSymbol =
63 typeof Symbol !== 'undefined' && isNative(Symbol) &&
64 typeof Reflect !== 'undefined' && isNative(Reflect.ownKeys)
65
66let _Set
67/* istanbul ignore if */ // $flow-disable-line
68if (typeof Set !== 'undefined' && isNative(Set)) {
69 // use native Set when available.
70 _Set = Set
71} else {
72 // a non-standard Set polyfill that only works with primitive keys.
73 _Set = class Set implements SimpleSet {
74 set: Object;
75 constructor () {
76 this.set = Object.create(null)
77 }
78 has (key: string | number) {
79 return this.set[key] === true
80 }
81 add (key: string | number) {
82 this.set[key] = true
83 }
84 clear () {
85 this.set = Object.create(null)
86 }
87 }
88}
89
90export interface SimpleSet {
91 has(key: string | number): boolean;
92 add(key: string | number): mixed;
93 clear(): void;
94}
95
96export { _Set }