Giter Club home page Giter Club logo

date-fns-upgrade-codemod's Introduction

date-fns-codemod

Usage

npx @date-fns/upgrade-codemod <path> [...options]

  • path files or directory to transform
  • --dry option for a dry-run
  • --print option to print the output for comparison

Example

npx @date-fns/upgrade-codemod src/

Codemods applied

N.B. At the moment this codemod applies fixes ONLY for first 3 points of 2.0 date-fns CHANGELOG
You'll have to take care of all the other breaking changes.

Codemod imports required tools from @date-fns/upgrade (you will have to add it as dependency in your project if required) and wraps date-fns function call arguments accordingly.

+import { legacyParse, legacyParseMap, convertTokens } from '@date-fns/upgrade/v2'

 const dateIs = '2019-07-01'

 const someToken = 'MM-DD';
-const format = importedFormat('2019-07-01', someToken)
-const closestIndex = closestToIndex(new Date(2015, 8, 6), [
-  new Date(2015, 0, 1),
-  new Date(2016, 0, 1),
-  new Date(2017, 0, 1)
-])
-const addSecondsPlease = addSeconds(dateIs, 999)
-const isoDay = getThatDay(new Date())
+const format = importedFormat(
+  legacyParse('2019-07-01'),
+  convertTokens(someToken)
+)
+const closestIndex = closestToIndex(
+  legacyParse(new Date(2015, 8, 6)),
+  legacyParseMap([
+    new Date(2015, 0, 1),
+    new Date(2016, 0, 1),
+    new Date(2017, 0, 1)
+  ])
+)
+const addSecondsPlease = addSeconds(legacyParse(dateIs), 999)
+const isoDay = getThatDay(legacyParse(new Date()))

Codemod also changes import locations

 import * as importedFormat from 'date-fns/format'
-import closestToIndex from 'date-fns/closest_to_index'
-import addSeconds from 'date-fns/add_seconds'
-import getThatDay from 'date-fns/get_iso_day'
+import closestToIndex from 'date-fns/closestToIndex'
+import addSeconds from 'date-fns/addSeconds'
+import getThatDay from 'date-fns/getISODay'

PRs welcome!

date-fns-upgrade-codemod's People

Contributors

danieldunderfelt avatar dependabot[bot] avatar dkozickis avatar kossnocorp avatar taylorbryant avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

date-fns-upgrade-codemod's Issues

Cannot find module '@date-fns/upgrade/v2'

I ran the codemod with npx according to the README but I go this error and no files have been modified. I also installed the package in question but it did not help. Am I doing something wrong? I use Yarn btw.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.