npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cxing/moment-extension

v0.1.2

Published

Extending MomentJs with additional support and functionality

Downloads

36

Readme

moment-extension

demos

This is a decorator for MomentJs, adding simple math support for 'decade' and 'century', method aliases for comparison methods, simple range methods, and more.

It is written for modern browsers, and does not include CommonJs support.

There are no built-in dependencies to this project. As a decorator, it requires you to give it moment, even if previously decorated in some way.

Install

npm install @cxing/moment-extension

Usage

myMoment.js

import moment from 'moment';
import 'moment-timezone'; // optionally
import decorate from '@cxing/moment-extension';

export default decorate(moment);

myPage.js

import moment from './myMoment.js';

// Do something with it

Supporting decade and century

These simple math and comparison methods have been overridden to support decade and century as a date/time unit without changing their standard method signatures.

  • add
  • subtract
  • startOf
  • endOf
  • isSame
  • isBefore
  • isAfter
  • isSameOrBefore
  • isSameOrAfter

Comparison Method Aliases

We've also provided aliases for all of the comparison methods, as well as one new one.

  • isSame() -> eq()
  • isBefore() -> lt()
  • isSameOrBefore() -> lte()
  • isAfter() -> gt()
  • isSameOrAfter() -> gte()
  • New neq() (century and decade are also supported)

Simple Range Methods

We provided some simple Range Methods

  • moment.range({ start:moment, end:moment, [unit:string = 'day'], [step:number = 1] }) => moment[] returns an array of moments according to the step and unit, defaulting to 1/day from start to end
  • moment().inRange(start:moment, end:moment) => boolean tells you if you're current moment falls within a specific range

Calendar Convenience Methods

We also provide some basic convenience method for use with calendar scenarios.

  • moment().firstVisibleDay() => moment gets the first visible calendar day of the moment's month based upon what the locale gives for the first day of the week.
  • moment().lastVisibleDay() => moment gets the last visible calendar day of the moment's month based upon what the locale gives for the first day of the week.
  • moment().calendarDays([month:number = undefined]) => moment[] gets an array of moments representing the calendar days of the moment's month. If a month is provided then it will return the days of that month, without mutating the moment.
  • moment().calendarMonths([year:number = undefined]) => moment() gets an array of moments representing the first day of each month of the moment's year. If a year is provided then it will return the months of that year, without mutating the moment.
  • moment().calendarDecade([year:number = undefined]) => moment[] gets an array of moments representing the first day of each year of the moment's decade. If a year is provided then it will return the years of the corresponding decade, without mutating the moment.

Timezone Convenience Method

These are only relevant if you're using moment-timezone. A moment object doesn't contain zone information unless it was either created with moment.tz() or you've moment.tz.setDefault(someZone). The decorator will immediately setDefault(moment.tz.guess), so that all moments will have this information. We've also made it easier to set your timezone.

  • moment.setTimezone([zoneName|undefined]) If undefined is passed it will reset the default back to the moment.tz.guess(). If a zoneName is passed, and it is not a valid IANA timezone, it will throw an Error. If you have not included moment-timezone then this will do nothing without error
  • moment.currentIANAZoneName is a new property to access the current default IANA timezone name.

Once called, this ensures that all future moments are created using the supplied timezone. At an individual level this can still be overridden by using the moment.tz(value, zoneName) syntax.

Validation Convenience Methods

Some developers use moment for building out calendar or date/time picker controls. These convenience methods are provided to do 'strict' validation of text input against a format.

  • moment.isValidForFormat(value:string, format:string) => moment|undefined It the given value can be strictly parsed using the supplied format then it will return a moment. If it can not create a valid moment then it will return undefined.
  • moment.validateInputValue(value:string, format:string, [maskChar:string = '_']) => moment|undefined This will safely strip the input of any maskChars, then trim that value, the run it through isValidForFormat. If the input is incomplete (stripped maskChars) then it will fail 'strict' format validation and return undefined. If the input is complete, but incorrect, it will also fail 'strict' format validation.

Support Us

Did this help you? Help further our Open Source development and buy us a cup of coffee.

ko-fi