mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-25 06:50:34 +03:00
fix: dont inline typeof definitions
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
|
import { isUndefined } from '../shared/typeof'
|
||||||
|
|
||||||
// fallback to timers if rAF not present
|
// fallback to timers if rAF not present
|
||||||
const stopUpdate = (typeof window !== 'undefined' ? window.cancelAnimationFrame : null) || clearTimeout
|
const stopUpdate = (!isUndefined(window) ? window.cancelAnimationFrame : null) || clearTimeout
|
||||||
const startUpdate = (typeof window !== 'undefined' ? window.requestAnimationFrame : null) || (cb => setTimeout(cb, 0))
|
const startUpdate = (!isUndefined(window) ? window.requestAnimationFrame : null) || (cb => setTimeout(cb, 0))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs a batched update. Uses requestAnimationFrame to prevent
|
* Performs a batched update. Uses requestAnimationFrame to prevent
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import getMetaInfo from '../shared/getMetaInfo'
|
import getMetaInfo from '../shared/getMetaInfo'
|
||||||
|
import { isFunction } from '../shared/typeof'
|
||||||
import updateClientMetaInfo from './updateClientMetaInfo'
|
import updateClientMetaInfo from './updateClientMetaInfo'
|
||||||
|
|
||||||
export default function _refresh(options = {}) {
|
export default function _refresh(options = {}) {
|
||||||
@@ -18,7 +19,7 @@ export default function _refresh(options = {}) {
|
|||||||
const tags = updateClientMetaInfo(options, metaInfo)
|
const tags = updateClientMetaInfo(options, metaInfo)
|
||||||
|
|
||||||
// emit "event" with new info
|
// emit "event" with new info
|
||||||
if (tags && typeof metaInfo.changed === 'function') {
|
if (tags && isFunction(metaInfo.changed)) {
|
||||||
metaInfo.changed.call(this, metaInfo, tags.addedTags, tags.removedTags)
|
metaInfo.changed.call(this, metaInfo, tags.addedTags, tags.removedTags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { isUndefined } from '../../shared/typeof'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates meta tags inside <head> and <body> on the client. Borrowed from `react-helmet`:
|
* Updates meta tags inside <head> and <body> on the client. Borrowed from `react-helmet`:
|
||||||
* https://github.com/nfl/react-helmet/blob/004d448f8de5f823d10f838b02317521180f34da/src/Helmet.js#L195-L245
|
* https://github.com/nfl/react-helmet/blob/004d448f8de5f823d10f838b02317521180f34da/src/Helmet.js#L195-L245
|
||||||
@@ -43,10 +45,10 @@ export default function updateTag({ attribute, tagIDKeyName } = {}, type, tags,
|
|||||||
}
|
}
|
||||||
} else if ([tagIDKeyName, 'body'].includes(attr)) {
|
} else if ([tagIDKeyName, 'body'].includes(attr)) {
|
||||||
const _attr = `data-${attr}`
|
const _attr = `data-${attr}`
|
||||||
const value = (typeof tag[attr] === 'undefined') ? '' : tag[attr]
|
const value = isUndefined(tag[attr]) ? '' : tag[attr]
|
||||||
newElement.setAttribute(_attr, value)
|
newElement.setAttribute(_attr, value)
|
||||||
} else {
|
} else {
|
||||||
const value = (typeof tag[attr] === 'undefined') ? '' : tag[attr]
|
const value = isUndefined(tag[attr]) ? '' : tag[attr]
|
||||||
newElement.setAttribute(attr, value)
|
newElement.setAttribute(attr, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { isUndefined } from '../../shared/typeof'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates tag attributes for use on the server.
|
* Generates tag attributes for use on the server.
|
||||||
*
|
*
|
||||||
@@ -15,9 +17,9 @@ export default function attributeGenerator({ attribute } = {}, type, data) {
|
|||||||
if (data.hasOwnProperty(attr)) {
|
if (data.hasOwnProperty(attr)) {
|
||||||
watchedAttrs.push(attr)
|
watchedAttrs.push(attr)
|
||||||
|
|
||||||
attributeStr += typeof data[attr] !== 'undefined'
|
attributeStr += isUndefined(data[attr])
|
||||||
? `${attr}="${data[attr]}"`
|
? attr
|
||||||
: attr
|
: `${attr}="${data[attr]}"`
|
||||||
|
|
||||||
attributeStr += ' '
|
attributeStr += ' '
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { tagsWithoutEndTag, tagsWithInnerContent, tagAttributeAsInnerContent } from '../../shared/constants'
|
import { tagsWithoutEndTag, tagsWithInnerContent, tagAttributeAsInnerContent } from '../../shared/constants'
|
||||||
|
import { isUndefined } from '../../shared/typeof'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates meta, base, link, style, script, noscript tags for use on the server
|
* Generates meta, base, link, style, script, noscript tags for use on the server
|
||||||
@@ -33,7 +34,7 @@ export default function tagGenerator({ attribute, tagIDKeyName } = {}, type, tag
|
|||||||
prefix = 'data-'
|
prefix = 'data-'
|
||||||
}
|
}
|
||||||
|
|
||||||
return typeof tag[attr] === 'undefined'
|
return isUndefined(tag[attr])
|
||||||
? `${attrsStr} ${prefix}${attr}`
|
? `${attrsStr} ${prefix}${attr}`
|
||||||
: `${attrsStr} ${prefix}${attr}="${tag[attr]}"`
|
: `${attrsStr} ${prefix}${attr}="${tag[attr]}"`
|
||||||
}, '')
|
}, '')
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import deepmerge from 'deepmerge'
|
import deepmerge from 'deepmerge'
|
||||||
import uniqueId from 'lodash.uniqueid'
|
import uniqueId from 'lodash.uniqueid'
|
||||||
|
import { isUndefined, isFunction, isObject } from '../shared/typeof'
|
||||||
import uniqBy from './uniqBy'
|
import uniqBy from './uniqBy'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,15 +25,15 @@ export default function getComponentOption({ component, deep, arrayMerge, keyNam
|
|||||||
}
|
}
|
||||||
|
|
||||||
// only collect option data if it exists
|
// only collect option data if it exists
|
||||||
if (typeof $options[keyName] !== 'undefined' && $options[keyName] !== null) {
|
if (!isUndefined($options[keyName]) && $options[keyName] !== null) {
|
||||||
let data = $options[keyName]
|
let data = $options[keyName]
|
||||||
|
|
||||||
// if option is a function, replace it with it's result
|
// if option is a function, replace it with it's result
|
||||||
if (typeof data === 'function') {
|
if (isFunction(data)) {
|
||||||
data = data.call(component)
|
data = data.call(component)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof data === 'object') {
|
if (isObject(data)) {
|
||||||
// merge with existing options
|
// merge with existing options
|
||||||
result = deepmerge(result, data, { arrayMerge })
|
result = deepmerge(result, data, { arrayMerge })
|
||||||
} else {
|
} else {
|
||||||
@@ -55,7 +56,7 @@ export default function getComponentOption({ component, deep, arrayMerge, keyNam
|
|||||||
if (metaTemplateKeyName && result.hasOwnProperty('meta')) {
|
if (metaTemplateKeyName && result.hasOwnProperty('meta')) {
|
||||||
result.meta = Object.keys(result.meta).map((metaKey) => {
|
result.meta = Object.keys(result.meta).map((metaKey) => {
|
||||||
const metaObject = result.meta[metaKey]
|
const metaObject = result.meta[metaKey]
|
||||||
if (!metaObject.hasOwnProperty(metaTemplateKeyName) || !metaObject.hasOwnProperty(contentKeyName) || typeof metaObject[metaTemplateKeyName] === 'undefined') {
|
if (!metaObject.hasOwnProperty(metaTemplateKeyName) || !metaObject.hasOwnProperty(contentKeyName) || isUndefined(metaObject[metaTemplateKeyName])) {
|
||||||
return result.meta[metaKey]
|
return result.meta[metaKey]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +64,7 @@ export default function getComponentOption({ component, deep, arrayMerge, keyNam
|
|||||||
delete metaObject[metaTemplateKeyName]
|
delete metaObject[metaTemplateKeyName]
|
||||||
|
|
||||||
if (template) {
|
if (template) {
|
||||||
metaObject.content = typeof template === 'function' ? template(metaObject.content) : template.replace(/%s/g, metaObject.content)
|
metaObject.content = isFunction(template) ? template(metaObject.content) : template.replace(/%s/g, metaObject.content)
|
||||||
}
|
}
|
||||||
|
|
||||||
return metaObject
|
return metaObject
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import deepmerge from 'deepmerge'
|
import deepmerge from 'deepmerge'
|
||||||
import isPlainObject from 'lodash.isplainobject'
|
import isPlainObject from 'lodash.isplainobject'
|
||||||
|
import { isUndefined, isFunction, isString } from '../shared/typeof'
|
||||||
import isArray from './isArray'
|
import isArray from './isArray'
|
||||||
import getComponentOption from './getComponentOption'
|
import getComponentOption from './getComponentOption'
|
||||||
|
|
||||||
const escapeHTML = str => typeof window === 'undefined'
|
const escapeHTML = str => isUndefined(window)
|
||||||
// server-side escape sequence
|
// server-side escape sequence
|
||||||
? String(str)
|
? String(str)
|
||||||
.replace(/&/g, '&')
|
.replace(/&/g, '&')
|
||||||
@@ -20,7 +21,7 @@ const escapeHTML = str => typeof window === 'undefined'
|
|||||||
.replace(/'/g, '\u0027')
|
.replace(/'/g, '\u0027')
|
||||||
|
|
||||||
const applyTemplate = (component, template, chunk) =>
|
const applyTemplate = (component, template, chunk) =>
|
||||||
typeof template === 'function' ? template.call(component, chunk) : template.replace(/%s/g, chunk)
|
isFunction(template) ? template.call(component, chunk) : template.replace(/%s/g, chunk)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the correct meta info for the given component
|
* Returns the correct meta info for the given component
|
||||||
@@ -137,7 +138,7 @@ export default function getMetaInfo({ keyName, tagIDKeyName, metaTemplateKeyName
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isDisabled) {
|
if (!isDisabled) {
|
||||||
if (typeof val === 'string') {
|
if (isString(val)) {
|
||||||
escaped[key] = escapeHTML(val)
|
escaped[key] = escapeHTML(val)
|
||||||
} else if (isPlainObject(val)) {
|
} else if (isPlainObject(val)) {
|
||||||
escaped[key] = escape(val)
|
escaped[key] = escape(val)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import batchUpdate from '../client/batchUpdate'
|
import batchUpdate from '../client/batchUpdate'
|
||||||
|
import { isUndefined, isFunction, isObject } from '../shared/typeof'
|
||||||
import $meta from './$meta'
|
import $meta from './$meta'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -11,7 +12,7 @@ import {
|
|||||||
} from './constants'
|
} from './constants'
|
||||||
|
|
||||||
// automatic install
|
// automatic install
|
||||||
if (typeof window !== 'undefined' && typeof window.Vue !== 'undefined') {
|
if (!isUndefined(window) && !isUndefined(window.Vue)) {
|
||||||
Vue.use(VueMeta)
|
Vue.use(VueMeta)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ export default function VueMeta(Vue, options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// combine options
|
// combine options
|
||||||
options = typeof options === 'object' ? options : {}
|
options = isObject('object') ? options : {}
|
||||||
|
|
||||||
for (const key in defaultOptions) {
|
for (const key in defaultOptions) {
|
||||||
if (!options[key]) {
|
if (!options[key]) {
|
||||||
@@ -59,13 +60,13 @@ export default function VueMeta(Vue, options = {}) {
|
|||||||
// Add a marker to know if it uses metaInfo
|
// Add a marker to know if it uses metaInfo
|
||||||
// _vnode is used to know that it's attached to a real component
|
// _vnode is used to know that it's attached to a real component
|
||||||
// useful if we use some mixin to add some meta tags (like nuxt-i18n)
|
// useful if we use some mixin to add some meta tags (like nuxt-i18n)
|
||||||
if (typeof this.$options[options.keyName] !== 'undefined' && this.$options[options.keyName] !== null) {
|
if (!isUndefined(this.$options[options.keyName]) && this.$options[options.keyName] !== null) {
|
||||||
this._hasMetaInfo = true
|
this._hasMetaInfo = true
|
||||||
|
|
||||||
// coerce function-style metaInfo to a computed prop so we can observe
|
// coerce function-style metaInfo to a computed prop so we can observe
|
||||||
// it on creation
|
// it on creation
|
||||||
if (typeof this.$options[options.keyName] === 'function') {
|
if (isFunction(this.$options[options.keyName])) {
|
||||||
if (typeof this.$options.computed === 'undefined') {
|
if (isUndefined(this.$options.computed)) {
|
||||||
this.$options.computed = {}
|
this.$options.computed = {}
|
||||||
}
|
}
|
||||||
this.$options.computed.$metaInfo = this.$options[options.keyName]
|
this.$options.computed.$metaInfo = this.$options[options.keyName]
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
export function isType(arg, type) {
|
||||||
|
return typeof arg === type
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isUndefined(arg) {
|
||||||
|
return isType(arg, 'undefined')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isObject(arg) {
|
||||||
|
return isType(arg, 'object')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isFunction(arg) {
|
||||||
|
return isType(arg, 'function')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isString(arg) {
|
||||||
|
return isType(arg, 'string')
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user