Skip to content

Commit 823e8ed

Browse files
committed
fix: date parse.
1 parent 2583f6e commit 823e8ed

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

dist/index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ function validateRule(rules, customize) {
114114
throw new Error(message);
115115
}
116116
}
117-
if (validator && lodash_1.isString(validator)) {
118-
if (customize && Object.keys(customize).includes(validator)) {
119-
validator = customize[validator];
120-
}
117+
}
118+
if (validator && lodash_1.isString(validator)) {
119+
if (customize && Object.keys(customize).includes(validator)) {
120+
validator = customize[validator];
121121
}
122-
if (validator && lodash_1.isFunction(validator)) {
123-
if (!validator(value)) {
124-
throw new Error(message);
125-
}
122+
}
123+
if (validator && lodash_1.isFunction(validator)) {
124+
if (!validator(value) || String(value) === 'Invalid Date') {
125+
throw new Error(message);
126126
}
127127
}
128128
}
@@ -331,14 +331,16 @@ function getRegexp(regexp) {
331331
function toValue(type) {
332332
if (type === void 0) { type = 'string'; }
333333
return function (value) {
334+
if (type === 'any')
335+
return value;
334336
var val = value;
335337
if (lodash_1.isString(value)) {
336338
if (/^([\d\.]+)\%$/.test(value)) {
337339
val = Number(value.replace(/\%$/i, '')) / 100;
338340
val = String(val);
339341
}
340342
else if (type === 'date') {
341-
val = new Date(rule_judgment_1.isDateString(value) ? value : 0);
343+
val = new Date(rule_judgment_1.isDateString(value) ? value : (/^\d+$/.test(value) ? Number(value) : value));
342344
}
343345
else if (type === 'map') {
344346
try {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "parse-string",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Parse the string into a Map.",
55
"main": "dist/index.js",
66
"typings": "types/index.d.ts",

src/index.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function filterData (options: FilterData.options[], customize?: Record<st
1919
}
2020
if (/\[\]$/.test(type) && isArray(value)) {
2121
let [, itype] = type.match(/(\S+)(\[\])$/)
22-
value = compact(value).map( toValue(itype as 'string' | 'number' | 'date') )
22+
value = compact(value).map( toValue(itype as ParseData.parseType) )
2323
rules && value.forEach( validateRule(rules, customize) )
2424
if (defaultValue && value.length === 0) {
2525
value = defaultValue
@@ -29,7 +29,7 @@ export function filterData (options: FilterData.options[], customize?: Record<st
2929
}
3030
}
3131
else {
32-
value = toValue(type as 'string' | 'number' | 'date')(value)
32+
value = toValue(type as ParseData.parseType)(value)
3333
rules && validateRule(rules, customize)(value)
3434
value = value || defaultValue
3535
if (format) {
@@ -82,15 +82,15 @@ function validateRule (rules: FilterData.rule[], customize?: Record<string, Func
8282
throw new Error(message)
8383
}
8484
}
85-
if (validator && isString(validator)) {
86-
if (customize && Object.keys(customize).includes(validator)) {
87-
validator = customize[validator] as (value: any) => boolean
88-
}
85+
}
86+
if (validator && isString(validator)) {
87+
if (customize && Object.keys(customize).includes(validator)) {
88+
validator = customize[validator] as (value: any) => boolean
8989
}
90-
if (validator && isFunction(validator)) {
91-
if (!validator(value)) {
92-
throw new Error(message)
93-
}
90+
}
91+
if (validator && isFunction(validator)) {
92+
if (!validator(value) || String(value) === 'Invalid Date') {
93+
throw new Error(message)
9494
}
9595
}
9696
}
@@ -301,18 +301,19 @@ function getRegexp (regexp: RegExp | string): RegExp {
301301

302302
/**
303303
* 转换指定类型值
304-
* @param type 'string' | 'number' | 'date' | 'map', 默认值 'string'
304+
* @param type 'string' | 'number' | 'date' | 'map' | 'any', 默认值 'string'
305305
*/
306-
export function toValue (type: 'string' | 'number' | 'date' | 'map' = 'string'): (value: any) => any {
306+
export function toValue (type: ParseData.parseType = 'string'): (value: any) => any {
307307
return (value: any) => {
308+
if (type === 'any') return value
308309
let val = value
309310
if (isString(value)) {
310311
if (/^([\d\.]+)\%$/.test(value)) {
311312
val = Number(value.replace(/\%$/i, '')) / 100
312313
val = String(val)
313314
}
314315
else if (type === 'date') {
315-
val = new Date(isDateString(value) ? value : 0)
316+
val = new Date(isDateString(value) ? value : (/^\d+$/.test(value) ? Number(value) : value))
316317
}
317318
else if (type === 'map') {
318319
try {

types/index.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
export declare namespace ParseData {
33

4+
type parseType = 'string' | 'number' | 'date' | 'map' | 'any'
5+
46
interface parse extends options {
57
key : string
68
orderBy ?: orderBy
@@ -15,14 +17,14 @@ export declare namespace ParseData {
1517

1618
interface collection {
1719
key : string
18-
type ?: 'string' | 'number' | 'date' | 'map'
20+
type ?: parseType
1921
format ?: format | format[]
2022
result ?: result
2123

2224
}
2325

2426
interface format {
25-
type ?: 'string' | 'number' | 'date' | 'map'
27+
type ?: parseType
2628
regexp ?: RegExp | string
2729
substr ?: string
2830
func ?: string
@@ -48,9 +50,11 @@ export declare namespace ParseData {
4850

4951
export declare namespace FilterData {
5052

53+
type filterType = 'string' | 'number' | 'date' | 'map' | 'string[]' | 'number[]' | 'date[]' | 'map[]' | 'any[]'
54+
5155
interface options {
5256
key : string
53-
type : 'string' | 'number' | 'date' | 'map' | 'string[]' | 'number[]' | 'date[]' | 'map[]'
57+
type : filterType
5458
separator ?: string | RegExp
5559
rules ?: rule[]
5660
format ?: ParseData.format | ParseData.format[]
@@ -79,7 +83,7 @@ export function formatData (formats: ParseData.format | ParseData.format[]): (va
7983
export function formatData (formats: ParseData.format | ParseData.format[], customize: Record<string, Function>): (value: any) => any
8084

8185
export function toValue (): (value: any) => any
82-
export function toValue (type: 'string' | 'number' | 'date' | 'map'): (value: any) => any
86+
export function toValue (type: ParseData.parseType): (value: any) => any
8387

8488
export function checkLength (str: string): number
8589

0 commit comments

Comments
 (0)