Skip to content

Commit

Permalink
fix: dont inline typeof definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie committed Feb 11, 2019
1 parent b4feec0 commit 5031acf
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/client/batchUpdate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { isUndefined } from '../shared/typeof'

// fallback to timers if rAF not present
const stopUpdate = (typeof window !== 'undefined' ? window.cancelAnimationFrame : null) || clearTimeout
const startUpdate = (typeof window !== 'undefined' ? window.requestAnimationFrame : null) || (cb => setTimeout(cb, 0))
const stopUpdate = (!isUndefined(window) ? window.cancelAnimationFrame : null) || clearTimeout
const startUpdate = (!isUndefined(window) ? window.requestAnimationFrame : null) || (cb => setTimeout(cb, 0))

/**
* Performs a batched update. Uses requestAnimationFrame to prevent
Expand Down
3 changes: 2 additions & 1 deletion src/client/refresh.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getMetaInfo from '../shared/getMetaInfo'
import { isFunction } from '../shared/typeof'
import updateClientMetaInfo from './updateClientMetaInfo'

export default function _refresh(options = {}) {
Expand All @@ -18,7 +19,7 @@ export default function _refresh(options = {}) {
const tags = updateClientMetaInfo(options, metaInfo)

// 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)
}

Expand Down
6 changes: 4 additions & 2 deletions src/client/updaters/tag.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isUndefined } from '../../shared/typeof'

/**
* 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
Expand Down Expand Up @@ -43,10 +45,10 @@ export default function updateTag({ attribute, tagIDKeyName } = {}, type, tags,
}
} else if ([tagIDKeyName, 'body'].includes(attr)) {
const _attr = `data-${attr}`
const value = (typeof tag[attr] === 'undefined') ? '' : tag[attr]
const value = isUndefined(tag[attr]) ? '' : tag[attr]
newElement.setAttribute(_attr, value)
} else {
const value = (typeof tag[attr] === 'undefined') ? '' : tag[attr]
const value = isUndefined(tag[attr]) ? '' : tag[attr]
newElement.setAttribute(attr, value)
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/server/generators/attribute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isUndefined } from '../../shared/typeof'

/**
* Generates tag attributes for use on the server.
*
Expand All @@ -15,9 +17,9 @@ export default function attributeGenerator({ attribute } = {}, type, data) {
if (data.hasOwnProperty(attr)) {
watchedAttrs.push(attr)

attributeStr += typeof data[attr] !== 'undefined'
? `${attr}="${data[attr]}"`
: attr
attributeStr += isUndefined(data[attr])
? attr
: `${attr}="${data[attr]}"`

attributeStr += ' '
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/generators/tag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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
Expand Down Expand Up @@ -33,7 +34,7 @@ export default function tagGenerator({ attribute, tagIDKeyName } = {}, type, tag
prefix = 'data-'
}

return typeof tag[attr] === 'undefined'
return isUndefined(tag[attr])
? `${attrsStr} ${prefix}${attr}`
: `${attrsStr} ${prefix}${attr}="${tag[attr]}"`
}, '')
Expand Down
11 changes: 6 additions & 5 deletions src/shared/getComponentOption.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import deepmerge from 'deepmerge'
import uniqueId from 'lodash.uniqueid'
import { isUndefined, isFunction, isObject } from '../shared/typeof'
import uniqBy from './uniqBy'

/**
Expand All @@ -24,15 +25,15 @@ export default function getComponentOption({ component, deep, arrayMerge, keyNam
}

// 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]

// if option is a function, replace it with it's result
if (typeof data === 'function') {
if (isFunction(data)) {
data = data.call(component)
}

if (typeof data === 'object') {
if (isObject(data)) {
// merge with existing options
result = deepmerge(result, data, { arrayMerge })
} else {
Expand All @@ -55,15 +56,15 @@ export default function getComponentOption({ component, deep, arrayMerge, keyNam
if (metaTemplateKeyName && result.hasOwnProperty('meta')) {
result.meta = Object.keys(result.meta).map((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]
}

const template = metaObject[metaTemplateKeyName]
delete metaObject[metaTemplateKeyName]

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
Expand Down
7 changes: 4 additions & 3 deletions src/shared/getMetaInfo.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import deepmerge from 'deepmerge'
import isPlainObject from 'lodash.isplainobject'
import { isUndefined, isFunction, isString } from '../shared/typeof'
import isArray from './isArray'
import getComponentOption from './getComponentOption'

const escapeHTML = str => typeof window === 'undefined'
const escapeHTML = str => isUndefined(window)
// server-side escape sequence
? String(str)
.replace(/&/g, '&amp;')
Expand All @@ -20,7 +21,7 @@ const escapeHTML = str => typeof window === 'undefined'
.replace(/'/g, '\u0027')

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
Expand Down Expand Up @@ -137,7 +138,7 @@ export default function getMetaInfo({ keyName, tagIDKeyName, metaTemplateKeyName
}

if (!isDisabled) {
if (typeof val === 'string') {
if (isString(val)) {
escaped[key] = escapeHTML(val)
} else if (isPlainObject(val)) {
escaped[key] = escape(val)
Expand Down
11 changes: 6 additions & 5 deletions src/shared/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import batchUpdate from '../client/batchUpdate'
import { isUndefined, isFunction, isObject } from '../shared/typeof'
import $meta from './$meta'

import {
Expand All @@ -11,7 +12,7 @@ import {
} from './constants'

// automatic install
if (typeof window !== 'undefined' && typeof window.Vue !== 'undefined') {
if (!isUndefined(window) && !isUndefined(window.Vue)) {
Vue.use(VueMeta)
}

Expand All @@ -31,7 +32,7 @@ export default function VueMeta(Vue, options = {}) {
}

// combine options
options = typeof options === 'object' ? options : {}
options = isObject('object') ? options : {}

for (const key in defaultOptions) {
if (!options[key]) {
Expand Down Expand Up @@ -59,13 +60,13 @@ export default function VueMeta(Vue, options = {}) {
// Add a marker to know if it uses metaInfo
// _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)
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

// coerce function-style metaInfo to a computed prop so we can observe
// it on creation
if (typeof this.$options[options.keyName] === 'function') {
if (typeof this.$options.computed === 'undefined') {
if (isFunction(this.$options[options.keyName])) {
if (isUndefined(this.$options.computed)) {
this.$options.computed = {}
}
this.$options.computed.$metaInfo = this.$options[options.keyName]
Expand Down
20 changes: 20 additions & 0 deletions src/shared/typeof.js
Original file line number Diff line number Diff line change
@@ -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')
}

0 comments on commit 5031acf

Please sign in to comment.