GithubHelp home page GithubHelp logo

namespace about moment HOT 6 CLOSED

moment avatar moment commented on May 6, 2024
namespace

from moment.

Comments (6)

timrwood avatar timrwood commented on May 6, 2024

Yeah, that's what I had done originally, but in order to use _.mixin, you can only have a single level of hierarchy of the functions you pass in.

I did notice that I had to prefix and suffix a lot of the functions, so it may make more sense to create a _.date() object that I prototype the other functions onto.

So, _.formatDate() would become _.date().format() and _.fromNow() would become _.date().fromNow().

I'll make a branch and try it out.

from moment.

timrwood avatar timrwood commented on May 6, 2024

Alright, here's a stab at cleaning up the _ namespace.

https://github.com/timrwood/underscore.date/blob/namespace/lib/underscore.date.js

So now, there would only be _.date(), _.now(), and _.isLeapYear(yearnumber). Everything else would be called on a _.date() variable like

_.date([2010, 0, 31]).add({M:1}).format("MMMM Do YYYY");
or
_([2010, 0, 31]).date().add({M:1}).format("MMMM Do YYYY");

instead of

_([2010, 0, 31]).chain().date().addTime({M:1}).formatDate("MMMM Do YYYY").value()

Also, I was able to strip out all the suffixes, so the _.date() prototype functions are now format(string), add(object), subtract(object), customize(object), from(dateInput, boolean), fromNow(boolean), isLeapYear(), and relative(number).

from moment.

dvv avatar dvv commented on May 6, 2024

Neat! In addition, what if upgrade _.isLeapYear(yearnumber) to take Date also? This would 1) improve usability (no need to snoop explicitly for getYear) and 2) allow to move it under _.date() ;)

from moment.

timrwood avatar timrwood commented on May 6, 2024

There are 2 instances of isLeapYear.
_.isLeapYear(number) and _.date().isLeapYear().

_.isLeapYear(number) takes a year number (eg. 2010) and returns true if that year is a leap year.

_.date().isLeapYear() basically maps to _.isLeapYear(this.date.getFullYear()), using the date's year.

I figured it might be useful to check if a year is a leap year without creating a date object.

from moment.

timrwood avatar timrwood commented on May 6, 2024

Pushed to master. Closing.

from moment.

dvv avatar dvv commented on May 6, 2024

Thanks! Soon will be heavily exploiting this lib ;)

from moment.

Related Issues (20)

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.