Skip to content

Commit

Permalink
Merge pull request #2 from FSolM/dev
Browse files Browse the repository at this point in the history
dev Pull Request
  • Loading branch information
FSolM authored Mar 11, 2020
2 parents 4a21843 + 5b97072 commit 1158a61
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

This `npm` module focuses on the date formatting of the `new Date()` JS object. It parses the result to a legible `string` state for both data manipulation and display.

**Currently it only parses the current date.**

## Usage & Installation

To use this module install it with either `npm`
Expand Down Expand Up @@ -48,7 +46,18 @@ const nonCompactFullDate = cdate('mm/dd/yy', { compact: false, fullDate: true })
// Monday, February 24 2020
```

If either the `format` of the date, or the `options` are wrong in some way, `cdate` will return `undefined`
Additionally, you can specify a custom date to parse with the same format passing a `instanceof` Date; the default parameter of this option will parse the current date:

```javascript
const dateUnspecified = cdate('mm/dd/yy', { compact: true, fullDate: false });
// Feb 24 2020

const dateSpecified = cdate('mm/dd/yy', { compact: true, fullDate: false }, new Date (2019, 10, 15));
// Nov 15 2019

```

If either the `format` of the date, the `options` are wrong in some way, or the `date` variable is not a valid instance of Date or an invalid date, `cdate` will return `undefined`.

#### Created by:

Expand Down
8 changes: 6 additions & 2 deletions lib/cdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import formatter from './helpers/formatter';
import getDays from './helpers/days';

export default (format = 'mm/dd/yy', options = { compact: true, fullDate: false }) => {
export default (format = 'mm/dd/yy', options = { compact: true, fullDate: false }, date = new Date()) => {
if (typeof options.compact !== 'boolean' || typeof options.fullDate !== 'boolean') {
console.error('You can only pass booleans in the options object');
return undefined;
}
if (!(date instanceof Date) || date === 'Invalid Date') {
console.error('The date attribute must be a Date type');
return undefined;
}
format.toLowerCase();
const d = new Date().toString().split(' ');
const d = date.toString().split(' ');
let date = formatter(d, format, options);
if (options.fullDate) {
return `${options.compact ? d[0] : getDays(d[0], true)}, ${date}`;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cdate-formatting",
"version": "0.5.6",
"version": "1.0.0",
"description": "A simple Node package library for quick JS Date formatting",
"main": "index.js",
"type": "module",
Expand Down

0 comments on commit 1158a61

Please sign in to comment.