Skip to content

Commit

Permalink
fix: adding duration() to project in Stages.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Hoogendoorn committed Sep 13, 2024
1 parent 2705bb2 commit 1beaffb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/easy-mongo/src/Stages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export const stages = {
ifNotEmpty(excludes, es => ({ $project: es.reduce((a: Filter, b: Filter) => ({ ...a, ...(isString(b) ? { [b]: 0 } : b) }), {}) })),
includes: (includes: Record<string, (string | Record<string, 1>)[]>) => new IncludeBuilder(includes),
project: (project?: Filter) => ifDefined(project, $project => ({ $project })),
date: (key: string, format = '%Y-%m-%d') => ({ $toDate: `$${key}`, format }),
date: (key: string, format?: string) => ({ $toDate: `$${key}`, ...ifDefined(format, { format }) }),
duration: (from: string, to: string) => ({ $divide: [{ $subtract: [stages.project.date(from), stages.project.date(to)] }, 1000] }),
},
replaceWith: {
replaceWith: (f?: Filter): Optional<Filter> => ifDefined(f, { $replaceWith: f }),
Expand Down
10 changes: 8 additions & 2 deletions packages/easy-mongo/test/Stages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ describe('Stages', () => {
});

// Project
const { include, exclude, includes, project, date: pDate } = stages.project;
const { include, exclude, includes, project, date: pDate, duration } = stages.project;

test('projection include', () => {
expect(include()).toBeUndefined();
Expand Down Expand Up @@ -412,10 +412,16 @@ describe('Stages', () => {
});

test('date', () => {
expect(pDate('created')).toStrictEqual({ $toDate: '$created', format: '%Y-%m-%d' });
expect(pDate('created')).toStrictEqual({ $toDate: '$created' });
expect(pDate('created', '%Y-%m-%d %H:%M:%S')).toStrictEqual({ $toDate: '$created', format: '%Y-%m-%d %H:%M:%S' });
});

test('duration', () => {
expect(duration('start', 'end')).toStrictEqual({
$divide: [{ $subtract: [{ $toDate: '$start' }, { $toDate: '$end' }] }, 1000],
});
});

// ReplaceWith

const { merge, reroot, concat, rootAnd, currentAnd, replaceWith } = stages.replaceWith;
Expand Down

0 comments on commit 1beaffb

Please sign in to comment.