@herowcode/utils 1.0.1
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/README.md +224 -0
- package/dist/array/index.d.ts +3 -0
- package/dist/array/index.d.ts.map +1 -0
- package/dist/array/index.esm.js +2 -0
- package/dist/array/index.js +2 -0
- package/dist/array/index.js.map +1 -0
- package/dist/array/shuffle.d.ts +2 -0
- package/dist/array/shuffle.d.ts.map +1 -0
- package/dist/array/shuffle.esm.js +12 -0
- package/dist/array/shuffle.js +12 -0
- package/dist/array/shuffle.js.map +1 -0
- package/dist/array/unique.d.ts +2 -0
- package/dist/array/unique.d.ts.map +1 -0
- package/dist/array/unique.esm.js +3 -0
- package/dist/array/unique.js +3 -0
- package/dist/array/unique.js.map +1 -0
- package/dist/date/dayjs.d.ts +3 -0
- package/dist/date/dayjs.d.ts.map +1 -0
- package/dist/date/dayjs.esm.js +14 -0
- package/dist/date/dayjs.js +14 -0
- package/dist/date/dayjs.js.map +1 -0
- package/dist/date/fixTimezoneOffset.d.ts +3 -0
- package/dist/date/fixTimezoneOffset.d.ts.map +1 -0
- package/dist/date/fixTimezoneOffset.esm.js +5 -0
- package/dist/date/fixTimezoneOffset.js +5 -0
- package/dist/date/fixTimezoneOffset.js.map +1 -0
- package/dist/date/formatDate.d.ts +2 -0
- package/dist/date/formatDate.d.ts.map +1 -0
- package/dist/date/formatDate.esm.js +9 -0
- package/dist/date/formatDate.js +9 -0
- package/dist/date/formatDate.js.map +1 -0
- package/dist/date/getCurrentDateInUTC.d.ts +3 -0
- package/dist/date/getCurrentDateInUTC.d.ts.map +1 -0
- package/dist/date/getCurrentDateInUTC.esm.js +4 -0
- package/dist/date/getCurrentDateInUTC.js +4 -0
- package/dist/date/getCurrentDateInUTC.js.map +1 -0
- package/dist/date/getDateInUTC.d.ts +3 -0
- package/dist/date/getDateInUTC.d.ts.map +1 -0
- package/dist/date/getDateInUTC.esm.js +4 -0
- package/dist/date/getDateInUTC.js +4 -0
- package/dist/date/getDateInUTC.js.map +1 -0
- package/dist/date/index.d.ts +7 -0
- package/dist/date/index.d.ts.map +1 -0
- package/dist/date/index.esm.js +6 -0
- package/dist/date/index.js +6 -0
- package/dist/date/index.js.map +1 -0
- package/dist/date/parseTimeSpent.d.ts +2 -0
- package/dist/date/parseTimeSpent.d.ts.map +1 -0
- package/dist/date/parseTimeSpent.esm.js +85 -0
- package/dist/date/parseTimeSpent.js +85 -0
- package/dist/date/parseTimeSpent.js.map +1 -0
- package/dist/function.d.ts +18 -0
- package/dist/function.d.ts.map +1 -0
- package/dist/function.esm.js +54 -0
- package/dist/function.js +54 -0
- package/dist/function.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +10 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/number.d.ts +8 -0
- package/dist/number.d.ts.map +1 -0
- package/dist/number.esm.js +15 -0
- package/dist/number.js +15 -0
- package/dist/number.js.map +1 -0
- package/dist/string/camel-case.d.ts +2 -0
- package/dist/string/camel-case.d.ts.map +1 -0
- package/dist/string/camel-case.esm.js +21 -0
- package/dist/string/camel-case.js +21 -0
- package/dist/string/camel-case.js.map +1 -0
- package/dist/string/capitalize.d.ts +2 -0
- package/dist/string/capitalize.d.ts.map +1 -0
- package/dist/string/capitalize.esm.js +9 -0
- package/dist/string/capitalize.js +9 -0
- package/dist/string/capitalize.js.map +1 -0
- package/dist/string/index.d.ts +9 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.esm.js +8 -0
- package/dist/string/index.js +8 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/kebab-case.d.ts +2 -0
- package/dist/string/kebab-case.d.ts.map +1 -0
- package/dist/string/kebab-case.esm.js +11 -0
- package/dist/string/kebab-case.js +11 -0
- package/dist/string/kebab-case.js.map +1 -0
- package/dist/string/remove-html-tags.d.ts +2 -0
- package/dist/string/remove-html-tags.d.ts.map +1 -0
- package/dist/string/remove-html-tags.esm.js +14 -0
- package/dist/string/remove-html-tags.js +14 -0
- package/dist/string/remove-html-tags.js.map +1 -0
- package/dist/string/slugify.d.ts +2 -0
- package/dist/string/slugify.d.ts.map +1 -0
- package/dist/string/slugify.esm.js +9 -0
- package/dist/string/slugify.js +9 -0
- package/dist/string/slugify.js.map +1 -0
- package/dist/string/snake-case.d.ts +2 -0
- package/dist/string/snake-case.d.ts.map +1 -0
- package/dist/string/snake-case.esm.js +11 -0
- package/dist/string/snake-case.js +11 -0
- package/dist/string/snake-case.js.map +1 -0
- package/dist/string/to-sentence-case.d.ts +2 -0
- package/dist/string/to-sentence-case.d.ts.map +1 -0
- package/dist/string/to-sentence-case.esm.js +9 -0
- package/dist/string/to-sentence-case.js +9 -0
- package/dist/string/to-sentence-case.js.map +1 -0
- package/dist/string/truncate.d.ts +2 -0
- package/dist/string/truncate.d.ts.map +1 -0
- package/dist/string/truncate.esm.js +15 -0
- package/dist/string/truncate.js +15 -0
- package/dist/string/truncate.js.map +1 -0
- package/package.json +92 -0
package/README.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# @herowcode/utils
|
|
2
|
+
|
|
3
|
+
A lightweight collection of utility functions for everyday JavaScript/TypeScript development. Built with dayjs for powerful date manipulation.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 **Powered by dayjs** - Robust date manipulation with dayjs integration
|
|
8
|
+
- 📦 **TypeScript support** - Full type definitions included
|
|
9
|
+
- 🧪 **Well tested** - Comprehensive test coverage
|
|
10
|
+
- 📱 **Universal** - Works in Node.js and browsers
|
|
11
|
+
- 🎯 **Tree-shakable** - Only import what you need
|
|
12
|
+
- 📂 **Scoped exports** - Import from specific modules
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @herowcode/utils
|
|
18
|
+
# or
|
|
19
|
+
yarn add @herowcode/utils
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Import everything:
|
|
25
|
+
```typescript
|
|
26
|
+
import { formatDate, capitalize, debounce } from '@herowcode/utils';
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Import by scope:
|
|
30
|
+
```typescript
|
|
31
|
+
import { formatDate, addDays } from '@herowcode/utils/date';
|
|
32
|
+
import { capitalize, camelCase } from '@herowcode/utils/string';
|
|
33
|
+
import { randomInt } from '@herowcode/utils/number';
|
|
34
|
+
import { debounce, throttle } from '@herowcode/utils/function';
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Examples:
|
|
38
|
+
```typescript
|
|
39
|
+
// Date formatting with dayjs power
|
|
40
|
+
console.log(formatDate(new Date(), 'DD/MM/YYYY')); // "08/09/2025"
|
|
41
|
+
console.log(formatDate('2023-12-25', 'MMMM Do, YYYY')); // "December 25th, 2023"
|
|
42
|
+
|
|
43
|
+
// String utilities
|
|
44
|
+
console.log(capitalize('hello world')); // "Hello world"
|
|
45
|
+
console.log(camelCase('hello-world')); // "helloWorld"
|
|
46
|
+
console.log(kebabCase('helloWorld')); // "hello-world"
|
|
47
|
+
|
|
48
|
+
// Function utilities
|
|
49
|
+
const debouncedFn = debounce(() => console.log('Called!'), 300);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## API Reference
|
|
53
|
+
|
|
54
|
+
### Date Utilities
|
|
55
|
+
|
|
56
|
+
#### `formatDate(date: Date, format: string): string`
|
|
57
|
+
|
|
58
|
+
Formats a date according to the specified format.
|
|
59
|
+
|
|
60
|
+
**Supported format tokens:**
|
|
61
|
+
- `YYYY` - 4-digit year
|
|
62
|
+
- `YY` - 2-digit year
|
|
63
|
+
- `MM` - 2-digit month (01-12)
|
|
64
|
+
- `DD` - 2-digit day (01-31)
|
|
65
|
+
- `HH` - 2-digit hours (00-23)
|
|
66
|
+
- `mm` - 2-digit minutes (00-59)
|
|
67
|
+
- `ss` - 2-digit seconds (00-59)
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
formatDate(new Date('2023-12-25'), 'YYYY-MM-DD'); // "2023-12-25"
|
|
71
|
+
formatDate(new Date('2023-12-25'), 'DD/MM/YYYY'); // "25/12/2023"
|
|
72
|
+
formatDate(new Date('2023-12-25T10:30:45'), 'DD/MM/YYYY HH:mm:ss'); // "25/12/2023 10:30:45"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### `addDays(date: Date, amount: number): Date`
|
|
76
|
+
|
|
77
|
+
Adds a specified number of days to a date. Returns a new Date object.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const date = new Date('2023-12-25');
|
|
81
|
+
addDays(date, 5); // 2023-12-30
|
|
82
|
+
addDays(date, -5); // 2023-12-20
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
#### `diffInDays(date1: Date, date2: Date): number`
|
|
86
|
+
|
|
87
|
+
Calculates the difference in days between two dates.
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const date1 = new Date('2023-12-30');
|
|
91
|
+
const date2 = new Date('2023-12-25');
|
|
92
|
+
diffInDays(date1, date2); // 5
|
|
93
|
+
diffInDays(date2, date1); // -5
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
#### `isBefore(date1: Date, date2: Date): boolean`
|
|
97
|
+
|
|
98
|
+
Checks if the first date is before the second date.
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
const date1 = new Date('2023-12-20');
|
|
102
|
+
const date2 = new Date('2023-12-25');
|
|
103
|
+
isBefore(date1, date2); // true
|
|
104
|
+
isBefore(date2, date1); // false
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### `isAfter(date1: Date, date2: Date): boolean`
|
|
108
|
+
|
|
109
|
+
Checks if the first date is after the second date.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
const date1 = new Date('2023-12-30');
|
|
113
|
+
const date2 = new Date('2023-12-25');
|
|
114
|
+
isAfter(date1, date2); // true
|
|
115
|
+
isAfter(date2, date1); // false
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### String Utilities
|
|
119
|
+
|
|
120
|
+
#### `capitalize(str: string): string`
|
|
121
|
+
|
|
122
|
+
Capitalizes the first letter of a string and converts the rest to lowercase.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
capitalize('hello world'); // "Hello world"
|
|
126
|
+
capitalize('HELLO WORLD'); // "Hello world"
|
|
127
|
+
capitalize('hELLo WoRLd'); // "Hello world"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### `camelCase(str: string): string`
|
|
131
|
+
|
|
132
|
+
Converts a string to camelCase by removing special characters and capitalizing words.
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
camelCase('hello world'); // "helloWorld"
|
|
136
|
+
camelCase('hello-world'); // "helloWorld"
|
|
137
|
+
camelCase('hello_world_test'); // "helloWorldTest"
|
|
138
|
+
camelCase('The Quick Brown Fox'); // "theQuickBrownFox"
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Number Utilities
|
|
142
|
+
|
|
143
|
+
#### `randomInt(min: number, max: number): number`
|
|
144
|
+
|
|
145
|
+
Generates a random integer between min and max (inclusive).
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
randomInt(1, 10); // Random integer between 1 and 10
|
|
149
|
+
randomInt(0, 100); // Random integer between 0 and 100
|
|
150
|
+
randomInt(-5, 5); // Random integer between -5 and 5
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Function Utilities
|
|
154
|
+
|
|
155
|
+
#### `debounce<T>(fn: T, delay: number): T`
|
|
156
|
+
|
|
157
|
+
Creates a debounced function that delays invoking `fn` until after `delay` milliseconds have elapsed since the last time it was invoked.
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
const debouncedSearch = debounce((query: string) => {
|
|
161
|
+
console.log('Searching for:', query);
|
|
162
|
+
}, 300);
|
|
163
|
+
|
|
164
|
+
// Will only execute once after 300ms of no new calls
|
|
165
|
+
debouncedSearch('a');
|
|
166
|
+
debouncedSearch('ab');
|
|
167
|
+
debouncedSearch('abc'); // Only this will execute
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### `throttle<T>(fn: T, delay: number): T`
|
|
171
|
+
|
|
172
|
+
Creates a throttled function that only invokes `fn` at most once per every `delay` milliseconds.
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
const throttledScroll = throttle(() => {
|
|
176
|
+
console.log('Scroll event handled');
|
|
177
|
+
}, 100);
|
|
178
|
+
|
|
179
|
+
// Will execute immediately, then at most once every 100ms
|
|
180
|
+
window.addEventListener('scroll', throttledScroll);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Browser Support
|
|
184
|
+
|
|
185
|
+
This library supports all modern browsers and Node.js environments. It uses ES2018 features and requires:
|
|
186
|
+
|
|
187
|
+
- Node.js 10+
|
|
188
|
+
- Modern browsers (Chrome 63+, Firefox 58+, Safari 12+, Edge 79+)
|
|
189
|
+
|
|
190
|
+
## Development
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
# Install dependencies
|
|
194
|
+
npm install
|
|
195
|
+
|
|
196
|
+
# Run tests
|
|
197
|
+
npm test
|
|
198
|
+
|
|
199
|
+
# Run tests in watch mode
|
|
200
|
+
npm run test:watch
|
|
201
|
+
|
|
202
|
+
# Build the library
|
|
203
|
+
npm run build
|
|
204
|
+
|
|
205
|
+
# Run tests with coverage
|
|
206
|
+
npm run test:coverage
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## License
|
|
210
|
+
|
|
211
|
+
MIT © [HerowCode](https://github.com/herowcode)
|
|
212
|
+
|
|
213
|
+
## Contributing
|
|
214
|
+
|
|
215
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
216
|
+
|
|
217
|
+
## Changelog
|
|
218
|
+
|
|
219
|
+
### 1.0.0
|
|
220
|
+
- Initial release
|
|
221
|
+
- Date utilities: `formatDate`, `addDays`, `diffInDays`, `isBefore`, `isAfter`
|
|
222
|
+
- String utilities: `capitalize`, `camelCase`
|
|
223
|
+
- Number utilities: `randomInt`
|
|
224
|
+
- Function utilities: `debounce`, `throttle`
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/array/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/array/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAyB;AACzB,2CAAwB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shuffle.d.ts","sourceRoot":"","sources":["../../src/array/shuffle.ts"],"names":[],"mappings":"AAAA,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAc1C"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function shuffle(array) {
|
|
2
|
+
const shuffled = [...array];
|
|
3
|
+
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
4
|
+
// Get cryptographically strong random values
|
|
5
|
+
const randomBuffer = new Uint32Array(1);
|
|
6
|
+
crypto.getRandomValues(randomBuffer);
|
|
7
|
+
// Use modulo to get a value in our range
|
|
8
|
+
const j = randomBuffer[0] % (i + 1);
|
|
9
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
10
|
+
}
|
|
11
|
+
return shuffled;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function shuffle(array) {
|
|
2
|
+
const shuffled = [...array];
|
|
3
|
+
for (let i = shuffled.length - 1; i > 0; i--) {
|
|
4
|
+
// Get cryptographically strong random values
|
|
5
|
+
const randomBuffer = new Uint32Array(1);
|
|
6
|
+
crypto.getRandomValues(randomBuffer);
|
|
7
|
+
// Use modulo to get a value in our range
|
|
8
|
+
const j = randomBuffer[0] % (i + 1);
|
|
9
|
+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
|
10
|
+
}
|
|
11
|
+
return shuffled;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shuffle.js","sourceRoot":"","sources":["../../src/array/shuffle.ts"],"names":[],"mappings":";;AAAA,0BAcC;AAdD,SAAgB,OAAO,CAAI,KAAU;IACnC,MAAM,QAAQ,GAAG,CAAC,GAAG,KAAK,CAAC,CAAA;IAC3B,KAAK,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7C,6CAA6C;QAC7C,MAAM,YAAY,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,CAAA;QACvC,MAAM,CAAC,eAAe,CAAC,YAAY,CAAC,CAAA;QAEpC,yCAAyC;QACzC,MAAM,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAGlC;QAAA,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;IAC1D,CAAC;IACD,OAAO,QAAQ,CAAA;AACjB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unique.d.ts","sourceRoot":"","sources":["../../src/array/unique.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAEzC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unique.js","sourceRoot":"","sources":["../../src/array/unique.ts"],"names":[],"mappings":";;AAAA,wBAEC;AAFD,SAAgB,MAAM,CAAI,KAAU;IAClC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,CAAA;AAC5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dayjs.d.ts","sourceRoot":"","sources":["../../src/date/dayjs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAezB,eAAO,MAAM,KAAK,cAAQ,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import dayJs from "dayjs";
|
|
2
|
+
import advancedFormat from "dayjs/plugin/advancedFormat.js";
|
|
3
|
+
import customParseFormat from "dayjs/plugin/customParseFormat.js";
|
|
4
|
+
import localizedFormat from "dayjs/plugin/localizedFormat.js";
|
|
5
|
+
import relativeTime from "dayjs/plugin/relativeTime.js";
|
|
6
|
+
import timezone from "dayjs/plugin/timezone.js";
|
|
7
|
+
import utc from "dayjs/plugin/utc.js";
|
|
8
|
+
dayJs.extend(utc);
|
|
9
|
+
dayJs.extend(customParseFormat);
|
|
10
|
+
dayJs.extend(advancedFormat);
|
|
11
|
+
dayJs.extend(timezone);
|
|
12
|
+
dayJs.extend(localizedFormat);
|
|
13
|
+
dayJs.extend(relativeTime);
|
|
14
|
+
export const dayjs = dayJs;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import dayJs from "dayjs";
|
|
2
|
+
import advancedFormat from "dayjs/plugin/advancedFormat.js";
|
|
3
|
+
import customParseFormat from "dayjs/plugin/customParseFormat.js";
|
|
4
|
+
import localizedFormat from "dayjs/plugin/localizedFormat.js";
|
|
5
|
+
import relativeTime from "dayjs/plugin/relativeTime.js";
|
|
6
|
+
import timezone from "dayjs/plugin/timezone.js";
|
|
7
|
+
import utc from "dayjs/plugin/utc.js";
|
|
8
|
+
dayJs.extend(utc);
|
|
9
|
+
dayJs.extend(customParseFormat);
|
|
10
|
+
dayJs.extend(advancedFormat);
|
|
11
|
+
dayJs.extend(timezone);
|
|
12
|
+
dayJs.extend(localizedFormat);
|
|
13
|
+
dayJs.extend(relativeTime);
|
|
14
|
+
export const dayjs = dayJs;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dayjs.js","sourceRoot":"","sources":["../../src/date/dayjs.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAyB;AACzB,uFAA2D;AAC3D,6FAAiE;AACjE,yFAA6D;AAC7D,mFAAuD;AACvD,2EAA+C;AAC/C,iEAAqC;AAErC,eAAK,CAAC,MAAM,CAAC,gBAAG,CAAC,CAAA;AACjB,eAAK,CAAC,MAAM,CAAC,8BAAiB,CAAC,CAAA;AAC/B,eAAK,CAAC,MAAM,CAAC,2BAAc,CAAC,CAAA;AAC5B,eAAK,CAAC,MAAM,CAAC,qBAAQ,CAAC,CAAA;AACtB,eAAK,CAAC,MAAM,CAAC,4BAAe,CAAC,CAAA;AAC7B,eAAK,CAAC,MAAM,CAAC,yBAAY,CAAC,CAAA;AAEb,QAAA,KAAK,GAAG,eAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixTimezoneOffset.d.ts","sourceRoot":"","sources":["../../src/date/fixTimezoneOffset.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;AAIlC,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,IAAI,GAAG,MAAM,GAAG,KAAK,CAG/D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixTimezoneOffset.js","sourceRoot":"","sources":["../../src/date/fixTimezoneOffset.ts"],"names":[],"mappings":";;AAIA,8CAGC;AALD,mCAA+B;AAE/B,SAAgB,iBAAiB,CAAC,OAAsB;IACtD,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC,iBAAiB,EAAE,CAAA;IACpD,OAAO,IAAA,aAAK,EAAC,OAAO,CAAC,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;AAC7C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatDate.d.ts","sourceRoot":"","sources":["../../src/date/formatDate.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CACxB,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC5B,MAAM,GAAE,IAAI,CAAC,eAAyB,EACtC,IAAI,GAAE,IAAI,CAAC,qBAA0B,UAQtC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function formatDate(date, locale = "pt-BR", opts = {}) {
|
|
2
|
+
var _a, _b, _c;
|
|
3
|
+
return new Intl.DateTimeFormat(locale, {
|
|
4
|
+
month: (_a = opts.month) !== null && _a !== void 0 ? _a : "long",
|
|
5
|
+
day: (_b = opts.day) !== null && _b !== void 0 ? _b : "numeric",
|
|
6
|
+
year: (_c = opts.year) !== null && _c !== void 0 ? _c : "numeric",
|
|
7
|
+
...opts,
|
|
8
|
+
}).format(new Date(date));
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export function formatDate(date, locale = "pt-BR", opts = {}) {
|
|
2
|
+
var _a, _b, _c;
|
|
3
|
+
return new Intl.DateTimeFormat(locale, {
|
|
4
|
+
month: (_a = opts.month) !== null && _a !== void 0 ? _a : "long",
|
|
5
|
+
day: (_b = opts.day) !== null && _b !== void 0 ? _b : "numeric",
|
|
6
|
+
year: (_c = opts.year) !== null && _c !== void 0 ? _c : "numeric",
|
|
7
|
+
...opts,
|
|
8
|
+
}).format(new Date(date));
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatDate.js","sourceRoot":"","sources":["../../src/date/formatDate.ts"],"names":[],"mappings":";;AAAA,gCAWC;AAXD,SAAgB,UAAU,CACxB,IAA4B,EAC5B,SAA+B,OAAO,EACtC,OAAmC,EAAE;;IAErC,OAAO,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE;QACrC,KAAK,EAAE,MAAA,IAAI,CAAC,KAAK,mCAAI,MAAM;QAC3B,GAAG,EAAE,MAAA,IAAI,CAAC,GAAG,mCAAI,SAAS;QAC1B,IAAI,EAAE,MAAA,IAAI,CAAC,IAAI,mCAAI,SAAS;QAC5B,GAAG,IAAI;KACR,CAAC,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;AAC3B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCurrentDateInUTC.d.ts","sourceRoot":"","sources":["../../src/date/getCurrentDateInUTC.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;AAIlC,wBAAgB,mBAAmB,IAAI,KAAK,CAE3C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCurrentDateInUTC.js","sourceRoot":"","sources":["../../src/date/getCurrentDateInUTC.ts"],"names":[],"mappings":";;AAIA,kDAEC;AAJD,mCAA+B;AAE/B,SAAgB,mBAAmB;IACjC,OAAO,IAAA,aAAK,GAAE,CAAC,GAAG,EAAE,CAAA;AACtB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getDateInUTC.d.ts","sourceRoot":"","sources":["../../src/date/getDateInUTC.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAA;AAIlC,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK,CAE9C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getDateInUTC.js","sourceRoot":"","sources":["../../src/date/getDateInUTC.ts"],"names":[],"mappings":";;AAIA,oCAEC;AAJD,mCAA+B;AAE/B,SAAgB,YAAY,CAAC,IAAU;IACrC,OAAO,IAAA,aAAK,EAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAA;AAC1B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/date/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,qBAAqB,CAAA;AACnC,cAAc,cAAc,CAAA;AAC5B,cAAc,uBAAuB,CAAA;AACrC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/date/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB;AACvB,sDAAmC;AACnC,+CAA4B;AAC5B,wDAAqC;AACrC,iDAA8B;AAC9B,mDAAgC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseTimeSpent.d.ts","sourceRoot":"","sources":["../../src/date/parseTimeSpent.ts"],"names":[],"mappings":"AA4DA,wBAAgB,cAAc,CAC5B,WAAW,EAAE,MAAM,GAAG,IAAI,EAC1B,SAAS,EAAE,MAAM,GAAG,IAAI,EACxB,MAAM,SAAU,UAsCjB"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { fixTimezoneOffset } from "./fixTimezoneOffset";
|
|
2
|
+
// Supported locales and their translations
|
|
3
|
+
const locales = {
|
|
4
|
+
"pt-BR": {
|
|
5
|
+
year: { singular: "ano", plural: "anos" },
|
|
6
|
+
month: { singular: "mês", plural: "meses" },
|
|
7
|
+
day: { singular: "dia", plural: "dias" },
|
|
8
|
+
formatList: (parts) => {
|
|
9
|
+
if (parts.length === 0)
|
|
10
|
+
return "";
|
|
11
|
+
if (parts.length === 1)
|
|
12
|
+
return parts[0];
|
|
13
|
+
const lastPart = parts[parts.length - 1];
|
|
14
|
+
const otherParts = parts.slice(0, -1);
|
|
15
|
+
return `${otherParts.join(", ")} e ${lastPart}`;
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
"en-US": {
|
|
19
|
+
year: { singular: "year", plural: "years" },
|
|
20
|
+
month: { singular: "month", plural: "months" },
|
|
21
|
+
day: { singular: "day", plural: "days" },
|
|
22
|
+
formatList: (parts) => {
|
|
23
|
+
if (parts.length === 0)
|
|
24
|
+
return "";
|
|
25
|
+
if (parts.length === 1)
|
|
26
|
+
return parts[0];
|
|
27
|
+
if (parts.length === 2)
|
|
28
|
+
return `${parts[0]} and ${parts[1]}`;
|
|
29
|
+
return `${parts.slice(0, -1).join(", ")}, and ${parts[parts.length - 1]}`;
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
"es-ES": {
|
|
33
|
+
year: { singular: "año", plural: "años" },
|
|
34
|
+
month: { singular: "mes", plural: "meses" },
|
|
35
|
+
day: { singular: "día", plural: "días" },
|
|
36
|
+
formatList: (parts) => {
|
|
37
|
+
if (parts.length === 0)
|
|
38
|
+
return "";
|
|
39
|
+
if (parts.length === 1)
|
|
40
|
+
return parts[0];
|
|
41
|
+
const lastPart = parts[parts.length - 1];
|
|
42
|
+
const otherParts = parts.slice(0, -1);
|
|
43
|
+
return `${otherParts.join(", ")} y ${lastPart}`;
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
"fr-FR": {
|
|
47
|
+
year: { singular: "an", plural: "ans" },
|
|
48
|
+
month: { singular: "mois", plural: "mois" },
|
|
49
|
+
day: { singular: "jour", plural: "jours" },
|
|
50
|
+
formatList: (parts) => {
|
|
51
|
+
if (parts.length === 0)
|
|
52
|
+
return "";
|
|
53
|
+
if (parts.length === 1)
|
|
54
|
+
return parts[0];
|
|
55
|
+
const lastPart = parts[parts.length - 1];
|
|
56
|
+
const otherParts = parts.slice(0, -1);
|
|
57
|
+
return `${otherParts.join(", ")} et ${lastPart}`;
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
export function parseTimeSpent(initialDate, finalDate, locale = "pt-BR") {
|
|
62
|
+
const final = fixTimezoneOffset(finalDate);
|
|
63
|
+
const initial = fixTimezoneOffset(initialDate);
|
|
64
|
+
const years = final.diff(initial, "year");
|
|
65
|
+
const months = final.diff(initial.add(years, "year"), "month");
|
|
66
|
+
const days = final.diff(initial.add(years, "year").add(months, "month"), "day");
|
|
67
|
+
// Use default locale if the requested one isn't supported
|
|
68
|
+
const translations = locales[locale] || locales["pt-BR"];
|
|
69
|
+
// Format each time unit
|
|
70
|
+
const parts = [];
|
|
71
|
+
if (years > 0) {
|
|
72
|
+
const unit = years === 1 ? translations.year.singular : translations.year.plural;
|
|
73
|
+
parts.push(`${years} ${unit}`);
|
|
74
|
+
}
|
|
75
|
+
if (months > 0) {
|
|
76
|
+
const unit = months === 1 ? translations.month.singular : translations.month.plural;
|
|
77
|
+
parts.push(`${months} ${unit}`);
|
|
78
|
+
}
|
|
79
|
+
if (days > 0) {
|
|
80
|
+
const unit = days === 1 ? translations.day.singular : translations.day.plural;
|
|
81
|
+
parts.push(`${days} ${unit}`);
|
|
82
|
+
}
|
|
83
|
+
// Format according to locale's list formatting rules
|
|
84
|
+
return translations.formatList(parts);
|
|
85
|
+
}
|