Skip to content

Commit

Permalink
fix: date parse.
Browse files Browse the repository at this point in the history
  • Loading branch information
thondery committed Nov 4, 2020
1 parent 2583f6e commit 823e8ed
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
20 changes: 11 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ function validateRule(rules, customize) {
throw new Error(message);
}
}
if (validator && lodash_1.isString(validator)) {
if (customize && Object.keys(customize).includes(validator)) {
validator = customize[validator];
}
}
if (validator && lodash_1.isString(validator)) {
if (customize && Object.keys(customize).includes(validator)) {
validator = customize[validator];
}
if (validator && lodash_1.isFunction(validator)) {
if (!validator(value)) {
throw new Error(message);
}
}
if (validator && lodash_1.isFunction(validator)) {
if (!validator(value) || String(value) === 'Invalid Date') {
throw new Error(message);
}
}
}
Expand Down Expand Up @@ -331,14 +331,16 @@ function getRegexp(regexp) {
function toValue(type) {
if (type === void 0) { type = 'string'; }
return function (value) {
if (type === 'any')
return value;
var val = value;
if (lodash_1.isString(value)) {
if (/^([\d\.]+)\%$/.test(value)) {
val = Number(value.replace(/\%$/i, '')) / 100;
val = String(val);
}
else if (type === 'date') {
val = new Date(rule_judgment_1.isDateString(value) ? value : 0);
val = new Date(rule_judgment_1.isDateString(value) ? value : (/^\d+$/.test(value) ? Number(value) : value));
}
else if (type === 'map') {
try {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parse-string",
"version": "1.2.0",
"version": "1.2.1",
"description": "Parse the string into a Map.",
"main": "dist/index.js",
"typings": "types/index.d.ts",
Expand Down
27 changes: 14 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function filterData (options: FilterData.options[], customize?: Record<st
}
if (/\[\]$/.test(type) && isArray(value)) {
let [, itype] = type.match(/(\S+)(\[\])$/)
value = compact(value).map( toValue(itype as 'string' | 'number' | 'date') )
value = compact(value).map( toValue(itype as ParseData.parseType) )
rules && value.forEach( validateRule(rules, customize) )
if (defaultValue && value.length === 0) {
value = defaultValue
Expand All @@ -29,7 +29,7 @@ export function filterData (options: FilterData.options[], customize?: Record<st
}
}
else {
value = toValue(type as 'string' | 'number' | 'date')(value)
value = toValue(type as ParseData.parseType)(value)
rules && validateRule(rules, customize)(value)
value = value || defaultValue
if (format) {
Expand Down Expand Up @@ -82,15 +82,15 @@ function validateRule (rules: FilterData.rule[], customize?: Record<string, Func
throw new Error(message)
}
}
if (validator && isString(validator)) {
if (customize && Object.keys(customize).includes(validator)) {
validator = customize[validator] as (value: any) => boolean
}
}
if (validator && isString(validator)) {
if (customize && Object.keys(customize).includes(validator)) {
validator = customize[validator] as (value: any) => boolean
}
if (validator && isFunction(validator)) {
if (!validator(value)) {
throw new Error(message)
}
}
if (validator && isFunction(validator)) {
if (!validator(value) || String(value) === 'Invalid Date') {
throw new Error(message)
}
}
}
Expand Down Expand Up @@ -301,18 +301,19 @@ function getRegexp (regexp: RegExp | string): RegExp {

/**
* 转换指定类型值
* @param type 'string' | 'number' | 'date' | 'map', 默认值 'string'
* @param type 'string' | 'number' | 'date' | 'map' | 'any', 默认值 'string'
*/
export function toValue (type: 'string' | 'number' | 'date' | 'map' = 'string'): (value: any) => any {
export function toValue (type: ParseData.parseType = 'string'): (value: any) => any {
return (value: any) => {
if (type === 'any') return value
let val = value
if (isString(value)) {
if (/^([\d\.]+)\%$/.test(value)) {
val = Number(value.replace(/\%$/i, '')) / 100
val = String(val)
}
else if (type === 'date') {
val = new Date(isDateString(value) ? value : 0)
val = new Date(isDateString(value) ? value : (/^\d+$/.test(value) ? Number(value) : value))
}
else if (type === 'map') {
try {
Expand Down
12 changes: 8 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

export declare namespace ParseData {

type parseType = 'string' | 'number' | 'date' | 'map' | 'any'

interface parse extends options {
key : string
orderBy ?: orderBy
Expand All @@ -15,14 +17,14 @@ export declare namespace ParseData {

interface collection {
key : string
type ?: 'string' | 'number' | 'date' | 'map'
type ?: parseType
format ?: format | format[]
result ?: result

}

interface format {
type ?: 'string' | 'number' | 'date' | 'map'
type ?: parseType
regexp ?: RegExp | string
substr ?: string
func ?: string
Expand All @@ -48,9 +50,11 @@ export declare namespace ParseData {

export declare namespace FilterData {

type filterType = 'string' | 'number' | 'date' | 'map' | 'string[]' | 'number[]' | 'date[]' | 'map[]' | 'any[]'

interface options {
key : string
type : 'string' | 'number' | 'date' | 'map' | 'string[]' | 'number[]' | 'date[]' | 'map[]'
type : filterType
separator ?: string | RegExp
rules ?: rule[]
format ?: ParseData.format | ParseData.format[]
Expand Down Expand Up @@ -79,7 +83,7 @@ export function formatData (formats: ParseData.format | ParseData.format[]): (va
export function formatData (formats: ParseData.format | ParseData.format[], customize: Record<string, Function>): (value: any) => any

export function toValue (): (value: any) => any
export function toValue (type: 'string' | 'number' | 'date' | 'map'): (value: any) => any
export function toValue (type: ParseData.parseType): (value: any) => any

export function checkLength (str: string): number

Expand Down

0 comments on commit 823e8ed

Please sign in to comment.