cybertooth-io/ember-helper-locale-date

View on GitHub
addon/utils/format-util.js

Summary

Maintainability
A
25 mins
Test Coverage
import { assign } from '@ember/polyfills';
import { isEmpty, isNone, typeOf } from '@ember/utils';

export default function formatUtil(formatter, defaultOptions, date, options = {}) {
  if (isEmpty(date) || typeOf(date) !== 'date') {
    return '';
  }

  if (isNone(options) || (Object.entries(options).length === 0 && options.constructor === Object)) {
    return formatter.format(date);
  } else {
    return date.toLocaleString(undefined, assign(assign({}, defaultOptions), options));
  }
}