From 5031acf0faf10a2313f526e14fb622b3c91a17e8 Mon Sep 17 00:00:00 2001 From: pimlie Date: Mon, 11 Feb 2019 10:37:56 +0100 Subject: [PATCH] fix: dont inline typeof definitions --- src/client/batchUpdate.js | 6 ++++-- src/client/refresh.js | 3 ++- src/client/updaters/tag.js | 6 ++++-- src/server/generators/attribute.js | 8 +++++--- src/server/generators/tag.js | 3 ++- src/shared/getComponentOption.js | 11 ++++++----- src/shared/getMetaInfo.js | 7 ++++--- src/shared/plugin.js | 11 ++++++----- src/shared/typeof.js | 20 ++++++++++++++++++++ 9 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 src/shared/typeof.js diff --git a/src/client/batchUpdate.js b/src/client/batchUpdate.js index a65aab48..eb54aa7a 100644 --- a/src/client/batchUpdate.js +++ b/src/client/batchUpdate.js @@ -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 diff --git a/src/client/refresh.js b/src/client/refresh.js index 88ec7a2c..1ef721f5 100644 --- a/src/client/refresh.js +++ b/src/client/refresh.js @@ -1,4 +1,5 @@ import getMetaInfo from '../shared/getMetaInfo' +import { isFunction } from '../shared/typeof' import updateClientMetaInfo from './updateClientMetaInfo' export default function _refresh(options = {}) { @@ -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) } diff --git a/src/client/updaters/tag.js b/src/client/updaters/tag.js index 93b641dc..f4cf9c9a 100644 --- a/src/client/updaters/tag.js +++ b/src/client/updaters/tag.js @@ -1,3 +1,5 @@ +import { isUndefined } from '../../shared/typeof' + /** * Updates meta tags inside and on the client. Borrowed from `react-helmet`: * 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)) { 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) } } diff --git a/src/server/generators/attribute.js b/src/server/generators/attribute.js index 2af05f1e..618e8d5b 100644 --- a/src/server/generators/attribute.js +++ b/src/server/generators/attribute.js @@ -1,3 +1,5 @@ +import { isUndefined } from '../../shared/typeof' + /** * Generates tag attributes for use on the server. * @@ -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 += ' ' } diff --git a/src/server/generators/tag.js b/src/server/generators/tag.js index 57861a78..8127c9d4 100644 --- a/src/server/generators/tag.js +++ b/src/server/generators/tag.js @@ -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 @@ -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]}"` }, '') diff --git a/src/shared/getComponentOption.js b/src/shared/getComponentOption.js index a8cdf2e8..9fa7b112 100644 --- a/src/shared/getComponentOption.js +++ b/src/shared/getComponentOption.js @@ -1,5 +1,6 @@ import deepmerge from 'deepmerge' import uniqueId from 'lodash.uniqueid' +import { isUndefined, isFunction, isObject } from '../shared/typeof' import uniqBy from './uniqBy' /** @@ -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 { @@ -55,7 +56,7 @@ 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] } @@ -63,7 +64,7 @@ export default function getComponentOption({ component, deep, arrayMerge, keyNam 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 diff --git a/src/shared/getMetaInfo.js b/src/shared/getMetaInfo.js index 14ff7bac..a8b16a8f 100644 --- a/src/shared/getMetaInfo.js +++ b/src/shared/getMetaInfo.js @@ -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, '&') @@ -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 @@ -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) diff --git a/src/shared/plugin.js b/src/shared/plugin.js index 01c128a9..1dc70841 100644 --- a/src/shared/plugin.js +++ b/src/shared/plugin.js @@ -1,4 +1,5 @@ import batchUpdate from '../client/batchUpdate' +import { isUndefined, isFunction, isObject } from '../shared/typeof' import $meta from './$meta' import { @@ -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) } @@ -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]) { @@ -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] diff --git a/src/shared/typeof.js b/src/shared/typeof.js new file mode 100644 index 00000000..952db371 --- /dev/null +++ b/src/shared/typeof.js @@ -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') +}