@rightcapital/date-helpers 2.0.1-feature-add-date-helpers.58.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -0
- package/lib/date-helpers.d.ts +12 -0
- package/lib/date-helpers.d.ts.map +1 -0
- package/lib/date-helpers.js +69 -0
- package/lib/date-helpers.js.map +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 RightCapital
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# `@rightcapital/date-helpers`
|
|
2
|
+
|
|
3
|
+
A utility class providing various date formatting and parsing methods in TypeScript.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Convert a date string to `Date` object with `parseDateString`.
|
|
8
|
+
- Format dates into various string formats, such as ISO, US locale, and more.
|
|
9
|
+
- Handle a variety of date string formats for both parsing and formatting.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add @rightcapital/date-helpers
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Parse Date String
|
|
20
|
+
|
|
21
|
+
Convert a date string to a Date object using various possible formats.
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { DateHelpers } from '@rightcapital/date-helpers';
|
|
25
|
+
const date = DateHelpers.parseDateString('2023/01/01');
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Format to ISO Date String
|
|
29
|
+
|
|
30
|
+
Format a date as an ISO date string.
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { DateHelpers } from '@rightcapital/date-helpers';
|
|
34
|
+
|
|
35
|
+
const isoDateString = DateHelpers.formatISO(new Date(2023, 0, 1));
|
|
36
|
+
// Result: Sun Jan 01 2023 00:00:00 GMT+0000 (Coordinated Universal Time)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Format to US Locale
|
|
40
|
+
|
|
41
|
+
Format a date as a US locale date string.
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { DateHelpers } from '@rightcapital/date-helpers';
|
|
45
|
+
const usDateString = DateHelpers.formatUsLocaleDate(new Date(2023, 0, 31));
|
|
46
|
+
// Result: "01/31/2023"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Formats a date as a US locale time string.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { DateHelpers } from '@rightcapital/date-helpers';
|
|
53
|
+
const usDateString = DateHelpers.formatUsLocaleTime('2025/10/20 12:00:00');
|
|
54
|
+
// Result: "10/20/2025 12:00:00"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Format to "Month day, year"
|
|
58
|
+
|
|
59
|
+
Formats a date as a string in the "Month day, year" format.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const monthDayYearString = DateHelpers.formatMediumDate(new Date(2023, 0, 1));
|
|
63
|
+
// Result: "Jan 1, 2023"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## API Reference
|
|
67
|
+
|
|
68
|
+
[Here](/packages/date-helpers/docs/modules.md) is the API documentation for the modules included in the package.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type DateType = Date | string;
|
|
2
|
+
export declare class DateHelpers {
|
|
3
|
+
static usLocaleDateFormat: string;
|
|
4
|
+
static usLocaleTimeFormat: string;
|
|
5
|
+
static isoDateFormat: string;
|
|
6
|
+
static parseDateString(input: string): Date;
|
|
7
|
+
static formatIsoDate(dateSource: DateType): string;
|
|
8
|
+
static formatUsLocaleDate(dateSource: DateType, dateFormat?: string): string;
|
|
9
|
+
static formatUsLocaleTime(dateSource: DateType, dateFormat?: string): string;
|
|
10
|
+
static formatMediumDate(dateSource: DateType): string;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=date-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-helpers.d.ts","sourceRoot":"","sources":["../src/date-helpers.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,QAAQ,GAAG,IAAI,GAAG,MAAM,CAAC;AAUrC,qBAAa,WAAW;IAEtB,OAAc,kBAAkB,SAAgB;IAGhD,OAAc,kBAAkB,SAAyB;IAGzD,OAAc,aAAa,SAAgB;WAe7B,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;WAyDpC,aAAa,CAAC,UAAU,EAAE,QAAQ,GAAG,MAAM;WA6B3C,kBAAkB,CAC9B,UAAU,EAAE,QAAQ,EACpB,UAAU,CAAC,EAAE,MAAM,GAClB,MAAM;WA+BK,kBAAkB,CAC9B,UAAU,EAAE,QAAQ,EACpB,UAAU,CAAC,EAAE,MAAM,GAClB,MAAM;WA8BK,gBAAgB,CAAC,UAAU,EAAE,QAAQ,GAAG,MAAM;CAG7D"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DateHelpers = void 0;
|
|
4
|
+
const exceptions_1 = require("@rightcapital/exceptions");
|
|
5
|
+
const browser_1 = require("@sentry/browser");
|
|
6
|
+
const date_fns_1 = require("date-fns");
|
|
7
|
+
const getParsedDate = (dateSource) => typeof dateSource === 'string'
|
|
8
|
+
? DateHelpers.parseDateString(dateSource)
|
|
9
|
+
: dateSource;
|
|
10
|
+
class DateHelpers {
|
|
11
|
+
static parseDateString(input) {
|
|
12
|
+
let output = (0, date_fns_1.parseISO)(input);
|
|
13
|
+
if ((0, date_fns_1.isValid)(output)) {
|
|
14
|
+
return output;
|
|
15
|
+
}
|
|
16
|
+
const possibleFormats = [
|
|
17
|
+
'MM/dd/yy',
|
|
18
|
+
DateHelpers.usLocaleDateFormat,
|
|
19
|
+
DateHelpers.isoDateFormat,
|
|
20
|
+
'yyyy/MM/dd',
|
|
21
|
+
'M/d/yyyy',
|
|
22
|
+
'MMM d, yyyy',
|
|
23
|
+
'MMM yyyy',
|
|
24
|
+
DateHelpers.usLocaleTimeFormat,
|
|
25
|
+
];
|
|
26
|
+
possibleFormats.some((pattern) => {
|
|
27
|
+
output = (0, date_fns_1.parse)(input, pattern, new Date());
|
|
28
|
+
return (0, date_fns_1.isValid)(output);
|
|
29
|
+
});
|
|
30
|
+
if (!(0, date_fns_1.isValid)(output)) {
|
|
31
|
+
(0, browser_1.withScope)((scope) => {
|
|
32
|
+
scope.setLevel('error');
|
|
33
|
+
scope.setExtras({
|
|
34
|
+
input,
|
|
35
|
+
});
|
|
36
|
+
(0, browser_1.captureException)(new exceptions_1.InvalidArgumentException(`Invalid Date: unable to parse date string - ${input}`));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return output;
|
|
40
|
+
}
|
|
41
|
+
static formatIsoDate(dateSource) {
|
|
42
|
+
if (dateSource === '') {
|
|
43
|
+
return dateSource;
|
|
44
|
+
}
|
|
45
|
+
return (0, date_fns_1.formatISO)(getParsedDate(dateSource), {
|
|
46
|
+
representation: 'date',
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
static formatUsLocaleDate(dateSource, dateFormat) {
|
|
50
|
+
if (dateSource === '') {
|
|
51
|
+
return dateSource;
|
|
52
|
+
}
|
|
53
|
+
if (dateFormat) {
|
|
54
|
+
return (0, date_fns_1.format)(getParsedDate(dateSource), dateFormat);
|
|
55
|
+
}
|
|
56
|
+
return (0, date_fns_1.format)(getParsedDate(dateSource), DateHelpers.usLocaleDateFormat);
|
|
57
|
+
}
|
|
58
|
+
static formatUsLocaleTime(dateSource, dateFormat) {
|
|
59
|
+
return DateHelpers.formatUsLocaleDate(dateSource, dateFormat !== null && dateFormat !== void 0 ? dateFormat : DateHelpers.usLocaleTimeFormat);
|
|
60
|
+
}
|
|
61
|
+
static formatMediumDate(dateSource) {
|
|
62
|
+
return (0, date_fns_1.format)(getParsedDate(dateSource), 'MMM d, yyyy');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.DateHelpers = DateHelpers;
|
|
66
|
+
DateHelpers.usLocaleDateFormat = 'MM/dd/yyyy';
|
|
67
|
+
DateHelpers.usLocaleTimeFormat = 'MM/dd/yyyy HH:mm:ss';
|
|
68
|
+
DateHelpers.isoDateFormat = 'yyyy-MM-dd';
|
|
69
|
+
//# sourceMappingURL=date-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date-helpers.js","sourceRoot":"","sources":["../src/date-helpers.ts"],"names":[],"mappings":";;;AA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E,EAAE;YACrB,OAAO,UAAU,CAAC;SACnB;QAED,OAAO,IAAA,oBAAS,EAAC,aAAa,CAAC,UAAU,CAAC,EAAE;YAC1C,cAAc,EAAE,MAAM;SACvB,CAAC,CAAC;IACL,CAAC;IAoBM,MAAM,CAAC,kBAAkB,CAC9B,UAAoB,EACpB,UAAmB;QAGnB,IAAI,UAAU,KAAK,EAAE,EAAE;YACrB,OAAO,UAAU,CAAC;SACnB;QAED,IAAI,UAAU,EAAE;YACd,OAAO,IAAA,iBAAM,EAAC,aAAa,CAAC,UAAU,CAAC,EAAE,UAAU,CAAC,CAAC;SACtD;QAED,OAAO,IAAA,iBAAM,EAAC,aAAa,CAAC,UAAU,CAAC,EAAE,WAAW,CAAC,kBAAkB,CAAC,CAAC;IAC3E,CAAC;IAoBM,MAAM,CAAC,kBAAkB,CAC9B,UAAoB,EACpB,UAAmB;QAEnB,OAAO,WAAW,CAAC,kBAAkB,CACnC,UAAU,EACV,UAAU,aAAV,UAAU,cAAV,UAAU,GAAI,WAAW,CAAC,kBAAkB,CAC7C,CAAC;IACJ,CAAC;IAyBM,MAAM,CAAC,gBAAgB,CAAC,UAAoB;QACjD,OAAO,IAAA,iBAAM,EAAC,aAAa,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC,CAAC;IAC1D,CAAC;;AAlLH,kCAmLC;AAjLe,8BAAkB,GAAG,YAAY,CAAC;AAGlC,8BAAkB,GAAG,qBAAqB,CAAC;AAG3C,yBAAa,GAAG,YAAY,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rightcapital/date-helpers",
|
|
3
|
+
"version": "2.0.1-feature-add-date-helpers.58.1.0",
|
|
4
|
+
"description": "Format dates with date-fns.",
|
|
5
|
+
"author": "RightCapital Ecosystem team <npm-publisher@rightcapital.com>",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"date",
|
|
8
|
+
"date-fns",
|
|
9
|
+
"format",
|
|
10
|
+
"helpers",
|
|
11
|
+
"moment",
|
|
12
|
+
"parse",
|
|
13
|
+
"parser",
|
|
14
|
+
"TypeScript"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://www.rightcapital.com",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"packageManager": "pnpm@8.8.0",
|
|
19
|
+
"main": "lib/index.js",
|
|
20
|
+
"typings": "lib/index.d.ts",
|
|
21
|
+
"directories": {
|
|
22
|
+
"lib": "lib",
|
|
23
|
+
"test": "__tests__"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"lib"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"date-fns": "2.30.0",
|
|
30
|
+
"@rightcapital/exceptions": "1.2.4-feature-add-date-helpers.58.1.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@sentry/browser": "7.x",
|
|
34
|
+
"date-fns": "^2.29.3"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@sentry/browser": "7.73.0",
|
|
38
|
+
"typedoc": "0.25.2",
|
|
39
|
+
"typedoc-plugin-markdown": "3.16.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"test": "jest",
|
|
43
|
+
"build": "tsc",
|
|
44
|
+
"docs": "pnpm exec typedoc --plugin typedoc-plugin-markdown --out docs src/date-helpers.ts",
|
|
45
|
+
"eslint": "eslint --cache 'src/**/*.ts*'",
|
|
46
|
+
"lint": "pnpm prettier && pnpm eslint",
|
|
47
|
+
"lint:fix": "pnpm prettier --write && eslint 'src/**/*.ts*' --fix",
|
|
48
|
+
"prettier": "prettier \"{src,__tests__}/**/*.{tsx,ts}\" -l"
|
|
49
|
+
}
|
|
50
|
+
}
|