Skip to content

Commit

Permalink
feat(manip): addDuration and subDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed May 26, 2024
1 parent 24596ba commit 0c198f5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
sourceType: 'module', // Allows for the use of imports
},
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'error', // makes code-reviews easier + code quality better by explicitly defining outputs of exported functions+classes
'@typescript-eslint/explicit-module-boundary-types': 'warn', // makes code-reviews easier + code quality better by explicitly defining outputs of exported functions+classes
'@typescript-eslint/explicit-function-return-type': 'off', // prefer '@typescript-eslint/explicit-module-boundary-types' since it only requires the check on exported functions+classes
'sort-imports': 'off',
'import/prefer-default-export': 'off', // default export = bad
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './utils/sleep';
export * from './checks/isUniDate';
export * from './checks/isUniDateTime';
export * from './casts/toMillisecondsSinceEpoch';
export * from './manipulate/addDuration';
export * from './manipulate/subDuration';
21 changes: 21 additions & 0 deletions src/manipulate/addDuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { parseISO, subMilliseconds } from 'date-fns';
import { PickOne } from 'type-fns';

import { asUniDate, isUniDate } from '../checks/isUniDate';
import { asUniDateTime } from '../checks/isUniDateTime';
import { UniDate, UniDateTime } from '../domain/UniDateTime';
import { UniDuration, toMilliseconds } from '../domain/UniDuration';

/**
* subtract a duration from a datetime
*/
export const subDuration = (
...args:
| [UniDateTime, UniDuration]
| [UniDate, PickOne<Pick<Required<UniDuration>, 'days'>>]
) =>
isUniDate(args[0])
? asUniDate(subMilliseconds(parseISO(args[0]), toMilliseconds(args[1])))
: asUniDateTime(
subMilliseconds(parseISO(args[0]), toMilliseconds(args[1])),
);
21 changes: 21 additions & 0 deletions src/manipulate/subDuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { addMilliseconds, parseISO } from 'date-fns';
import { PickOne } from 'type-fns';

import { asUniDate, isUniDate } from '../checks/isUniDate';
import { asUniDateTime } from '../checks/isUniDateTime';
import { UniDate, UniDateTime } from '../domain/UniDateTime';
import { UniDuration, toMilliseconds } from '../domain/UniDuration';

/**
* add a duration to a datetime
*/
export const addDuration = (
...args:
| [UniDateTime, UniDuration]
| [UniDate, PickOne<Pick<Required<UniDuration>, 'days'>>]
) =>
isUniDate(args[0])
? asUniDate(addMilliseconds(parseISO(args[0]), toMilliseconds(args[1])))
: asUniDateTime(
addMilliseconds(parseISO(args[0]), toMilliseconds(args[1])),
);

0 comments on commit 0c198f5

Please sign in to comment.