Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale formatting not working. #3286

Closed
trichner opened this issue Jul 5, 2016 · 2 comments
Closed

Locale formatting not working. #3286

trichner opened this issue Jul 5, 2016 · 2 comments

Comments

@trichner
Copy link

trichner commented Jul 5, 2016

Hi, I'm getting a weird error from moment.js during moment(..).format('dd DD. MMM YYYY').
Sometimes it seems to work fine but in other cases it throws a TypeError (see below).

I tried to track down the underlying problem with no success, as far as I can see it's related to locales. Hence I used the with-locales version (previously I included only the necessary locales).

I am using the latest (2.14.1) min/moment-with-locales.js from bower. Older versions such as 2.11 also didn't work properly.

angular.js:13708 TypeError: this.localeData(...).weekdaysMin is not a function
    at Moment.<anonymous> (moment-with-locales.js:1230)
    at Object.dd DD. MMM YYYY (moment-with-locales.js:612)
    at formatMoment (moment-with-locales.js:627)
    at Moment.moment_format__format [as format] (moment-with-locales.js:3057)

Which is thrown here:

addFormatToken('dd', 0, 0, function (format) {
        return this.localeData().weekdaysMin(this, format); // <--- TypeError
});

Due to this._locale._weekdaysMin = ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"] being an array.

This is the moment object I'm trying to format: https://gist.github.com/trichner/f2d3ff0a7e6c2b3d668ded52cf69f245#file-gistfile1-txt

Is there anything I missed? Or could it be a library bug?

@trichner trichner changed the title Locale not able to load. Locale formatting not working. Jul 5, 2016
@trichner
Copy link
Author

trichner commented Jul 5, 2016

I found the issue, apparently angular.merge and moment don't mix well.

My fix:

   mergeRec(dst, objs) {
      // heavily inspired by 'angular.merge' but includes proper clone for moment.js
      let isObject = angular.isObject;
      let isFunction = angular.isFunction;
      let isElement = angular.isElement;
      let isDate = angular.isDate;
      let isArray = angular.isArray;

      for (let obj of objs) {
         if (!isObject(obj) && !isFunction(obj)) continue;
         for (let key of Object.keys(obj)) {
            let src = obj[key];

            if (isObject(src)) {
               if (isDate(src)) {
                  dst[key] = new Date(src.valueOf());
               } else if (src instanceof RegExp) {
                  dst[key] = new RegExp(src);
               } else if(this.moment.isMoment(src)){
                  dst[key] = src.clone();
               } else if(this.moment.isDuration(src)){
                  dst[key] = src.clone();
               }else if (src.nodeName) {
                  dst[key] = src.cloneNode(true);
               } else if (isElement(src)) {
                  dst[key] = src.clone();
               } else {
                  if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
                  this.baseExtend(dst[key], [src], true);
               }
            } else {
               dst[key] = src;
            }
         }
      }

      return dst;
   }

@icambron icambron added Bug and removed Bug labels Jul 10, 2016
@icambron
Copy link
Member

icambron commented Jul 10, 2016

Oops, didn't mean to label that a bug.

See #3080, which might be related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants