jalaali / moment-jalaali

A Jalaali (Jalali, Persian, Khorshidi, Shamsi) calendar system plugin for moment.js.
MIT License
936 stars 162 forks source link

'humanize' of undefined Moment.fromNow() #159

Closed soroushm closed 4 years ago

soroushm commented 5 years ago

Dear when i add dialect: 'persian-modern and use moment(date).fromNow()i getting error 'humanize' of undefined but without loadPersian working as well

version

"react":"16.6.0" "moment": "^2.22.2", "moment-jalaali": "^0.8.1",

moment(date).fromNow()

## Error

F.from node_modules/moment/moment.js:3430 3427 | return createDuration({ 3428 | to: this, 3429 | from: time

3430 | }).locale(this.locale()).humanize(!withoutSuffix); | ^ 3431 | } else { 3432 | return this.localeData().invalidDate(); 3433 | }

npm test

first time getting error about chai is not found

masouds-MacBook-Pro:moment-jalaali silverstar$ npm test

> moment-jalaali@0.8.1 test /Users/silverstar/Sites/healths-chi/react/node_modules/moment-jalaali
> mocha --reporter spec --ui bdd --colors --check-leaks test.js

module.js:549
    throw err;
    ^

Error: Cannot find module 'chai'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/silverstar/Sites/healths-chi/react/node_modules/moment-jalaali/test.js:2:12)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at /usr/local/lib/node_modules/mocha/lib/mocha.js:250:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:247:14)
    at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:576:10)
    at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:637:18)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
npm ERR! Test failed.  See above for more details.

after install chai

masouds-MacBook-Pro:moment-jalaali silverstar$ npm test

> moment-jalaali@0.8.1 test /Users/silverstar/Sites/healths-chi/react/node_modules/moment-jalaali
> mocha --reporter spec --ui bdd --colors --check-leaks test.js

  moment
    #parse
      ✓ should parse gregorian dates
      ✓ should parse correctly when input is only time
      ✓ should parse when only Jalaali year is in the format
      ✓ should parse when only Jalaali month is in the format
      ✓ should parse when only Jalaali month string is in the format
      ✓ should parse when only Jalaali date is in the format
      ✓ should parse when Jalaali year and month are in the format
      ✓ should parse when Jalaali year and date are in the format
      ✓ should parse when Jalaali month and date are in the format
      ✓ should parse when Jalaali year, month and date are in the format
      ✓ should parse with complex format
      ✓ should parse format result
      ✓ should be able to parse in utc
      ✓ should parse with a format array
    #format
      ✓ should work normally when there is no Jalaali token
      ✓ should format to Jalaali with Jalaali tokens
      ✓ should format with escaped and unescaped tokens
      ✓ should format with mixed tokens
      ✓ should format with jMo
      ✓ should format with jM
      ✓ should format with jMM
      ✓ should format with jMMM
      ✓ should format with jMMMM
      ✓ should format with jDo
      ✓ should format with jD
      ✓ should format with jDD
      ✓ should format with jDDD
      ✓ should format with jDDDo
      ✓ should format with jDDDD
      ✓ should format with jwo
      ✓ should format with jw
      ✓ should format with jww
      ✓ should format with jYY
      ✓ should format with jYYYY
      ✓ should format with jYYYYY
      ✓ should format with jgg
      ✓ should format with jgggg
      ✓ should format with jggggg
      ✓ should work with long date formats too
      ✓ should work with long date formats too if we have time
    #jYear
      ✓ should return Jalaali year
      ✓ should set Jalaali year
      ✓ should also has jYears alias
    #jMonth
      ✓ should return Jalaali month
      ✓ should set Jalaali month
      ✓ should also has jMonths alias
    #jDate
      ✓ should return Jalaali date
      ✓ should set Jalaali date
      ✓ should also has jDates alias
    #jDayOfYear
      ✓ should return Jalaali date of year
      ✓ should set Jalaali date of year
    #jWeek
      ✓ should return Jalaali week of year
      ✓ should set Jalaali week of year
    #jWeekYear
      ✓ should return Jalaali week year
      ✓ should set Jalaali week year
    #startOf
      ✓ should work as expected without jYear and jMonth
      ✓ should return start of Jalaali year, month and date
    #endOf
      ✓ should work as expected without jYear and jMonth
      ✓ should return end of Jalaali year, month and date
    #isValid
      ✓ should return true when a valid date is parsed and false otherwise
    #isValid-strict
      ✓ should return false when gregorian date is not strictly valid
      ✓ should return false when jalaali date is not strictly valid
    #clone
      ✓ should return a cloned instance
    #add
      ✓ should add gregorian dates correctly
      ✓ should add jalaali dates correctly
      ✓ should retain last day of month when adding months or years
    #subtract
      ✓ should subtract gregorian dates correctly
      ✓ should subtract jalaali dates correctly
      ✓ should retain last day of month when subtracting months or years
      ✓ should subtract months correctly
    .jIsLeapYear
      ✓ should return true for Jalaali leap years and false otherwise
    .loadPersian
      ✓ should load Persian lang
    .loadPersian({usePersianDigits: true})
      ✓ should load Persian lang with usePersianDigits = true
    .loadPersian({dialect: persian-modern})
      ✓ should load Persian lang with dialect = persian-modern
    .unix
      ✓ should create a moment with unix epoch
    #isSame
      ✓ should work correctly for same year
      ✓ should work correctly for same month
      ✓ should work correctly for same day
      ✓ should work correctly for same jyear
      ✓ should work correctly for same jmonth

  80 passing (165ms)
soroushm commented 5 years ago

Seems fine in code sand box https://codesandbox.io/s/5y6qn0xwnx

is that any tip or trick ?

soroushm commented 5 years ago

@behrang dude issues coming because require('moment/locale/fa') is removed as see in below link and humanize function is not exist humanize will be calculate x time ago

https://github.com/jalaali/moment-jalaali/commit/3ed634c3b7dff16d4eb3845edf14b70c247762f2

index.js

jMoment.loadPersian = function (args) {
  var usePersianDigits =  args !== undefined && args.hasOwnProperty('usePersianDigits') ? args.usePersianDigits : false
  var dialect =  args !== undefined && args.hasOwnProperty('dialect') ? args.dialect : 'persian'
  require('moment/locale/fa')

i just add require('moment/locale/fa') into 782 line and problem getting fix

soroushm commented 5 years ago

@alitaheri can you take a look and take care/accept the pull request? I'm a bit rushed

alitaheri commented 5 years ago

This will break some other module system. we've added and removed this line so many time now.

This was a bad design decision made by the moment developers. I suggest you use your own fork for this library. as merging this will upset others. and not doing so will keep breaking your code.

you can easily use your own modified fork from package.json

"dependencies": {
 ...
  "moment-jalaali": "soroushm/moment-jalaali",
...
}

for more info: https://docs.npmjs.com/cli/install.html

behrang commented 5 years ago

Hey @alitaheri, There are many issues related to this: #131, #138, #142, #143, and #160.

I'm not very familiar with the problem. I was just thinking maybe adding the require line inside loadPersian method, while also changing the builder scripts in the builder directory (adding a middle2.js file for registering moment/locale/fa or ./locale/fa) might fix the problem.

Or am I missing another thing?

soroushm commented 5 years ago

@alitaheri even add in function scope ? can you test when required inside function scope or loadPersian ?

soroushm commented 5 years ago

@behrang temporary i update the document and send a pull request

behrang commented 5 years ago

I saw it, but let's wait for Ali on this.

P.S: In your pull request, require has a typo in it.

soroushm commented 5 years ago

whats is mistake same work for me ?

behrang commented 5 years ago

Added a comment on your pull request. We better wait for Ali for a few days first.

alitaheri commented 5 years ago

This is a lot better 👍

We better leave it to the end user to solve this for their specific case. I agree with your solution

soroushm commented 5 years ago

So we can close all those issue ?

mohamadsdg commented 5 years ago

"moment-jalaali": "^0.8.3", this version still not work in react-native Requiring unknown module "./locale/fa" makes the app crashed Any idea? @soroushm @alitaheri @behrang

soroushm commented 5 years ago

Hi @mohamadsdg ,

you need to import this two things

import moment from "moment-jalaali";
import fa from "moment/src/locale/fa";
moment.locale("fa", fa);
moment.loadPersian();

as seen on this link fa is exist

https://github.com/moment/moment/blob/develop/locale/fa.js