Skip to content
This repository has been archived by the owner on Jun 23, 2019. It is now read-only.

Commit

Permalink
💩 src: Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
motss committed Mar 16, 2019
1 parent dd3d98a commit 9b41355
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 89 deletions.
77 changes: 40 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

<hr />

[![Follow me][follow-me-badge]][follow-me-url]

[![Version][version-badge]][version-url]
[![Node version][node-version-badge]][node-version-url]
[![MIT License][mit-license-badge]][mit-license-url]
Expand All @@ -27,15 +29,14 @@

> Returns a [JavaScript date object][date-mdn-url] using the [UTC][utc-url] timezone with optional offsets to adjust the `year`, `month`, or `date`.
## Table of contents
## Table of contents <!-- omit in toc -->

- [Table of contents](#table-of-contents)
- [Pre-requisites](#pre-requisites)
- [Setup](#setup)
- [Install](#install)
- [Usage](#usage)
- [Node.js](#nodejs)
- [Native ES modules or TypeScript](#native-es-modules-or-typescript)
- [Install](#install)
- [Usage](#usage)
- [TypeScript or ES Modules](#typescript-or-es-modules)
- [Node.js](#nodejs)
- [API Reference](#api-reference)
- [UTCDateParams](#utcdateparams)
- [utcDate([UTCDateParams])](#utcdateutcdateparams)
Expand All @@ -50,26 +51,26 @@

## Setup

### Install
## Install

```sh
# Install via NPM
$ npm install --save @motss/utc-date
$ npm install --save utc-date
```

### Usage
## Usage

#### Node.js
### TypeScript or ES Modules

```js
const {
```ts
import {
utcDate,
// utcDateSync,
} = require('@motss/utc-date');
} from 'utc-date';

void async function main() {
/** NOTE: Assuming today's date is '2020-02-02', */
const defaultUTCDate = await utcDate(); // utcDateSync();
const defaultUTCDate = await utcDate();
const defaultUTCDateWithOffsets = await utcDate({
offset: {
year: 2,
Expand All @@ -80,26 +81,24 @@ void async function main() {
const specifiedUTCDate = await utcDate({
startDate: '2030-03-02',
});

assert(defaultUTCDate, new Date('2020-02-02T00:00:00.000Z')); // OK
assert(defaultUTCDateWithOffsets, new Date('2022-03-02T00:00:00.000Z')); // OK
assert(specifiedUTCDate, new Date('2030-03-02T00:00:00.000Z')); // OK
}();
```

#### Native ES modules or TypeScript

```ts
// @ts-check
### Node.js

import {
```js
const {
utcDate,
// utcDateSync,
} from '@motss/utc-date';
} = require('utc-date');

void async function main() {
/** NOTE: Assuming today's date is '2020-02-02', */
const defaultUTCDate = await utcDate();
const defaultUTCDate = await utcDate(); // utcDateSync();
const defaultUTCDateWithOffsets = await utcDate({
offset: {
year: 2,
Expand All @@ -110,7 +109,7 @@ void async function main() {
const specifiedUTCDate = await utcDate({
startDate: '2030-03-02',
});

assert(defaultUTCDate, new Date('2020-02-02T00:00:00.000Z')); // OK
assert(defaultUTCDateWithOffsets, new Date('2022-03-02T00:00:00.000Z')); // OK
assert(specifiedUTCDate, new Date('2030-03-02T00:00:00.000Z')); // OK
Expand Down Expand Up @@ -167,33 +166,37 @@ This methods works the same as `utcDate([UTCDateParams])` except that this is th
[string-mdn-url]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

<!-- Badges -->
[version-badge]: https://flat.badgen.net/npm/v/@motss/utc-date
[node-version-badge]: https://flat.badgen.net/npm/node/@motss/utc-date
[mit-license-badge]: https://flat.badgen.net/npm/license/@motss/utc-date
[follow-me-badge]: https://flat.badgen.net/twitter/follow/igarshmyb?icon=twitter

[downloads-badge]: https://flat.badgen.net/npm/dm/@motss/utc-date
[total-downloads-badge]: https://flat.badgen.net/npm/dt/@motss/utc-date?label=total%20downloads
[packagephobia-badge]: https://flat.badgen.net/packagephobia/install/@motss/utc-date
[bundlephobia-badge]: https://flat.badgen.net/bundlephobia/minzip/@motss/utc-date
[version-badge]: https://flat.badgen.net/npm/v/utc-date?icon=npm
[node-version-badge]: https://flat.badgen.net/npm/node/utc-date
[mit-license-badge]: https://flat.badgen.net/npm/license/utc-date

[travis-badge]: https://flat.badgen.net/travis/motss/utc-date
[circleci-badge]: https://flat.badgen.net/circleci/github/motss/utc-date
[downloads-badge]: https://flat.badgen.net/npm/dm/utc-date
[total-downloads-badge]: https://flat.badgen.net/npm/dt/utc-date?label=total%20downloads
[packagephobia-badge]: https://flat.badgen.net/packagephobia/install/utc-date
[bundlephobia-badge]: https://flat.badgen.net/bundlephobia/minzip/utc-date

[travis-badge]: https://flat.badgen.net/travis/motss/utc-date?icon=travis
[circleci-badge]: https://flat.badgen.net/circleci/github/motss/utc-date?icon=circleci
[daviddm-badge]: https://flat.badgen.net/david/dep/motss/utc-date
[codecov-badge]: https://flat.badgen.net/codecov/c/github/motss/utc-date?label=codecov
[codecov-badge]: https://flat.badgen.net/codecov/c/github/motss/utc-date?label=codecov&icon=codecov
[coveralls-badge]: https://flat.badgen.net/coveralls/c/github/motss/utc-date?label=coveralls

[codebeat-badge]: https://codebeat.co/badges/1ed02b65-dca8-45a5-8719-cdead763a617?style=flat-square
[codacy-badge]: https://api.codacy.com/project/badge/Grade/1d15da734ee5424c8981d7e3e4d74c18?style=flat-square
[coc-badge]: https://flat.badgen.net/badge/code%20of/conduct/pink

<!-- Links -->
[version-url]: https://www.npmjs.com/package/@motss/utc-date
[follow-me-url]: https://twitter.com/igarshmyb?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=motss/utc-date

[version-url]: https://www.npmjs.com/package/utc-date
[node-version-url]: https://nodejs.org/en/download
[mit-license-url]: https://github.com/motss/utc-date/blob/master/LICENSE

[downloads-url]: http://www.npmtrends.com/@motss/utc-date
[packagephobia-url]: https://packagephobia.now.sh/result?p=%40motss%2Futc-date
[bundlephobia-url]: https://bundlephobia.com/result?p=@motss/utc-date
[downloads-url]: http://www.npmtrends.com/utc-date
[packagephobia-url]: https://packagephobia.now.sh/result?p=utc-date
[bundlephobia-url]: https://bundlephobia.com/result?p=utc-date

[travis-url]: https://travis-ci.org/motss/utc-date
[circleci-url]: https://circleci.com/gh/motss/utc-date/tree/master
Expand Down
59 changes: 18 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@motss/utc-date",
"name": "utc-date",
"version": "0.3.0",
"description": "Generate JavaScript's UTC dates with various offsets",
"keywords": [
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
type StartDate = string|number|Date;
interface UTCDateOffset {
year?: number;
month?: number;
day?: number;
}
interface UTCDateParams {
startDate?: string|number|Date;
offset?: UTCDateOffset;
}

export function utcDateSync(startDate?: StartDate, offset?: UTCDateOffset): Date {
export function utcDateSync({
startDate,
offset,
}: UTCDateParams = {}): Date {
const dated = null == startDate ? new Date() : new Date(startDate);
const { year, month, day }: UTCDateOffset = offset || {};
const validatedYear = +(null == year ? 0 : year);
Expand All @@ -30,8 +36,8 @@ export function utcDateSync(startDate?: StartDate, offset?: UTCDateOffset): Date
dated.getUTCDate() + validatedDay));
}

export async function utcDate(startDate?: StartDate, offset?: UTCDateOffset): Promise<Date> {
return utcDateSync(startDate, offset);
export async function utcDate(options?: UTCDateParams): Promise<Date> {
return utcDateSync(options);
}

export default utcDate;
12 changes: 6 additions & 6 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('utc-date', () => {

it(`throws when invalid 'year'`, async () => {
try {
await utcDate(null!, { year: NaN });
await utcDate({ offset: { year: NaN } });
} catch (e) {
expect(e).toStrictEqual(
new TypeError(`Expected 'year' to be a valid number, but received '${NaN}'`));
Expand All @@ -22,7 +22,7 @@ describe('utc-date', () => {

it(`throws when invalid 'month'`, async () => {
try {
await utcDate(null!, { month: NaN });
await utcDate({ offset: { month: NaN } });
} catch (e) {
expect(e).toStrictEqual(
new TypeError(`Expected 'month' to be a valid number, but received '${NaN}'`));
Expand All @@ -31,7 +31,7 @@ describe('utc-date', () => {

it(`throws when invalid 'day'`, async () => {
try {
await utcDate(null!, { day: NaN });
await utcDate({ offset: { day: NaN } });
} catch (e) {
expect(e).toStrictEqual(
new TypeError(`Expected 'day' to be a valid number, but received '${NaN}'`));
Expand All @@ -48,7 +48,7 @@ describe('utc-date', () => {
});

it(`returns UTC date with defined 'startDate'`, async () => {
const d = await utcDate('2018-03-03');
const d = await utcDate({ startDate: '2018-03-03' });

expect(d).toStrictEqual(new Date(new Date('2018-03-03').toJSON()));
});
Expand All @@ -57,13 +57,13 @@ describe('utc-date', () => {
const dated = new Date();
const exp = new Date(Date.UTC(
dated.getUTCFullYear() + 1, dated.getUTCMonth() + 2, dated.getUTCDate()));
const d = await utcDate(null!, { year: 1, month: 2 });
const d = await utcDate({ offset: { year: 1, month: 2 } });

expect(d).toStrictEqual(exp);
});

it(`returns UTC date with defined 'startDate' and 'offset'`, async () => {
const d = await utcDate('2018-03-03', { year: 1, month: 2 });
const d = await utcDate({ startDate: '2018-03-03', offset: { year: 1, month: 2 } });

expect(d).toStrictEqual(new Date(new Date('2019-05-03').toJSON()));
});
Expand Down

0 comments on commit 9b41355

Please sign in to comment.