Skip to content

Commit

Permalink
formatDate (#1)
Browse files Browse the repository at this point in the history
* add formatDate

* add function formatDate
  • Loading branch information
emersonlaurentino committed Nov 5, 2018
1 parent 32c2a0b commit 5246a95
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -20,6 +20,7 @@ JavaScript implementation of formulas for humans.
- [pi](#pi)
- [Functions](#functions)
- [concat](#concat)
- [formatDate](#formatDate)
- [join](#join)
- [now](#now)
- [toNumber](#toNumber)
Expand Down Expand Up @@ -111,6 +112,27 @@ concat(...text)
concat('Hello', 'World') // HelloWorld
```

### formatDate

Format a date using format string.

<b>Types</b>

Input: `string`<br>
Output: `string`

<b>Syntax</b>

```js
formatDate(date, format)
```

<b>Examples</b>

```js
formatDate(now(), 'YYYY-MM-DD') // 1999-11-06
```

### join

Inserts the first argument between the rest and returns their concatenation.
Expand Down
31 changes: 30 additions & 1 deletion src/constants.ts
@@ -1,4 +1,21 @@
export const MONTHS_NAMES: string[] = [
export const DAY_NAME: string[] = [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat',
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];

export const MONTH_NAME: string[] = [
'Jan',
'Feb',
'Mar',
Expand All @@ -11,4 +28,16 @@ export const MONTHS_NAMES: string[] = [
'Oct',
'Nov',
'Dec',
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
9 changes: 6 additions & 3 deletions src/index.test.ts
@@ -1,5 +1,5 @@
import { concat, e, join, now, pi, toNumber } from '.';
import { MONTHS_NAMES } from './constants';
import { concat, e, formatDate, join, now, pi, toNumber } from '.';
import { MONTH_NAME } from './constants';

describe('CONSTANTS', () => {
test('e', () => {
Expand All @@ -14,13 +14,16 @@ describe('FUNCTIONS', () => {
test('concat', () => {
expect(concat('text', 1, 'example')).toBe('text1example');
});
test('formatDate', () => {
expect(formatDate(now(), 'MMMM DDDYYYY, HH:mm')).toBe('text1example');
});
test('join', () => {
expect(join('/', 'text', 1, 'example')).toBe('text/1/example');
});
test('now', () => {
const date = new Date();
const day = date.getDate();
const month = MONTHS_NAMES[date.getMonth()];
const month = MONTH_NAME[date.getMonth()];
const year = date.getFullYear();

expect(now()).toBe(`${month} ${day}, ${year}`);
Expand Down
52 changes: 50 additions & 2 deletions src/index.ts
@@ -1,14 +1,62 @@
import { MONTHS_NAMES } from './constants';
import { DAY_NAME, MONTH_NAME } from './constants';

// CONSTANTS

/** The ratio of a circle's circumference to its diameter. */
export const pi: number = Math.PI;

/** The base of the natural logarithm. */
export const e: number = Math.E;

// FUNCTIONS

/** Concatenates its arguments and returns the result. */
export const concat: (...input: any) => string = (...input) => input.join('');

/** Format a date using format string. */
export const formatDate: (input: string, format: string) => string = (
input,
format
) => {
const date = new Date(input);
const pad = (val: string) => {
if (val.length < 2) {
return `0${val}`;
}
return val;
};
const d = date.getDate();
const D = date.getDay();
const H = date.getHours();
const m = date.getMinutes();
const M = date.getMonth();
const Y = date.getFullYear();
const flags: any = {
D: d,
DD: pad(String(d)),
DDD: DAY_NAME[D],
DDDD: DAY_NAME[D + 7],
H,
HH: pad(String(H)),
M: M + 1,
MM: pad(String(M + 1)),
MMM: MONTH_NAME[M],
MMMM: MONTH_NAME[M + 12],
YY: String(Y).slice(2),
YYYY: Y,
m,
mm: pad(String(m)),
};

return format.replace(/([YMDHm])\w*/g, match => {
if (match in flags) {
return flags[match];
}

return match;
});
};

/** Inserts the first argument betweenthe rest and returns their concatenation. */
export const join: (...input: any) => string = (...input) => {
const value = input[0];
Expand All @@ -20,7 +68,7 @@ export const join: (...input: any) => string = (...input) => {
export const now: () => string = () => {
const date = new Date();
const day = date.getDate();
const month = MONTHS_NAMES[date.getMonth()];
const month = MONTH_NAME[date.getMonth()];
const year = date.getFullYear();

return `${month} ${day}, ${year}`;
Expand Down

0 comments on commit 5246a95

Please sign in to comment.