Skip to content

Commit b043ce3

Browse files
committed
refactor: syncQuery2Conditions
1 parent 2f3c7af commit b043ce3

File tree

5 files changed

+119
-28
lines changed

5 files changed

+119
-28
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@typescript-eslint/no-explicit-any": 0,
4040
"@typescript-eslint/no-unused-vars": 0,
4141
"@typescript-eslint/explicit-module-boundary-types": 0,
42+
"@typescript-eslint/ban-ts-comment": 0,
4243
"@typescript-eslint/ban-types": [
4344
"error",
4445
{

src/core/composable/useHistory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function decode(text: string | number): string {
1515
return '' + text
1616
}
1717

18-
function parseQuery(search: string) {
18+
function parseQuery(search: string): object {
1919
const query = {}
2020
if (search === '' || search === '?') return query
2121
const hasLeadingIM = search[0] === '?'

src/core/useConditionWatcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ export default function useConditionWatcher<O extends object, K extends keyof O>
272272
sync: config.history.sync,
273273
ignore: config.history.ignore || [],
274274
navigation: config.history.navigation || 'push',
275-
listener(parsedQuery) {
275+
listener(parsedQuery: object) {
276276
const queryObject = Object.keys(parsedQuery).length ? parsedQuery : backupIntiConditions
277-
syncQuery2Conditions(_conditions, queryObject)
277+
syncQuery2Conditions(_conditions, queryObject, backupIntiConditions)
278278
},
279279
}
280280
useHistory(query, historyOption)

src/core/utils/common.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,57 @@ export function stringifyQuery(params: ConditionsType, ignoreKeys?: any[]): stri
4848
.join('&')
4949
}
5050

51-
export function syncQuery2Conditions(conditions: ConditionsType, query: ConditionsType): void {
51+
export function typeOf(obj) {
52+
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()
53+
}
54+
55+
export function syncQuery2Conditions(
56+
conditions: ConditionsType,
57+
query: ConditionsType,
58+
backupIntiConditions: ConditionsType
59+
): void {
5260
const conditions2Object = { ...conditions }
5361
const noQuery = Object.keys(query).length === 0
5462
Object.keys(conditions2Object).forEach((key) => {
5563
if (key in query || noQuery) {
56-
if (conditions2Object[key] instanceof Date) {
57-
conditions[key] = noQuery ? null : new Date(query[key])
58-
return
64+
const conditionType = typeOf(conditions2Object[key])
65+
switch (conditionType) {
66+
case 'date':
67+
conditions[key] = noQuery ? '' : new Date(query[key])
68+
break
69+
case 'array':
70+
conditions[key] =
71+
noQuery || !query[key].length ? [] : typeOf(query[key]) === 'string' ? query[key].split(',') : query[key]
72+
73+
if (backupIntiConditions[key].length && conditions[key].length) {
74+
let originArrayValueType = typeOf(backupIntiConditions[key][0])
75+
conditions[key] = conditions[key].map((v: any) => {
76+
switch (originArrayValueType) {
77+
case 'number':
78+
return Number(v)
79+
case 'date':
80+
return new Date(v)
81+
case 'boolean':
82+
return v === 'true'
83+
default:
84+
return String(v)
85+
}
86+
})
87+
}
88+
break
89+
case 'string':
90+
conditions[key] = noQuery ? '' : String(query[key])
91+
break
92+
case 'number':
93+
conditions[key] = noQuery ? 0 : Number(query[key])
94+
break
95+
case 'boolean':
96+
conditions[key] = noQuery ? '' : Boolean(query[key])
97+
break
98+
default:
99+
conditions[key] = noQuery ? '' : query[key]
100+
break
59101
}
60-
conditions[key] = Array.isArray(conditions2Object[key])
61-
? noQuery || !query[key].length
62-
? []
63-
: typeof query[key] === 'string'
64-
? query[key].split(',')
65-
: query[key]
66-
: typeof conditions2Object[key] === 'number'
67-
? noQuery
68-
? 0
69-
: +query[key]
70-
: noQuery
71-
? ''
72-
: query[key]
73102
}
74103
})
75104
}

test/utils.test.ts

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
syncQuery2Conditions,
66
isEquivalent,
77
deepClone,
8+
typeOf,
89
} from '../src/core/utils/common'
910

1011
describe('utils: isEquivalent', () => {
@@ -27,6 +28,37 @@ describe('utils: isEquivalent', () => {
2728
})
2829
})
2930

31+
describe('utils: typeof', () => {
32+
it(`Is array`, () => {
33+
expect(typeOf([])).toEqual('array')
34+
})
35+
it(`Is date`, () => {
36+
expect(typeOf(new Date())).toEqual('date')
37+
})
38+
it(`Is object`, () => {
39+
expect(typeOf({})).toEqual('object')
40+
})
41+
it(`Is number`, () => {
42+
expect(typeOf(1)).toEqual('number')
43+
})
44+
it(`Is string`, () => {
45+
expect(typeOf('hi')).toEqual('string')
46+
})
47+
it(`Is null`, () => {
48+
expect(typeOf(null)).toEqual('null')
49+
})
50+
it(`Is undefined`, () => {
51+
expect(typeOf(undefined)).toEqual('undefined')
52+
})
53+
it(`Is function`, () => {
54+
expect(
55+
typeOf(() => {
56+
//
57+
})
58+
).toEqual('function')
59+
})
60+
})
61+
3062
describe('utils: deepClone', () => {
3163
it(`Check Object deepClone`, () => {
3264
const current = {
@@ -102,7 +134,7 @@ describe('utils: syncQuery2Conditions', () => {
102134
age: 20,
103135
tags: ['react', 'vue'],
104136
}
105-
syncQuery2Conditions(conditions, query)
137+
syncQuery2Conditions(conditions, query, conditions)
106138
expect(conditions).toMatchObject({
107139
age: 50,
108140
tags: ['react', 'vue'],
@@ -116,22 +148,51 @@ describe('utils: syncQuery2Conditions', () => {
116148
const conditions = {
117149
date: new Date(),
118150
}
119-
syncQuery2Conditions(conditions, query)
151+
syncQuery2Conditions(conditions, query, conditions)
120152
expect(Object.prototype.toString.call(conditions.date) === '[object Date]').toBeTruthy()
121153
})
122154

155+
it('should sync query object to conditions with Array<string>', () => {
156+
const query = {
157+
daterange: ['2020-01-02', '2020-01-03'],
158+
}
159+
const conditions = {
160+
daterange: [],
161+
}
162+
syncQuery2Conditions(conditions, query, conditions)
163+
expect(conditions.daterange).toMatchObject(['2020-01-02', '2020-01-03'])
164+
})
165+
166+
it('should sync query object to conditions with Array<number>', () => {
167+
const query = {
168+
daterange: [1, 2],
169+
}
170+
const conditions = {
171+
daterange: [],
172+
}
173+
syncQuery2Conditions(conditions, query, conditions)
174+
expect(conditions.daterange).toMatchObject([1, 2])
175+
})
176+
123177
it('if query is empty conditions should set init value', () => {
124178
const query = {}
125179
const conditions = {
126180
date: new Date(),
127-
name: 'runkids',
128-
tags: ['react'],
181+
string: 'runkids',
182+
array: ['react', 'vue'],
183+
boolean: false,
184+
undefined: undefined,
185+
null: null,
129186
}
130-
syncQuery2Conditions(conditions, query)
187+
syncQuery2Conditions(conditions, query, conditions)
188+
131189
expect(conditions).toMatchObject({
132-
date: null,
133-
name: '',
134-
tags: [],
190+
date: '',
191+
string: '',
192+
array: [],
193+
boolean: '',
194+
undefined: '',
195+
null: '',
135196
})
136197
})
137198
})

0 commit comments

Comments
 (0)