Skip to content

Commit

Permalink
Merge pull request #1 from FSolM/dev
Browse files Browse the repository at this point in the history
dev Pull Request
  • Loading branch information
FSolM authored Feb 25, 2020
2 parents 39a6798 + f1af647 commit 4a21843
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 0 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# cDate Formatting
### Classic Date Formatting for JavaScript

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`

```
$ npm i cdate-formatting
```

Or `yarn`

```
$ yarn add cdate-formatting
```

To use `cdate`, import it to your project

```javascript
import cdate from 'cdate-formatting';

const date = cdate();
// returns 'Feb 24 2020' as a string
```

By default, `cdate` will return the current date in format *mm/dd/yy*, you can pass the function a string specifying the date order; it supports:

- mm/dd/yy
- dd/mm/yy
- yy/mm/dd
- yy/dd/mm

You can also specify if you want a compact date and the full date passing an object with the attributes `compact` & `fullDate` as booleans.

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

const compactFullDate = cdate('mm/dd/yy', { compact: true, fullDate: true });
// Mon, Feb 24 2020

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`

#### Created by:

Carlos Sol: [@FSolM](https://github.com/FSolM)
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/cdate');
19 changes: 19 additions & 0 deletions lib/cdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

import formatter from './helpers/formatter';
import getDays from './helpers/days';

export default (format = 'mm/dd/yy', options = { compact: true, fullDate: false }) => {
if (typeof options.compact !== 'boolean' || typeof options.fullDate !== 'boolean') {
console.error('You can only pass booleans in the options object');
return undefined;
}
format.toLowerCase();
const d = new Date().toString().split(' ');
let date = formatter(d, format, options);
if (options.fullDate) {
return `${options.compact ? d[0] : getDays(d[0], true)}, ${date}`;
} else {
return date;
}
}
25 changes: 25 additions & 0 deletions lib/helpers/days.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const longDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

export default (monthString) => {
switch (monthString) {
case 'Mon':
return longDays[0];
case 'Tue':
return longDays[1];
case 'Wed':
return longDays[2];
case 'Thu':
return longDays[3];
case 'Fri':
return longDays[4];
case 'Sat':
return longDays[5];
case 'Sun':
return longDays[6];
default:
console.error("Couldn't convert Days to string");
return undefined;
};
};
19 changes: 19 additions & 0 deletions lib/helpers/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

import getMonths from './months';

export default (d, format, compact) => {
switch (format) {
case 'mm/dd/yy':
return `${compact ? d[1] : getMonths(d[1])} ${d[2]} ${d[3]}`;
case 'dd/mm/yy':
return `${d[2]} ${compact ? d[1] : getMonths(d[1])} ${d[3]}`;
case 'yy/mm/dd':
return `${d[3]} ${d[2]} ${compact ? d[1] : getMonths(d[1])}`;
case 'yy/dd/mm':
return `${d[3]} ${compact ? d[1] : getMonths(d[1])} ${d[2]}`;
default:
console.error('Seems like the date format is wrong');
return undefined;
}
}
35 changes: 35 additions & 0 deletions lib/helpers/months.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const longMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

export default (monthString) => {
switch (monthString) {
case 'Jan':
return longMonths[0];
case 'Feb':
return longMonths[1];
case 'Mar':
return longMonths[2];
case 'Apr':
return longMonths[3];
case 'May':
return longMonths[4];
case 'Jun':
return longMonths[5];
case 'Jul':
return longMonths[6];
case 'Agu':
return longMonths[7];
case 'Sep':
return longMonths[8];
case 'Oct':
return longMonths[9];
case 'Nov':
return longMonths[10];
case 'Dic':
return longMonths[11];
default:
console.error("Couldn't convert Months to string");
return undefined;
};
};
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "cdate-formatting",
"version": "0.5.6",
"description": "A simple Node package library for quick JS Date formatting",
"main": "index.js",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/FSolM/cdate-formatting.git"
},
"keywords": [
"Date Formatter",
"Javascript Formatter",
"Javascript Date Formatter"
],
"author": "FSolM <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/FSolM/cdate-formatting/issues"
},
"homepage": "https://github.com/FSolM/cdate-formatting#readme"
}

0 comments on commit 4a21843

Please sign in to comment.