Skip to content

Commit

Permalink
Merge pull request #8 from entria/fix/dates-config
Browse files Browse the repository at this point in the history
Fix Dates config
  • Loading branch information
felippepuhle authored Aug 23, 2017
2 parents 0143b4f + 67fc2bc commit 3071e5d
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/utils/Dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ import * as Locale from './Locale';
type PossibleDate = Date | string;

type GeneralConfig = {
locale: string,
};
const defaultConfig: GeneralConfig = {
locale: Locale.get(),
locale?: string,
};
const sanitizeConfig = (config: GeneralConfig): GeneralConfig => {
const sanitizedConfig = { ...config };

if (!sanitizedConfig.locale) {
sanitizedConfig.locale = Locale.get();
}

return sanitizedConfig;
}

type ExtractedDate = {
day: string,
Expand All @@ -22,10 +28,12 @@ type ExtractedDate = {
};
export function extract(
value: string,
config: GeneralConfig = { ...defaultConfig }
config: GeneralConfig = {}
): ExtractedDate {
const sanitizedConfig = sanitizeConfig(config);

// brazil = dd/mm/yyyy
if (config.locale === Locale.BRAZIL) {
if (sanitizedConfig.locale === Locale.BRAZIL) {
return {
day: value.substr(0, 2),
month: value.substr(3, 2),
Expand All @@ -43,7 +51,7 @@ export function extract(

export function parse(
value: PossibleDate,
config: GeneralConfig = { ...defaultConfig }
config: GeneralConfig = {}
): Date {
if (value && typeof value === 'string') {
const { day, month, year } = extract(value, config);
Expand All @@ -55,7 +63,7 @@ export function parse(

export function isValid(
date: PossibleDate,
config: GeneralConfig = { ...defaultConfig }
config: GeneralConfig = {}
): boolean {
const isValidFns = fnsIsValid(parse(date, config));

Expand All @@ -70,22 +78,22 @@ export function isValid(

export function isFuture(
date: PossibleDate,
config: GeneralConfig = { ...defaultConfig }
config: GeneralConfig = {}
): boolean {
return fnsIsFuture(parse(date, config));
}

export function isPast(
date: PossibleDate,
config: GeneralConfig = { ...defaultConfig }
config: GeneralConfig = {}
): boolean {
return fnsIsPast(parse(date, config));
}

export function compare(
dateLeft: PossibleDate,
dateRight: PossibleDate,
config: GeneralConfig = { ...defaultConfig }
config: GeneralConfig = {}
): number {
return fnsCompareAsc(parse(dateLeft, config), parse(dateRight, config));
}

0 comments on commit 3071e5d

Please sign in to comment.