Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

upgradeGuide.md 3.9 KiB

3 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # v2 Upgrade Guide
  2. ## Common changes
  3. ### Camel case naming schema
  4. Function submodules now use camelCase naming schema:
  5. ```javascript
  6. // Before v2.0.0
  7. import differenceInCalendarISOYears from 'date-fns/difference_in_calendar_iso_years'
  8. // v2.0.0 onward
  9. import differenceInCalendarISOYears from 'date-fns/differenceInCalendarISOYears'
  10. ```
  11. ### New formatting tokens
  12. Starting with v2 `format` and `parse` uses [Unicode tokens].
  13. See [Unicode Tokens doc](./unicodeTokens.md) for more details.
  14. ### String arguments
  15. Functions now don't accept strings as arguments. Strings should
  16. be parsed using `parseISO` (ISO 8601) or `parse`.
  17. See [this post](https://blog.date-fns.org/post/we-cut-date-fns-v2-minimal-build-size-down-to-300-bytes-and-now-its-the-smallest-date-library-18f2nvh2z0yal) for more details.
  18. ```javascript
  19. // Before v2.0.0
  20. addDays('2016-01-01', 1)
  21. // v2.0.0 onward
  22. addDays(parseISO('2016-01-01'), 1)
  23. ```
  24. ### Arguments conversion
  25. All functions now implicitly convert arguments by following rules:
  26. | | date | number | string | boolean |
  27. | --------- | ------------ | ------ | ----------- | ------- |
  28. | 0 | new Date(0) | 0 | '0' | false |
  29. | '0' | Invalid Date | 0 | '0' | false |
  30. | 1 | new Date(1) | 1 | '1' | true |
  31. | '1' | Invalid Date | 1 | '1' | true |
  32. | true | Invalid Date | NaN | 'true' | true |
  33. | false | Invalid Date | NaN | 'false' | false |
  34. | null | Invalid Date | NaN | 'null' | false |
  35. | undefined | Invalid Date | NaN | 'undefined' | false |
  36. | NaN | Invalid Date | NaN | 'NaN' | false |
  37. Notes:
  38. - as before, arguments expected to be `Date` are converted to `Date` using _date-fns'_ `toDate` function;
  39. - arguments expected to be numbers are converted to integer numbers using our custom `toInteger` implementation
  40. (see [#765](https://github.com/date-fns/date-fns/pull/765));
  41. - arguments expected to be strings are converted to strings using JavaScript's `String` function;
  42. - arguments expected to be booleans are converted to boolean using JavaScript's `Boolean` function.
  43. `null` and `undefined` passed to optional arguments (i.e. properties of `options` argument)
  44. are ignored as if no argument was passed.
  45. If any argument is invalid (i.e. `NaN` for numbers and `Invalid Date` for dates),
  46. an invalid value will be returned:
  47. - `false` for functions that return booleans (expect `isValid`);
  48. - `Invalid Date` for functions that return dates;
  49. - `NaN` for functions that return numbers;
  50. - and `String('Invalid Date')` for functions that return strings.
  51. See tests and PRs [#460](https://github.com/date-fns/date-fns/pull/460) and
  52. [#765](https://github.com/date-fns/date-fns/pull/765) for exact behavior.
  53. ### `null`
  54. `null` now is not a valid date. `isValid(null)` returns `false`;
  55. `toDate(null)` returns an invalid date. Since `toDate` is used internally
  56. by all the functions, operations over `null` will also return an invalid date.
  57. [See #537](https://github.com/date-fns/date-fns/issues/537) for the reasoning.
  58. ### `RangeError`
  59. Functions now throw `RangeError` if optional values passed to `options`
  60. are not `undefined` or have expected values.
  61. This change is introduced for consistency with ECMAScript standard library which does the same.
  62. ### `TypeError`
  63. All functions now check if the passed number of arguments is less
  64. than the number of required arguments and throw `TypeError` exception if so.
  65. ### UMD/CDN
  66. The Bower & UMD/CDN package versions are no longer supported.
  67. ### New locale format
  68. See [docs/Locale](https://date-fns.org/docs/Locale).
  69. Locales renamed:
  70. - `en` → `en-US`
  71. - `zh_cn` → `zh-CN`
  72. - `zh_tw` → `zh-TW`
  73. ```javascript
  74. // Before v2.0.0
  75. import locale from 'date-fns/locale/zh_cn'
  76. // v2.0.0 onward
  77. import locale from 'date-fns/locale/zh-CN'
  78. ```
  79. [unicode tokens]: https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table