Skip to content

Commit 287ffdb

Browse files
authored
Merge pull request #452 from chantouchsek/fix/451-fix-on-clear-errors
Make the clear function onkeydown work correctly
2 parents 012fe7c + 29def5b commit 287ffdb

File tree

10 files changed

+25
-185
lines changed

10 files changed

+25
-185
lines changed

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"watch": "tsc -w",
1313
"start": "nodemon",
1414
"release": "standard-version && git push --follow-tags origin main && yarn publish",
15-
"prepublish": "yarn lint && yarn test && yarn build",
1615
"clean": "rimraf dist",
1716
"prepare": "husky install",
1817
"lint:js": "eslint --ext \".js,.ts\" --ignore-path .gitignore .",
@@ -52,8 +51,7 @@
5251
"@commitlint/config-conventional": "^17.0.0",
5352
"@nuxt/types": "^2.15.8",
5453
"@types/jest": "^27.4.0",
55-
"@types/lodash.get": "^4.4.7",
56-
"@types/lodash.has": "^4.5.7",
54+
"@types/lodash": "^4.14.182",
5755
"@types/node": "^18.0.0",
5856
"@types/qs": "^6.9.7",
5957
"@typescript-eslint/eslint-plugin": "^5.9.0",
@@ -83,8 +81,7 @@
8381
],
8482
"dependencies": {
8583
"axios": "^0.27.2",
86-
"lodash.get": "^4.4.2",
87-
"lodash.has": "^4.5.2",
84+
"lodash": "^4.17.21",
8885
"qs": "^6.10.2"
8986
},
9087
"lint-staged": {

src/__tests__/base-service.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import MockAdapter from 'axios-mock-adapter'
44
import PostService from '../util/PostService'
55
import type { ValidatorType } from '../core/Validator'
66
import Validator from '../core/Validator'
7-
import { merge } from '../util'
7+
import { merge } from 'lodash'
88

99
let service: PostService
1010
let mockAdapter: MockAdapter

src/__tests__/object.test.ts

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
isFile,
3-
cloneDeep,
4-
hasOwnProperty,
5-
merge,
6-
isPlainObject,
7-
} from '../util'
1+
import { hasFiles, hasOwnProperty, isFile } from '../util'
82

93
describe('Object Test', () => {
104
// const { window, File } = global
@@ -16,61 +10,23 @@ describe('Object Test', () => {
1610
const file = new File(['hello world!'], 'myfile')
1711
expect(isFile(file)).toBeTruthy()
1812
})
19-
/*
13+
it('should check has own property', function () {
14+
expect(hasOwnProperty({ dev: null }, '')).toBeFalsy()
15+
})
2016
it('check if window is undefined', () => {
17+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
18+
// @ts-ignore
2119
delete global.window
2220
const file = new File(['hello world!'], 'myfile')
23-
expect(isFile(file)).toBeFalsy()
21+
const form = new FormData()
22+
form.append('file', file)
23+
expect(hasFiles(form)).toBeFalsy()
2424
})
25+
/*
2526
it('check if File is not function', () => {
2627
const file = new File(['hello world!'], 'myfile')
2728
delete global.File
2829
expect(isFile(file)).toBeFalsy()
2930
})
3031
*/
3132
})
32-
describe('cloneDeep', () => {
33-
it('Object is null', () => {
34-
expect(cloneDeep(null)).toBe(null)
35-
})
36-
it('Object is typeof File', () => {
37-
const file = new File(['hello world!'], 'myfile')
38-
expect(cloneDeep(file)).toBeInstanceOf(File)
39-
})
40-
it('Object is typeof Array', () => {
41-
const obj = [{ name: 'Chantouch' }]
42-
expect(cloneDeep(obj)).toBeInstanceOf(Object)
43-
})
44-
it('Has own property undefined', () => {
45-
const obj = [{ name: 'Chantouch' }]
46-
expect(hasOwnProperty(obj, undefined)).toBeFalsy()
47-
})
48-
it('Merge object into object', () => {
49-
const obj1 = { name: 'Chantouch' }
50-
const obj2 = { email: '[email protected]' }
51-
const merged = { email: '[email protected]', name: 'Chantouch' }
52-
expect(merge(obj1, obj2)).toEqual(merged)
53-
})
54-
it('should return `true` if the object is created by the `Object` constructor.', () => {
55-
expect(isPlainObject(Object.create({}))).toBeTruthy()
56-
expect(isPlainObject(Object.create(Object.prototype))).toBeTruthy()
57-
expect(isPlainObject({ foo: 'bar' })).toBeTruthy()
58-
expect(isPlainObject({})).toBeTruthy()
59-
expect(isPlainObject(Object.create(null))).toBeTruthy()
60-
})
61-
it('should return `false` if the object is not created by the `Object` constructor.', () => {
62-
function Foo(this: any) {
63-
this.abc = {}
64-
}
65-
expect(isPlainObject(/foo/)).toBeFalsy()
66-
// eslint-disable-next-line @typescript-eslint/no-empty-function
67-
expect(isPlainObject(function () {})).toBeFalsy()
68-
expect(isPlainObject(1)).toBeFalsy()
69-
expect(isPlainObject(['foo', 'bar'])).toBeFalsy()
70-
expect(isPlainObject([])).toBeFalsy()
71-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
72-
// @ts-ignore
73-
expect(isPlainObject(new Foo())).toBeFalsy()
74-
expect(isPlainObject(null)).toBeFalsy()
75-
})
76-
})

src/core/BaseService.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ import type {
77
} from 'axios'
88
import type { Errors } from '..'
99
import Validator from './Validator'
10-
import {
11-
hasFiles,
12-
objectToFormData,
13-
removeDoubleSlash,
14-
isObject,
15-
isArray,
16-
} from '../util'
10+
import { hasFiles, objectToFormData, removeDoubleSlash } from '../util'
11+
import { isObject, isArray } from 'lodash'
1712
import qs, { IParseOptions } from 'qs'
1813

1914
const validator = Validator

src/core/Validator.ts

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
import { cloneDeep, get, has, omit, isArray } from 'lodash'
12
import { is } from '../util'
2-
import get from 'lodash.get'
3-
import has from 'lodash.has'
43

54
class Validator {
65
public errors: Record<string, any>
@@ -23,7 +22,7 @@ class Validator {
2322
}
2423

2524
has(field: string | string[]) {
26-
if (Array.isArray(field)) {
25+
if (isArray(field)) {
2726
return is(Object.keys(this.errors), field)
2827
}
2928
let hasError = has(this.errors, field)
@@ -110,28 +109,7 @@ class Validator {
110109
if (!attribute) {
111110
return this.flush()
112111
}
113-
const errors = Object.assign({}, this.errors)
114-
if (attribute instanceof Array) {
115-
attribute.map((field: string) => {
116-
Object.keys(errors)
117-
.filter(
118-
(e: string) =>
119-
e === field ||
120-
e.startsWith(`${field}.`) ||
121-
e.startsWith(`${field}[`),
122-
)
123-
.forEach((e: string) => delete errors[e])
124-
})
125-
} else {
126-
Object.keys(errors)
127-
.filter(
128-
(e: string) =>
129-
e === attribute ||
130-
e.startsWith(`${attribute}.`) ||
131-
e.startsWith(`${attribute}[`),
132-
)
133-
.forEach((e: string) => delete errors[e])
134-
}
112+
const errors = omit(cloneDeep(this.errors), attribute)
135113
this.fill(errors)
136114
}
137115

@@ -140,10 +118,10 @@ class Validator {
140118
}
141119

142120
onKeydown(event: any, prefix?: string) {
143-
const { name } = event.target
121+
const { name } = event.target as HTMLInputElement
144122
if (!name) return
145-
const nameWithPrefix = prefix ? `${prefix}.${name}` : null
146-
this.clear([name, nameWithPrefix])
123+
const names = prefix ? [name, `${prefix}.${name}`] : [name]
124+
this.clear(names)
147125
}
148126
}
149127

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ValidatorType } from './core/Validator'
22
import BaseService from './core/BaseService'
33
import Validator from './core/Validator'
4-
import { merge } from './util'
4+
import { merge } from 'lodash'
55
import _Vue from 'vue'
66

77
// augment typings of Vue.js

src/util/createDom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function disableTransitions() {
55
transition: none !important;
66
transition-timing-function: none !important;
77
}`
8-
const head = document.head || document.getElementsByTagName('head')[0]
8+
const head = document.getElementsByTagName('head')[0]
99
const style = document.createElement('style')
1010
head.appendChild(style)
1111
style.type = 'text/css'

src/util/objects.ts

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,69 +18,8 @@ export function isFile(object: any): boolean {
1818
return object instanceof File || object instanceof FileList
1919
}
2020

21-
export function merge(a: any, b: any): Record<any, any> {
22-
for (const key in b) {
23-
if (hasOwnProperty(b, key)) {
24-
a[key] = cloneDeep(b[key])
25-
}
26-
}
27-
return a
28-
}
29-
30-
export function cloneDeep(object: any): any {
31-
if (object === null) {
32-
return null
33-
}
34-
35-
if (isFile(object)) {
36-
return object
37-
}
38-
39-
if (isArray(object)) {
40-
const clone: any = []
41-
42-
for (const key in object) {
43-
if (hasOwnProperty(object, key)) {
44-
clone[key] = cloneDeep(object[key])
45-
}
46-
}
47-
48-
return clone
49-
}
50-
51-
if (typeof object === 'object') {
52-
const clone: any = {}
53-
54-
for (const key in object) {
55-
if (hasOwnProperty(object, key)) {
56-
clone[key] = cloneDeep(object[key])
57-
}
58-
}
59-
60-
return clone
61-
}
62-
63-
return object
64-
}
65-
6621
export function is(errors: any, error: any): boolean {
6722
return isArray(error)
6823
? error.some((w: string) => is(errors, w))
6924
: errors.includes(error)
7025
}
71-
72-
export function isObject(value: any) {
73-
return Object.prototype.toString.call(value) === '[object Object]'
74-
}
75-
76-
export function isPlainObject(value: any) {
77-
if (!isObject(value)) return false
78-
79-
const ctor = value.constructor
80-
if (ctor === undefined) return true
81-
82-
const prot = ctor.prototype
83-
if (!isObject(prot)) return false
84-
85-
return hasOwnProperty(prot, 'isPrototypeOf')
86-
}

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"axios",
2525
"axios-mock-adapter",
2626
"@types/qs",
27-
"@types/lodash.get",
28-
"@types/lodash.has"
27+
"@types/lodash"
2928
]
3029
},
3130
"include": ["src"],

yarn.lock

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,21 +2393,7 @@
23932393
resolved "https://registry.yarnpkg.com/@types/less/-/less-3.0.2.tgz#2761d477678c8374cb9897666871662eb1d1115e"
23942394
integrity sha512-62vfe65cMSzYaWmpmhqCMMNl0khen89w57mByPi1OseGfcV/LV03fO8YVrNj7rFQsRWNJo650WWyh6m7p8vZmA==
23952395

2396-
"@types/lodash.get@^4.4.7":
2397-
version "4.4.7"
2398-
resolved "https://registry.yarnpkg.com/@types/lodash.get/-/lodash.get-4.4.7.tgz#1ea63d8b94709f6bc9e231f252b31440abe312cf"
2399-
integrity sha512-af34Mj+KdDeuzsJBxc/XeTtOx0SZHZNLd+hdrn+PcKGQs0EG2TJTzQAOTCZTgDJCArahlCzLWSy8c2w59JRz7Q==
2400-
dependencies:
2401-
"@types/lodash" "*"
2402-
2403-
"@types/lodash.has@^4.5.7":
2404-
version "4.5.7"
2405-
resolved "https://registry.yarnpkg.com/@types/lodash.has/-/lodash.has-4.5.7.tgz#9e1c3da3ee67f68fadc3d168ffdf8e11a3f3ccd3"
2406-
integrity sha512-nfbAzRbsZBdzSAkL9iiLy4SQk89uuFcXBFwZ7pf6oZhBgPvNys8BY5Twp/w8XvZKGt1o6cAa85wX4QhqO3uQ7A==
2407-
dependencies:
2408-
"@types/lodash" "*"
2409-
2410-
"@types/lodash@*":
2396+
"@types/lodash@^4.14.182":
24112397
version "4.14.182"
24122398
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.182.tgz#05301a4d5e62963227eaafe0ce04dd77c54ea5c2"
24132399
integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==
@@ -7820,16 +7806,6 @@ lodash.debounce@^4.0.8:
78207806
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
78217807
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=
78227808

7823-
lodash.get@^4.4.2:
7824-
version "4.4.2"
7825-
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
7826-
integrity sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==
7827-
7828-
lodash.has@^4.5.2:
7829-
version "4.5.2"
7830-
resolved "https://registry.yarnpkg.com/lodash.has/-/lodash.has-4.5.2.tgz#d19f4dc1095058cccbe2b0cdf4ee0fe4aa37c862"
7831-
integrity sha512-rnYUdIo6xRCJnQmbVFEwcxF144erlD+M3YcJUVesflU9paQaE8p+fJDcIQrlMYbxoANFL+AB9hZrzSBBk5PL+g==
7832-
78337809
lodash.ismatch@^4.4.0:
78347810
version "4.4.0"
78357811
resolved "https://registry.yarnpkg.com/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz#756cb5150ca3ba6f11085a78849645f188f85f37"

0 commit comments

Comments
 (0)