Skip to content

Commit

Permalink
feat(date): adding maching before a specific date and time operator (…
Browse files Browse the repository at this point in the history
…dateLessThan)
  • Loading branch information
roggervalf committed May 29, 2021
1 parent 94321ad commit 3c6a7fe
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/conditionOperators/date/dateLessThan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { dateLessThan } from './dateLessThan';

describe('dateLessThan', () => {
it('returns true', () => {
expect(dateLessThan(new Date('December 17, 1995 03:24:00'), 'December 18, 2000 03:24:00')).toBeTruthy;
expect(dateLessThan('December 15, 1994 03:24:00', new Date('December 15, 1994 03:25:00'))).toBeTruthy;
});

it('returns false', () => {
expect(dateLessThan(new Date('December 17, 1995 03:24:00'), 'December 17, 1995 03:24:00')).toBeFalsy;
expect(dateLessThan(new Date('December 17, 1995 03:24:00'), new Date('December 16, 1995 03:24:00'))).toBeFalsy;
});
});
26 changes: 26 additions & 0 deletions src/conditionOperators/date/dateLessThan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {DateType, convertDate} from './convertDate';

/**
* Matching before a specific date and time
*
* @since 4.9.0
* @category Date
* @param {Date} data The value to be compared.
* @param {Date} expected The expected value.
* @returns {boolean} Returns `true` if `value` is before than `expected value`.
* @example
* ```javascript
* dateLessThan('December 15, 1994 03:24:00', 'December 16, 1994 03:24:00')
* // => true
*
* dateLessThan('December 15, 2000 03:24:00', 'December 15, 2000 03:24:00')
* // => false
* ```
*/
export function dateLessThan(data: DateType, expected: DateType): boolean {
const convertedData = convertDate(data);
const convertedExpectation = convertDate(expected);
return (
convertedData < convertedExpectation
);
}
14 changes: 14 additions & 0 deletions src/conditionOperators/date/dateLessThanEquals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { dateLessThanEquals } from './dateLessThanEquals';

describe('dateLessThanEquals', () => {
it('returns true', () => {
expect(dateLessThanEquals(new Date('December 17, 1995 03:24:00'), 'December 18, 2000 03:24:00')).toBeTruthy;
expect(dateLessThanEquals('December 15, 1994 03:24:00', new Date('December 15, 1994 03:24:00'))).toBeTruthy;
});

it('returns false', () => {
expect(dateLessThanEquals(new Date('December 17, 1995 03:24:00'), 'December 17, 1995 03:23:00')).toBeFalsy;
expect(dateLessThanEquals(new Date('December 17, 1995 03:24:00'), new Date('December 16, 1995 03:24:00')))
.toBeFalsy;
});
});
26 changes: 26 additions & 0 deletions src/conditionOperators/date/dateLessThanEquals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {DateType, convertDate} from './convertDate';

/**
* Matching at or before a specific date and time
*
* @since 4.9.0
* @category Date
* @param {Date} data The value to be compared.
* @param {Date} expected The expected value.
* @returns {boolean} Returns `true` if `value` is equal or before than `expected value`.
* @example
* ```javascript
* dateLessThanEquals('December 15, 1994 03:24:00', 'December 15, 1994 03:24:00')
* // => true
*
* dateLessThanEquals('December 15, 2000 03:24:00', 'December 15, 2000 03:24:00')
* // => false
* ```
*/
export function dateLessThanEquals(data: DateType, expected: DateType): boolean {
const convertedData = convertDate(data);
const convertedExpectation = convertDate(expected);
return (
convertedData <= convertedExpectation
);
}
2 changes: 2 additions & 0 deletions src/conditionOperators/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {bool} from './boolean/bool';
import {dateEquals} from './date/dateEquals';
import {dateLessThan} from './date/dateLessThan';
import {dateNotEquals} from './date/dateNotEquals';
import {numericEquals} from './numeric/numericEquals';
import {numericGreaterThan} from './numeric/numericGreaterThan';
Expand All @@ -16,6 +17,7 @@ import {stringNotEqualsIgnoreCase} from './string/stringNotEqualsIgnoreCase';
export const operators: Record<string, unknown>={
bool,
dateEquals,
dateLessThan,
dateNotEquals,
numericEquals,
numericGreaterThan,
Expand Down
2 changes: 1 addition & 1 deletion www/src/docs/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,4 @@ const embeddedStr = applyContext(str, context)

## License

## MIT © [roggervalf](https://github.com/roggervalf)
# MIT © [roggervalf](https://github.com/roggervalf)

0 comments on commit 3c6a7fe

Please sign in to comment.