1
0
mirror of synced 2024-11-24 22:36:02 +03:00

refactor: merge util removed due it's no longer required

This commit is contained in:
Zaytsev Kirill 2021-06-18 14:11:54 +03:00
parent dfc6557bc6
commit 0d2a5329db
6 changed files with 10 additions and 108 deletions

View File

@ -10,8 +10,6 @@ import type {
import type { Options } from '../types/plugin' import type { Options } from '../types/plugin'
import merge from '@/utils/merge'
import validationRules from '@/validation/rules' import validationRules from '@/validation/rules'
import validationMessages from '@/validation/messages' import validationMessages from '@/validation/messages'
@ -35,8 +33,8 @@ export default class Formulario {
*/ */
public extend (extendWith: Options): Formulario { public extend (extendWith: Options): Formulario {
if (typeof extendWith === 'object') { if (typeof extendWith === 'object') {
this.validationRules = merge(this.validationRules, extendWith.validationRules || {}) this.validationRules = { ...this.validationRules, ...(extendWith.validationRules || {}) }
this.validationMessages = merge(this.validationMessages, extendWith.validationMessages || {}) this.validationMessages = { ...this.validationMessages, ...(extendWith.validationMessages || {}) }
return this return this
} }
throw new Error(`[Formulario]: Formulario.extend(): should be passed an object (was ${typeof extendWith})`) throw new Error(`[Formulario]: Formulario.extend(): should be passed an object (was ${typeof extendWith})`)
@ -89,7 +87,7 @@ export default class Formulario {
* @internal * @internal
*/ */
public getRules (extendWith: Record<string, ValidationRuleFn> = {}): Record<string, ValidationRuleFn> { public getRules (extendWith: Record<string, ValidationRuleFn> = {}): Record<string, ValidationRuleFn> {
return merge(this.validationRules, extendWith) return { ...this.validationRules, ...extendWith }
} }
/** /**
@ -97,12 +95,14 @@ export default class Formulario {
* @internal * @internal
*/ */
public getMessages (vm: Vue, extendWith: Record<string, ValidationMessageI18NFn|string>): Record<string, ValidationMessageFn> { public getMessages (vm: Vue, extendWith: Record<string, ValidationMessageI18NFn|string>): Record<string, ValidationMessageFn> {
const raw = merge(this.validationMessages || {}, extendWith) const raw = { ...this.validationMessages, ...extendWith }
const messages: Record<string, ValidationMessageFn> = {} const messages: Record<string, ValidationMessageFn> = {}
for (const name in raw) { for (const name in raw) {
messages[name] = (context: ValidationContext, ...args: any[]): string => { messages[name] = (context: ValidationContext, ...args: unknown[]): string => {
return typeof raw[name] === 'string' ? raw[name] : raw[name](vm, context, ...args) const fn = raw[name]
return typeof fn === 'string' ? fn : fn(vm, context, ...args)
} }
} }

View File

@ -24,7 +24,6 @@ import {
deepEquals, deepEquals,
get, get,
has, has,
merge,
set, set,
unset, unset,
} from '@/utils' } from '@/utils'
@ -59,7 +58,7 @@ export default class FormularioForm extends Vue {
private localFormErrors: string[] = [] private localFormErrors: string[] = []
private get fieldsErrorsComputed (): Record<string, string[]> { private get fieldsErrorsComputed (): Record<string, string[]> {
return merge(this.fieldsErrors || {}, this.localFieldsErrors) return { ...this.fieldsErrors, ...this.localFieldsErrors }
} }
private get formErrorsComputed (): string[] { private get formErrorsComputed (): string[] {

View File

@ -72,7 +72,7 @@ export function typeOf (value: unknown): string {
return 'InstanceOf<' + (constructorOf(value) as { name?: string }).name + '>' 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 { export function isScalar (value: unknown): boolean {

View File

@ -1,7 +1,6 @@
export { default as id } from './id' export { default as id } from './id'
export { default as clone } from './clone' export { default as clone } from './clone'
export { default as has } from './has' export { default as has } from './has'
export { default as merge } from './merge'
export { get, set, unset } from './access' export { get, set, unset } from './access'
export { default as regexForFormat } from './regexForFormat' export { default as regexForFormat } from './regexForFormat'
export { deepEquals, shallowEquals } from './compare' export { deepEquals, shallowEquals } from './compare'

View File

@ -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<string, any>,
b: Record<string, any>,
concatArrays = true
): Record<string, any> {
const merged: Record<string, any> = {}
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
}

View File

@ -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',
})
})
})