From d717dbf4e1bdf903b87601a32a36da200f1c30cf Mon Sep 17 00:00:00 2001 From: pimlie Date: Mon, 11 Mar 2019 17:15:25 +0100 Subject: [PATCH] fix: dont use object.assign/spread so we dont need a polyfill --- src/browser.js | 2 +- src/client/$meta.js | 3 ++- src/index.js | 2 +- src/server/$meta.js | 5 +++-- src/shared/getComponentOption.js | 12 +++++------- src/shared/options.js | 10 +++++++++- test/unit/getComponentOptions.test.js | 10 +++++----- 7 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/browser.js b/src/browser.js index 0be29cf..73693f0 100644 --- a/src/browser.js +++ b/src/browser.js @@ -1,6 +1,6 @@ import { version } from '../package.json' import createMixin from './shared/mixin' -import setOptions from './shared/options' +import { setOptions } from './shared/options' import { isUndefined } from './utils/is-type' import $meta from './client/$meta' import { hasMetaInfo } from './shared/meta-helpers' diff --git a/src/client/$meta.js b/src/client/$meta.js index 874d06d..1d60d3f 100644 --- a/src/client/$meta.js +++ b/src/client/$meta.js @@ -1,3 +1,4 @@ +import { getOptions } from '../shared/options' import { pause, resume } from '../shared/pausing' import refresh from './refresh' @@ -12,7 +13,7 @@ export default function _$meta(options = {}) { */ return function $meta() { return { - getOptions: () => Object.freeze({ ...options }), + getOptions: () => getOptions(options), refresh: _refresh.bind(this), inject, pause: pause.bind(this), diff --git a/src/index.js b/src/index.js index 470fb7d..303c73d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ import { version } from '../package.json' import createMixin from './shared/mixin' -import setOptions from './shared/options' +import { setOptions } from './shared/options' import $meta from './server/$meta' import { hasMetaInfo } from './shared/meta-helpers' diff --git a/src/server/$meta.js b/src/server/$meta.js index 5faffa0..42694a6 100644 --- a/src/server/$meta.js +++ b/src/server/$meta.js @@ -1,5 +1,6 @@ -import refresh from '../client/refresh' +import { getOptions } from '../shared/options' import { pause, resume } from '../shared/pausing' +import refresh from '../client/refresh' import inject from './inject' export default function _$meta(options = {}) { @@ -13,7 +14,7 @@ export default function _$meta(options = {}) { */ return function $meta() { return { - getOptions: () => Object.freeze({ ...options }), + getOptions: () => getOptions(options), refresh: _refresh.bind(this), inject: _inject.bind(this), pause: pause.bind(this), diff --git a/src/shared/getComponentOption.js b/src/shared/getComponentOption.js index f0d2d38..16485a0 100644 --- a/src/shared/getComponentOption.js +++ b/src/shared/getComponentOption.js @@ -1,4 +1,5 @@ import { isFunction, isObject } from '../utils/is-type' +import { findIndex } from '../utils/array' import { merge } from './merge' import { applyTemplate } from './template' import { inMetaInfoBranch } from './meta-helpers' @@ -17,8 +18,8 @@ import { inMetaInfoBranch } from './meta-helpers' * @param {Object} [result={}] - result so far * @return {Object} result - final aggregated result */ -export default function getComponentOption(options = {}, result = {}) { - const { component, keyName, metaTemplateKeyName, tagIDKeyName } = options +export default function getComponentOption(options = {}, component, result = {}) { + const { keyName, metaTemplateKeyName, tagIDKeyName } = options const { $options, $children } = component if (component._inactive) { @@ -52,10 +53,7 @@ export default function getComponentOption(options = {}, result = {}) { return } - result = getComponentOption({ - ...options, - component: childComponent - }, result) + result = getComponentOption(options, childComponent, result) }) } @@ -69,7 +67,7 @@ export default function getComponentOption(options = {}, result = {}) { // keep meta item if it doesnt has a vmid !metaItem.hasOwnProperty(tagIDKeyName) || // or if it's the first item in the array with this vmid - index === arr.findIndex(item => item[tagIDKeyName] === metaItem[tagIDKeyName]) + index === findIndex(arr, item => item[tagIDKeyName] === metaItem[tagIDKeyName]) ) }) } diff --git a/src/shared/options.js b/src/shared/options.js index da36d43..deb8079 100644 --- a/src/shared/options.js +++ b/src/shared/options.js @@ -1,7 +1,7 @@ import { isObject } from '../utils/is-type' import { defaultOptions } from './constants' -export default function setOptions(options) { +export function setOptions(options) { // combine options options = isObject(options) ? options : {} @@ -13,3 +13,11 @@ export default function setOptions(options) { return options } + +export function getOptions(options) { + const optionsCopy = {} + for (const key in options) { + optionsCopy[key] = options[key] + } + return optionsCopy +} diff --git a/test/unit/getComponentOptions.test.js b/test/unit/getComponentOptions.test.js index 7bdc895..5d482b7 100644 --- a/test/unit/getComponentOptions.test.js +++ b/test/unit/getComponentOptions.test.js @@ -9,13 +9,13 @@ describe('getComponentOption', () => { test('returns an empty object when no matching options are found', () => { const component = new Vue() - const mergedOption = getComponentOption({ component, keyName: 'noop' }) + const mergedOption = getComponentOption({ keyName: 'noop' }, component) expect(mergedOption).toEqual({}) }) test('fetches the given option from the given component', () => { const component = new Vue({ someOption: { foo: 'bar' } }) - const mergedOption = getComponentOption({ component, keyName: 'someOption' }) + const mergedOption = getComponentOption({ keyName: 'someOption' }, component) expect(mergedOption.foo).toBeDefined() expect(mergedOption.foo).toEqual('bar') }) @@ -27,7 +27,7 @@ describe('getComponentOption', () => { return { opt: this.$options.name } } }) - const mergedOption = getComponentOption({ component, keyName: 'someFunc' }) + const mergedOption = getComponentOption({ keyName: 'someFunc' }, component) // TODO: Should this be foobar or Foobar expect(mergedOption.opt).toBeDefined() expect(mergedOption.opt).toEqual('Foobar') @@ -44,7 +44,7 @@ describe('getComponentOption', () => { const wrapper = mount(component, { localVue }) - const mergedOption = getComponentOption({ component: wrapper.vm, keyName: 'foo' }) + const mergedOption = getComponentOption({ keyName: 'foo' }, wrapper.vm) expect(mergedOption).toEqual({ bar: 'baz', fizz: 'buzz' }) }) @@ -111,7 +111,7 @@ describe('getComponentOption', () => { const wrapper = mount(component, { localVue }) - const mergedOption = getComponentOption({ component: wrapper.vm, keyName: 'foo' }) + const mergedOption = getComponentOption({ keyName: 'foo' }, wrapper.vm) expect(mergedOption).toEqual({ bar: 'baz' }) expect(wrapper.vm.$children[0]._vueMeta).toBe(true)