Skip to content

Commit

Permalink
another naive commit to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Jun 25, 2021
1 parent 6fbbbb5 commit f3fc385
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 62 deletions.
43 changes: 25 additions & 18 deletions addon/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { isEmpty } from '@ember/utils';
import { set } from '@ember/object';
import validationError from 'ember-validators/utils/validation-error';

function formatDate(date, format, locale) {
return new Intl.DateTimeFormat(locale, format).format(date);
}

/**
* @class Date
* @module Validators
Expand All @@ -18,14 +22,14 @@ import validationError from 'ember-validators/utils/validation-error';
* @param {String} options.onOrAfter The specified date must be on or after this date
* @param {String} options.precision Limit the comparison check to a specific granularity.
* Possible Options: [`year`, `month`, `week`, `day`, `hour`, `minute`, `second`].
* @param {String} options.format Input value date format
* @param {String} options.format Input value date format - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
* @param {String} options.errorFormat Error output date format. Defaults to `MMM Do, YYYY`
* @param {Object} model
* @param {String} attribute
*/
export default function validateDate(value, options) {
let errorFormat = options.errorFormat || 'MMM Do, YYYY';
let { locale, format, precision, allowBlank } = options;
let errorFormat = options.errorFormat || { dateStyle: 'long' };
let { locale = 'en-us', format/*, precision*/, allowBlank } = options;
let { before, onOrBefore, after, onOrAfter } = options;
let date;

Expand Down Expand Up @@ -57,35 +61,35 @@ export default function validateDate(value, options) {
if (before) {
before = parseDate(before, format, locale);

if (!isBefore(date, before, precision)) {
set(options, 'before', format(before, errorFormat));
if (!isBefore(date, before)) {
set(options, 'before', formatDate(before, errorFormat, locale));
return validationError('before', value, options);
}
}

if (onOrBefore) {
onOrBefore = parseDate(onOrBefore, format, locale);

if (!isSameOrBefore(date, onOrBefore, precision)) {
set(options, 'onOrBefore', format(onOrBefore, errorFormat));
if (!isSameOrBefore(date, onOrBefore)) {
set(options, 'onOrBefore', formatDate(onOrBefore, errorFormat, locale));
return validationError('onOrBefore', value, options);
}
}

if (after) {
after = parseDate(after, format, locale);

if (!isAfter(date, after, precision)) {
set(options, 'after', format(after, errorFormat));
if (!isAfter(date, after)) {
set(options, 'after', formatDate(after, errorFormat, locale));
return validationError('after', value, options);
}
}

if (onOrAfter) {
onOrAfter = parseDate(onOrAfter, format, locale);

if (!isSameOrAfter(date, onOrAfter, precision)) {
set(options, 'onOrAfter', onOrAfter.format(errorFormat));
if (!isSameOrAfter(date, onOrAfter)) {
set(options, 'onOrAfter', formatDate(onOrAfter, errorFormat, locale));
return validationError('onOrAfter', value, options);
}
}
Expand All @@ -98,7 +102,10 @@ export function parseDate(date, format, locale) {
return new Date();
}

return format ? new Date(date) : new Intl.DateTimeFormat(locale, format);
if (format === null || format === undefined) {
format = { dateStyle: 'long' };
}
return format ? new Date(date) : new Intl.DateTimeFormat(locale, format).format();
}

function isValidDate(d) {
Expand All @@ -107,22 +114,22 @@ function isValidDate(d) {

// WIP naive implementation
// TODO: timezone beware
function isSame(date, comp/*, _precision*/) {
return date === comp;
function isSame(date, comp) {
return date.getTime() === comp.getTime();
}

function isBefore(date, comp/*, _precision*/) {
function isBefore(date, comp) {
return date < comp;
}

function isAfter(date, comp/*, _precision*/) {
function isAfter(date, comp) {
return date > comp;
}

function isSameOrAfter(date, comp/*, _precision*/) {
function isSameOrAfter(date, comp) {
return isSame(date, comp) || isAfter(date, comp);
}

function isSameOrBefore(date, comp/*, _precision*/) {
function isSameOrBefore(date, comp) {
return isSame(date, comp) || isBefore(date, comp);
}
3 changes: 2 additions & 1 deletion addon/ds-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export default function validateDsError(value, options, model, attribute) {
let errors = model[path];

if (errors && errors.has && errors.has(key)) {
let errorsFor = errors.errorsFor(key);
return validationError(
'ds',
null,
options,
get(errors.errorsFor(key), 'lastObject.message')
errorsFor.length ? errorsFor[errorsFor.length - 1].message : []
);
}

Expand Down
Loading

0 comments on commit f3fc385

Please sign in to comment.