GithubHelp home page GithubHelp logo

you-dont-need / you-dont-need-momentjs Goto Github PK

View Code? Open in Web Editor NEW
13.2K 140.0 315.0 961 KB

List of functions which you can use to replace moment.js + ESLint Plugin

License: MIT License

JavaScript 100.00%
moment-js date-fns tree-shaking esmodules pure-function bundle-size hacktoberfest

you-dont-need-momentjs's Introduction

English | 简体中文

You Don't Need

Logo

People choose popular projects, often not because it applies to their problems.

Awesome

Contents

Packages

JavaScript Packages

Languages

Programming Practices

  • You Don't Need Loops ➿ - Loops are bullshit. Let's embrace wholemeal programming!
  • You Probably Don't Need Derived State (React) - As a general rule, derived state should be used sparingly. All problems with derived state that we have seen can be ultimately reduced to either unconditionally updating state from props or updating state whenever props and state don't match.
  • You Might Not Need to Transpile Your JavaScript - In other words if you're transpiling your JavaScript to ES5, you're making your code unnecessarily big and slow to support a minority of the users who will probably upgrade their system by the time you manage to configure your Webpack and Babel! 😉

Miscellaneous

you-dont-need-momentjs's People

Contributors

andrew-yangy avatar cht8687 avatar cylim avatar cymen avatar dependabot[bot] avatar frederik-b avatar jagomf avatar jgalat avatar jogai avatar jonatjano avatar justjavac avatar lasserafn avatar leafac avatar loganvolkers avatar lukaszwiktor avatar lukyth avatar molenzwiebel avatar odudek avatar reod avatar rkkautsar avatar runarberg avatar sahil1786 avatar sandstrom avatar steambap avatar stevemao avatar sukkaw avatar valbooth28 avatar vfonic avatar zac1st1k avatar zazzaz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

you-dont-need-momentjs's Issues

Sometimes there is no alternative to Moment.js

Moment.js can be still a useful (and perhaps even the only) library.

JavaScript is a widely used language, not only used by web-browsers.
Most alternative libraries require access to the internet in order to connect to the IANA timezone database and/or rely on Intl Object.

When you use for example mongo shell then

  • you don't have connection to the internet
  • the shell does not support Intl object. The new mongosh is still in beta-phase
  • you don't care about a 300k library. With MongoDB scripts you process much larger documents. Other popular libraries like Node.js or mongoose are even larger.

I am really happy that moment.js is still maintained. So far I did not find any suitable alternative library.

Include info on how to tree shake dynamically imported locales

If locale is required dynamically all languages in the date-fns are loaded into bundle (~160kb). However it is possible to use webpack to trim down the languages added to bundle using Context replacement plugin.

Usage example

const getLocale = (locale) => require(`date-fns/locale/${locale}/index.js`);

const formatDate = (date, formatStyle, locale) => {
    return format(date, formatStyle, {
        locale: getLocale(locale)
    });
};

Config example:

const supportedLocales = ["en", "de", "pl", "it"]
plugins: [
  new ContextReplacementPlugin(/date\-fns[\/\\]/, new RegExp(`[/\\\\\](${supportedLocales.join('|')})[/\\\\\]`)),
]

This results in a language bundle of ~23kb.

Add demo gif to README

Disclaimer: This is a bot

It looks like your repo is trending. The github_trending_videos Instgram account automatically shows the demo gifs of trending repos in Github.

Your README doesn't seem to have any demo gifs. Add one and the next time the parser runs it will pick it up and post it on its Instagram feed. If you don't want to just close this issue we won't bother you again.

Example should use UTC timezone

screenshot 2018-10-21 at 18 42 22

The output result will be affected by timezone, in case if anyone wants to add a new library in the example, output result might be different.

Day.js also support token 'Do' e.g. 9th

Day.js has a plugin advancedFormat that allows tokens like Do Q X

// current readme 
dayjs().format('dddd, MMMM D YYYY, h:mm:ss A');
// => "Sunday, September 9 2018, 7:12:49 PM" ⚠️  not support 9th

// however with plugin
import advancedFormat from 'dayjs/plugin/advancedFormat'
dayjs.extend(advancedFormat)
dayjs().format('Q Do k kk X x')

Besides, we might could update the screenshot and the description of this great project. Since it's not just the comparison between moment.js and date-dns, also Day.js and native Date object.

IsSame example is not equivalent

Hello, thanks for this great resource. I'm working on a new version of my date picker and I'm looking to remove momentjs. I was able to use some of the info here to whittle down the functions I needed. You are welcome to look at the full example on stackblitz as make use of what's there.

One thing I noticed is that your IsSame example with a granularity is not equivalent:
new Date(2010, 9, 20).toDateString().substring(4, 7) === new Date(2010, 9, 21).toDateString().substring(4, 7);

While technically does verify that both dates are in Oct this is not what moment/dayjs do. Those libs use their startOf function to take the dates to the beginning of the unit passed and then compare the valueOf. (I think the comparison of dates example should use valueOf as that's a more accurate comparison anyway).

In the provide example moment and dayjs would do the following

moment('2010-10-20').isSame('2010-10-21', 'month');
thisDate = 2010-10-1 //via startOf
compareDate = 2010-10-1
thisDate.valueOf() === compareDate.valueOf() //true

moment('2010-10-20').isSame('2021-10-21', 'month');
thisDate = 2010-10-1
compareDate = 2021-10-1
thisDate.valueOf() === compareDate.valueOf() //false

benchmark

I'd like to see the benchmark of momentjs and the alternative, and see which one is faster.

subDays into sub_days

hello I tried to use date_fns but there are some problems
I tried to import subDays from 'date-fns/subDays' but it doesn't work.
So I found out something thorough the document.

https://date-fns.org/v1.28.5/docs/subDays
According to this document, date-fns/subDays should be changed to date-fns/sub_days
Am I correct?

Why are native ‘add’ and ‘subtract’ different?

Native add reads:

const now = new Date();
now.setDate(now.getDate() + 7);

Native subtract reads:

new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7);

Why are they different? Would the approach used for add not work for subtract or vice-versa? (I tried them and they seemed to work, but when working with times there are so many edge cases that you can never be sure…)

Repo description update

List of date-fns or native functions which you can use to replace moment.js + ESLint Plugin

Thanks for this amazing repo. Above is the description of our repo. Since we have dayjs and luxon right now, should we update the description accordingly?

date-fns doesn't support LocaleData

this was a problem for me when I was migrating so I hope it saves anyone sometime intro trying to migrate just to realize you can't do this.

Unable to resolve dependency

Hullo, recently tried to add this plugin to a project only to be faced with some
dependency issue shenanigans

Error Log:
$ npm install --save-dev eslint-plugin-you-dont-need-momentjs
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: <Project Name>
npm ERR! Found: [email protected]
npm ERR! node_modules/eslint
npm ERR!   dev eslint@"^7.19.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer eslint@"^5.5.0" from [email protected]
npm ERR! node_modules/eslint-plugin-you-dont-need-momentjs
npm ERR!   dev eslint-plugin-you-dont-need-momentjs@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

I don't particularly feel like forcing the install, and help would be appreciated.

Non-date based time expressions lib

I made this pure vanilla library to work with time expressions some time ago. It's pretty small, well tested, completely documented, easy to use and works on both Node.js(any version) and browser(any browser/version).

Maybe somebody could get interested.

Ex.:

const Kairos = require('kairos');

const time = Kairos.new('10:30', 'hh:mm');
time.divide(2);
time.toString('hh:mm:ss'); // 05:15:00
time.multiply(3);
time.toString('hh:mm:ss'); // 15:45:00
time.getMinutes(); // 45
time.toMinutes(); // 945
time.addHours(100);
// Without overflow
time.toString('hh:mm:ss'); // 15:45:00
// With overflow
time.toString('hh:mm:ss', true); // 115:45:00

https://github.com/rodrigogs/kairos

Unable to set following rules to warn

The following rules give a warning if set to warn

1:1  warning  Definition for rule 'you-dont-need-momentjs/endOf' was not found  you-dont-need-momentjs/endOf
'you-dont-need-momentjs/dayOfYear': 'warn',
'you-dont-need-momentjs/isoWeeksInYear': 'warn',
'you-dont-need-momentjs/startOf': 'warn',
'you-dont-need-momentjs/endOf': 'warn',
'you-dont-need-momentjs/fromNow': 'warn',
'you-dont-need-momentjs/daysInMonth': 'warn',
'you-dont-need-momentjs/isBefore': 'warn',
'you-dont-need-momentjs/isSame': 'warn',
'you-dont-need-momentjs/isAfter': 'warn',
'you-dont-need-momentjs/isBetween': 'warn',
'you-dont-need-momentjs/isLeapYear': 'warn',
'you-dont-need-momentjs/isDate': 'warn',

Timezone support

You might mention that if you need to format dates for a timezone that's not that of the host then you can't use date-fns/native dates right now.

Day of Year breaks due to Daylight savings

Your Day Of Year Native code will fail between midnight and 1am during daylight savings.

My replacement was:

Math.floor( (new Date().setHours(2) - new Date(new Date().getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24 );

which forces the time for the day.

Alternatively:

Math.floor( (Date.UTC(year, month, day) - Date.UTC(year, 0, 0)) / 1000 / 60 / 60 / 24 )

The opposite code is quite simple, too:

new Date(year, 0 , dayOfYear)

IsBefore with an offset

How would I achieve something like this in native javascript :

moment(viewingDay).isBefore(moment(), 'day');

If this is possible then I can fully ditch moment

Equivalent for moment.utc APIs

momentjs supports using it either in the local timezone or in the UTC timezone. But this repo only gives alternatives for the local timezone case. It would be great to have alternatives for the UTC case too.

Add fix when using `--fix` flag

It would be amazing to have the ability to have eslint automatically fix files that violate this plugin's rule. This would make migration to date-fns and other packages much easier.

Thoughts on adding getting long month names (and possibly day names)?

It's simple enough to hard code it of course but I ran into this cool answer on StackOverflow on getting the long month names via Intl:

https://stackoverflow.com/questions/47232534/how-to-get-a-list-of-month-names-in-javascript-using-intl

I wondered if it might be worth adding to this guide. It would be awesome to get long and short day names too if that is possible.

Just wanted to ask before digging into it and to have a reminder (as have to come back to it later).

How do I parse certain locale dates without MomentJS?

The example I have would be an input box where the user is allowed to type in a date with their preferred locale format or modify the date shown to them. I am showing German locale because it usually causes typical Date.parse() functions to fail because it doesn't know the month and day are swapped.

function deriveDateFormat(locale) {
  const isoString1 = '1979-02-03'; // example date!
  const isoString2 = '79-2-3'; // example date!

  const intlString = new Date(isoString1).toLocaleDateString(locale, { timeZone: 'UTC' }); // generate a formatted date
  const dateParts1 = isoString1.split('-'); // prepare to replace with pattern parts
  const dateParts2 = isoString2.split('-'); // prepare to replace with pattern parts

  return intlString
    .replace(dateParts1[2], 'DD')
    .replace(dateParts1[1], 'MM')
    .replace(dateParts1[0], 'YYYY')
    .replace(dateParts2[2], 'D')
    .replace(dateParts2[1], 'M')
    .replace(dateParts2[0], 'YY');
}

const myLocale = 'de-DE';
let d = new Date('2023-01-30T05:00:00.000Z');
let dateFormatted = d.toLocaleDateString(myLocale);
// 30.1.2023

// user changes input field
dateFormatted = '31.1.2023';

let nativeParsed = new Date(dateFormatted).toISOString();
// Invalid Date -- WRONG!

let dateParsed = moment(dateFormatted, deriveDateFormat(myLocale)).toISOString();
// 2023-01-31T05:00:00.000Z -- CORRECT!

I will add that this is just an example. Assume I do not know what the user's locale is until the time of execution. I am aware that I could manually convert this particular locale, but I am looking for a generic solution.

Add js-joda

Would you accept a PR that adds js-joda? If so, I could create one.

js-joda is an immutable date and time library for JavaScript that is very close to the java.time API. It is not just a wrapper around the native Date object, like most other libraries. The API has a domain-oriented design with classes for each of the different use cases, such as LocalDate, ZonedDateTime or Period. Actually, it is quite similar to the upcoming javascript temporal proposal.

Add js-joda

Please consider adding js-joda. It is amazing!

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.