From 0d2a5329db18974dd56e2f29e01baac3ab8ce4f4 Mon Sep 17 00:00:00 2001 From: Zaytsev Kirill Date: Fri, 18 Jun 2021 14:11:54 +0300 Subject: [PATCH] refactor: merge util removed due it's no longer required --- src/Formulario.ts | 16 +++++----- src/FormularioForm.vue | 3 +- src/types.ts | 2 +- src/utils/index.ts | 1 - src/utils/merge.ts | 40 ------------------------- test/unit/utils/merge.test.js | 56 ----------------------------------- 6 files changed, 10 insertions(+), 108 deletions(-) delete mode 100644 src/utils/merge.ts delete mode 100644 test/unit/utils/merge.test.js diff --git a/src/Formulario.ts b/src/Formulario.ts index 42003a8..e835c42 100644 --- a/src/Formulario.ts +++ b/src/Formulario.ts @@ -10,8 +10,6 @@ import type { import type { Options } from '../types/plugin' -import merge from '@/utils/merge' - import validationRules from '@/validation/rules' import validationMessages from '@/validation/messages' @@ -35,8 +33,8 @@ export default class Formulario { */ public extend (extendWith: Options): Formulario { if (typeof extendWith === 'object') { - this.validationRules = merge(this.validationRules, extendWith.validationRules || {}) - this.validationMessages = merge(this.validationMessages, extendWith.validationMessages || {}) + this.validationRules = { ...this.validationRules, ...(extendWith.validationRules || {}) } + this.validationMessages = { ...this.validationMessages, ...(extendWith.validationMessages || {}) } return this } throw new Error(`[Formulario]: Formulario.extend(): should be passed an object (was ${typeof extendWith})`) @@ -89,7 +87,7 @@ export default class Formulario { * @internal */ public getRules (extendWith: Record = {}): Record { - return merge(this.validationRules, extendWith) + return { ...this.validationRules, ...extendWith } } /** @@ -97,12 +95,14 @@ export default class Formulario { * @internal */ public getMessages (vm: Vue, extendWith: Record): Record { - const raw = merge(this.validationMessages || {}, extendWith) + const raw = { ...this.validationMessages, ...extendWith } const messages: Record = {} for (const name in raw) { - messages[name] = (context: ValidationContext, ...args: any[]): string => { - return typeof raw[name] === 'string' ? raw[name] : raw[name](vm, context, ...args) + messages[name] = (context: ValidationContext, ...args: unknown[]): string => { + const fn = raw[name] + + return typeof fn === 'string' ? fn : fn(vm, context, ...args) } } diff --git a/src/FormularioForm.vue b/src/FormularioForm.vue index 44e9f0e..3971076 100644 --- a/src/FormularioForm.vue +++ b/src/FormularioForm.vue @@ -24,7 +24,6 @@ import { deepEquals, get, has, - merge, set, unset, } from '@/utils' @@ -59,7 +58,7 @@ export default class FormularioForm extends Vue { private localFormErrors: string[] = [] private get fieldsErrorsComputed (): Record { - return merge(this.fieldsErrors || {}, this.localFieldsErrors) + return { ...this.fieldsErrors, ...this.localFieldsErrors } } private get formErrorsComputed (): string[] { diff --git a/src/types.ts b/src/types.ts index 86f0661..0e15d0b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -72,7 +72,7 @@ export function typeOf (value: unknown): string { return 'InstanceOf<' + (constructorOf(value) as { name?: string }).name + '>' } - throw new Error() + throw new Error('[Formulario] typeOf - unknown type detected') } export function isScalar (value: unknown): boolean { diff --git a/src/utils/index.ts b/src/utils/index.ts index c5aae58..615582b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,7 +1,6 @@ export { default as id } from './id' export { default as clone } from './clone' export { default as has } from './has' -export { default as merge } from './merge' export { get, set, unset } from './access' export { default as regexForFormat } from './regexForFormat' export { deepEquals, shallowEquals } from './compare' diff --git a/src/utils/merge.ts b/src/utils/merge.ts deleted file mode 100644 index 1b9a0a3..0000000 --- a/src/utils/merge.ts +++ /dev/null @@ -1,40 +0,0 @@ -import isPlainObject from 'is-plain-object' -import has from '@/utils/has' - -/** - * Create a new object by copying properties of base and mergeWith. - * Note: arrays don't overwrite - they push - * - * @param {Object} a - * @param {Object} b - * @param {boolean} concatArrays - */ -export default function merge ( - a: Record, - b: Record, - concatArrays = true -): Record { - const merged: Record = {} - - for (const key in a) { - if (has(b, key)) { - if (isPlainObject(b[key]) && isPlainObject(a[key])) { - merged[key] = merge(a[key], b[key], concatArrays) - } else if (concatArrays && Array.isArray(a[key]) && Array.isArray(b[key])) { - merged[key] = a[key].concat(b[key]) - } else { - merged[key] = b[key] - } - } else { - merged[key] = a[key] - } - } - - for (const prop in b) { - if (!has(merged, prop)) { - merged[prop] = b[prop] - } - } - - return merged -} diff --git a/test/unit/utils/merge.test.js b/test/unit/utils/merge.test.js deleted file mode 100644 index b29dfb8..0000000 --- a/test/unit/utils/merge.test.js +++ /dev/null @@ -1,56 +0,0 @@ -import merge from '@/utils/merge.ts' - -describe('merge', () => { - it('Can merge simple object', () => { - expect(merge({ - optionA: true, - optionB: '1234', - }, { - optionA: false, - })).toEqual({ - optionA: false, - optionB: '1234', - }) - }) - - it('Can add to simple array', () => { - expect(merge({ - optionA: true, - optionB: ['first', 'second'] - }, { - optionB: ['third'] - }, true)).toEqual({ - optionA: true, - optionB: ['first', 'second', 'third'] - }) - }) - - it('Can merge recursively', () => { - expect(merge({ - optionA: true, - optionC: { - first: '123', - third: { - a: 'b', - }, - }, - optionB: '1234', - }, { - optionB: '567', - optionC: { - first: '1234', - second: '789', - } - })).toEqual({ - optionA: true, - optionC: { - first: '1234', - third: { - a: 'b', - }, - second: '789', - }, - optionB: '567', - }) - }) -})