Skip to content

Commit

Permalink
Merge pull request #208 from StickNitro/master
Browse files Browse the repository at this point in the history
feat: add amIsBefore and amIsAfter pipes for #118
  • Loading branch information
urish authored Dec 30, 2018
2 parents 660ab31 + d6b492c commit 1630cc6
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 7 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,33 @@ Parses a string but keeps the resulting Moment object in a fixed-offset timezone

Prints `Last updated: Saturday, December 31, 2016 11:00 PM (-03:00)`

## amIsBefore and amIsAfter pipe

Check if a moment is before another moment. Supports limiting granularity to a unit other than milliseconds, pass the units as second parameter

```typescript
@Component({
selector: 'app',
template: `
Today is before tomorrow: {{ today | amIsBefore:tomorrow:'day' }}
`
})
```

Prints `Today is before tomorrow: true`

```typescript
@Component({
selector: 'app',
template: `
Tomorrow is after today: {{ tomorrow | amIsAfter:today:'day' }}
`
})
```

Prints `Tomorrow is after today: true`


Complete Example
----------------

Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export { FromUtcPipe } from './from-utc.pipe';
export { LocalTimePipe } from './local.pipe';
export { LocalePipe } from './locale.pipe';
export { ParseZonePipe } from './parse-zone.pipe';
export { IsBeforePipe } from './is-before.pipe';
export { IsAfterPipe } from './is-after.pipe';
33 changes: 33 additions & 0 deletions src/is-after.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* tslint:disable:no-unused-variable */
import { IsAfterPipe } from './is-after.pipe';

describe('IsAfterPipe', () => {
let pipe: IsAfterPipe;

beforeEach(() => pipe = new IsAfterPipe());

describe('#transform', () => {

it('should return true if value is after otherValue', () => {
const test = new Date(2018, 11, 15, 0, 0, 0);
const testDate1 = new Date(2018, 11, 13, 0, 0, 0);
expect(pipe.transform(test, testDate1)).toBeTruthy();
});

it('should support passing "year", "month", "week", "day", etc as a unit parameter', () => {
const test = new Date(2019, 11, 13, 12, 45, 45);
const testDate1 = new Date(2018, 0, 13, 12, 45, 45);
expect(pipe.transform(test, testDate1, 'year')).toBe(true);
const testDate2 = new Date(2018, 1, 13, 12, 45, 45);
expect(pipe.transform(test, testDate2, 'month')).toBe(true);
const testDate3 = new Date(2018, 1, 10, 12, 45, 45);
expect(pipe.transform(test, testDate3, 'day')).toBe(true);
});

it('should return false if value is before otherValue', () => {
const test = new Date(2018, 11, 13, 0, 0, 0);
const testDate1 = new Date(2018, 11, 15, 0, 0, 0);
expect(pipe.transform(test, testDate1)).toBeFalsy();
});
});
});
18 changes: 18 additions & 0 deletions src/is-after.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as moment from 'moment';

import { Pipe, PipeTransform } from '@angular/core';

const momentConstructor = moment;

@Pipe({
name: 'amIsAfter'
})
export class IsAfterPipe implements PipeTransform {

transform(value: Date | moment.Moment,
otherValue: Date | moment.Moment,
unit?: moment.unitOfTime.StartOf): boolean {
return momentConstructor(value).isAfter(momentConstructor(otherValue), unit);
}

}
32 changes: 32 additions & 0 deletions src/is-before.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IsBeforePipe } from './is-before.pipe';

describe('IsBeforePipe', () => {
let pipe: IsBeforePipe;

beforeEach(() => pipe = new IsBeforePipe());

describe('#transform', () => {

it('should return true if value is before otherValue', () => {
const test = new Date(2018, 11, 13, 0, 0, 0);
const testDate1 = new Date(2018, 11, 15, 0, 0, 0);
expect(pipe.transform(test, testDate1)).toBeTruthy();
});

it('should support passing "year", "month", "week", "day", etc as a unit parameter', () => {
const test = new Date(2018, 0, 13, 12, 45, 45);
const testDate1 = new Date(2019, 0, 13, 12, 45, 45);
expect(pipe.transform(test, testDate1, 'year')).toBe(true);
const testDate2 = new Date(2018, 1, 13, 12, 45, 45);
expect(pipe.transform(test, testDate2, 'month')).toBe(true);
const testDate3 = new Date(2018, 1, 13, 12, 45, 45);
expect(pipe.transform(test, testDate3, 'day')).toBe(true);
});

it('should return false if value is after otherValue', () => {
const test = new Date(2018, 11, 15, 0, 0, 0);
const testDate1 = new Date(2018, 11, 13, 0, 0, 0);
expect(pipe.transform(test, testDate1)).toBeFalsy();
});
});
});
18 changes: 18 additions & 0 deletions src/is-before.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as moment from 'moment';

import { Pipe, PipeTransform } from '@angular/core';

const momentConstructor = moment;

@Pipe({
name: 'amIsBefore'
})
export class IsBeforePipe implements PipeTransform {

transform(value: Date | moment.Moment,
otherValue: Date | moment.Moment,
unit?: moment.unitOfTime.StartOf): boolean {
return momentConstructor(value).isBefore(momentConstructor(otherValue), unit);
}

}
17 changes: 10 additions & 7 deletions src/moment.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { NgModule } from '@angular/core';

import { AddPipe } from './add.pipe';
import { CalendarPipe } from './calendar.pipe';
import { DateFormatPipe } from './date-format.pipe';
import { DifferencePipe } from './difference.pipe';
import { DurationPipe } from './duration.pipe';
import { FromUnixPipe } from './from-unix.pipe';
import { ParsePipe } from './parse.pipe';
import { SubtractPipe } from './subtract.pipe';
import { TimeAgoPipe } from './time-ago.pipe';
import { UtcPipe } from './utc.pipe';
import { FromUtcPipe } from './from-utc.pipe';
import { IsAfterPipe } from './is-after.pipe';
import { IsBeforePipe } from './is-before.pipe';
import { LocalTimePipe } from './local.pipe';
import { LocalePipe } from './locale.pipe';
import { NgModule } from '@angular/core';
import { ParsePipe } from './parse.pipe';
import { ParseZonePipe } from './parse-zone.pipe';
import { SubtractPipe } from './subtract.pipe';
import { TimeAgoPipe } from './time-ago.pipe';
import { UtcPipe } from './utc.pipe';

const ANGULAR_MOMENT_PIPES = [
AddPipe,
Expand All @@ -29,7 +30,9 @@ const ANGULAR_MOMENT_PIPES = [
FromUtcPipe,
LocalTimePipe,
LocalePipe,
ParseZonePipe
ParseZonePipe,
IsBeforePipe,
IsAfterPipe
];

@NgModule({
Expand Down

0 comments on commit 1630cc6

Please sign in to comment.