-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
699dd5a
commit 9f75e9a
Showing
19 changed files
with
139 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export { withMessage } from './withMessage'; | ||
export { withAsync } from './withAsync'; | ||
export { applyIf } from './applyIf'; | ||
export { ruleHelpers } from './rulesHelpers'; | ||
export { ruleHelpers } from './ruleHelpers'; | ||
export { and } from './and'; | ||
export { or } from './or'; | ||
export { not } from './not'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { isEmpty } from './isEmpty'; | ||
|
||
export function isDate(value: unknown): value is Date { | ||
if (isEmpty(value)) { | ||
return false; | ||
} | ||
try { | ||
let possibleDate: Date | null = null; | ||
if (value instanceof Date) { | ||
possibleDate = value; | ||
} else if (typeof value === 'number' && !isNaN(value)) { | ||
possibleDate = new Date(value); | ||
} else if (typeof value === 'string') { | ||
const date = new Date(value); | ||
if (date.toString() === 'Invalid Date') { | ||
return false; | ||
} | ||
possibleDate = date; | ||
} | ||
return !!possibleDate; | ||
} catch (e) { | ||
return false; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function toDate(argument: Date) { | ||
const argStr = Object.prototype.toString.call(argument); | ||
if (argument instanceof Date || (typeof argument === 'object' && argStr === '[object Date]')) { | ||
return new Date(argument.getTime()); | ||
} else if (typeof argument === 'number' || argStr === '[object Number]') { | ||
return new Date(argument); | ||
} else if (typeof argument === 'string' || argStr === '[object String]') { | ||
return new Date(argument); | ||
} else { | ||
return new Date(NaN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ruleHelpers } from '../helpers'; | ||
import { createRule, Maybe, RegleRuleWithParamsDefinition } from '@regle/core'; | ||
|
||
export const dateAfter: RegleRuleWithParamsDefinition<Date, [after: Maybe<Date>]> = createRule< | ||
Date, | ||
[after: Maybe<Date>] | ||
>({ | ||
validator: (value, after) => { | ||
if (ruleHelpers.isDate(value) && ruleHelpers.isDate(after)) { | ||
return ruleHelpers.toDate(value).getTime() > ruleHelpers.toDate(after).getTime(); | ||
} | ||
return true; | ||
}, | ||
message: (_, after) => { | ||
return `The date must be after ${after}`; | ||
}, | ||
type: 'dateAfter', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ruleHelpers } from '../helpers'; | ||
import { createRule, Maybe, RegleRuleWithParamsDefinition } from '@regle/core'; | ||
|
||
export const dateBefore: RegleRuleWithParamsDefinition<Date, [before: Maybe<Date>]> = createRule< | ||
Date, | ||
[before: Maybe<Date>] | ||
>({ | ||
validator: (value, before) => { | ||
if (ruleHelpers.isDate(value) && ruleHelpers.isDate(before)) { | ||
return ruleHelpers.toDate(value).getTime() < ruleHelpers.toDate(before).getTime(); | ||
} | ||
return true; | ||
}, | ||
message: (_, before) => { | ||
return `The date must be before ${before}`; | ||
}, | ||
type: 'dateBefore', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ruleHelpers } from '../helpers'; | ||
import { createRule, Maybe, RegleRuleWithParamsDefinition } from '@regle/core'; | ||
|
||
export const dateBetween: RegleRuleWithParamsDefinition< | ||
Date, | ||
[before: Maybe<Date>, after: Maybe<Date>] | ||
> = createRule<Date, [before: Maybe<Date>, after: Maybe<Date>]>({ | ||
validator: (value, after) => { | ||
if (ruleHelpers.isDate(value) && ruleHelpers.isDate(after)) { | ||
return ruleHelpers.toDate(value).getTime() < ruleHelpers.toDate(after).getTime(); | ||
} | ||
return true; | ||
}, | ||
message: (_, after) => { | ||
return `The date must be before ${after}`; | ||
}, | ||
type: 'dateBetween', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters