Skip to content

Commit

Permalink
Merge 51b3042 into 12bdfa4
Browse files Browse the repository at this point in the history
  • Loading branch information
regaw-leinad committed Dec 18, 2018
2 parents 12bdfa4 + 51b3042 commit ace6d91
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 0 deletions.
182 changes: 182 additions & 0 deletions __tests__/actions/getTimeBetweenDates.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import { getTimeBetweenDates } from '../../src/actions';
import { askWhenRun } from '../../src/variables';

import WFSerialization from '../../src/interfaces/WF/WFSerialization';

describe('getTimeBetweenDates function', () => {

it('is a function', () => {
expect(typeof getTimeBetweenDates).toBe('function');
});

it('builds a getTimeBetweenDates action', () => {
const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {},
};
const actual = getTimeBetweenDates({});

expect(actual).toEqual(expected);
});

it('builds a getTimeBetweenDates action with a specified time unit', () => {
const unit = 'Hours';

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
WFTimeUntilUnit: unit,
},
};

const actual = getTimeBetweenDates({ unit });

expect(actual).toEqual(expected);
});

it('builds a getTimeBetweenDates action that asks for time unit when run', () => {
const unit = askWhenRun;

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
WFTimeUntilUnit: unit,
},
};

const actual = getTimeBetweenDates({ unit });

expect(actual).toEqual(expected);
});

it('builds a getTimeBetweenDates action that has a custom start time', () => {
const date = '3/5/2011 5:42PM';

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
WFTimeUntilReferenceDate: 'Other',
WFTimeUntilCustomDate: date,
},
};

const actual = getTimeBetweenDates({ date });

expect(actual).toEqual(expected);
});

it('builds a getTimeBetweenDates action that has a garbage custom start time', () => {
const date = 'thisIsGarbageButItIsStillValid';

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
WFTimeUntilReferenceDate: 'Other',
WFTimeUntilCustomDate: date,
},
};

const actual = getTimeBetweenDates({ date });

expect(actual).toEqual(expected);
});

it('builds a getTimeBetweenDates action that has a custom start time and unit', () => {
const unit = 'Hours';
const date = '3/5/2011 5:42PM';

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
WFTimeUntilUnit: unit,
WFTimeUntilReferenceDate: 'Other',
WFTimeUntilCustomDate: date,
},
};

const actual = getTimeBetweenDates({ unit, date });

expect(actual).toEqual(expected);
});

it('builds a getTimeBetweenDates action that asks for a custom time when run', () => {
const customDateAsk: WFSerialization = {
Value: {
string: '', // Object replacement character
attachmentsByRange: {
'{0, 1}': {
Type: 'Ask',
},
},
},
WFSerializationType: 'WFTextTokenString',
};

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
WFTimeUntilReferenceDate: 'Other',
WFTimeUntilCustomDate: customDateAsk,
},
};

const actual = getTimeBetweenDates({ date: customDateAsk });

expect(actual).toEqual(expected);
});

it('builds a getTimeBetweenDates action that uses a variable for a custom time', () => {
const variableObject: WFSerialization = {
Value: {
string: '', // Object replacement character
attachmentsByRange: {
'{0, 1}': {
OutputUUID: 'b74c81a8-192a-463f-a0a6-2d327963714f',
Type: 'ActionOutput',
},
},
},
WFSerializationType: 'WFTextTokenString',
};

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
WFTimeUntilReferenceDate: 'Other',
WFTimeUntilCustomDate: variableObject,
},
};

const actual = getTimeBetweenDates({ date: variableObject });

expect(actual).toEqual(expected);
});

it('builds a getTimeBetweenDates action that asks for a unit and custom time when run', () => {
const unit = askWhenRun;
const customDateAsk: WFSerialization = {
Value: {
string: '', // Object replacement character
attachmentsByRange: {
'{0, 1}': {
Type: 'Ask',
},
},
},
WFSerializationType: 'WFTextTokenString',
};

const expected = {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
WFTimeUntilUnit: unit,
WFTimeUntilReferenceDate: 'Other',
WFTimeUntilCustomDate: customDateAsk,
},
};

const actual = getTimeBetweenDates({ unit, date: customDateAsk });

expect(actual).toEqual(expected);
});
});
41 changes: 41 additions & 0 deletions src/actions/getTimeBetweenDates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { withActionOutput } from '../utils';

import WFSerialization from '../interfaces/WF/WFSerialization';
import WFTimeUntilUnit from '../interfaces/WF/WFTimeUntilUnit';
import WFWorkflowAction from '../interfaces/WF/WFWorkflowAction';

/**
* Get Time Between Dates Action. Subtracts the specified date from the date passed
* into the action. For example, this action could get the number of minutes from
* now until a calendar event passed in as input.
*
* ```js
* getTimeBetweenDates({
* unit: 'Days',
* date: '3/5/2011 5:45pm',
* });
* ```
*/
const getTimeBetweenDates = (
options: {
/** The unit of time for the result. Defaults to 'Minutes' */
unit?: WFSerialization | WFTimeUntilUnit,
/** The date/time to calculate the difference from. Defaults to empty string */
date?: WFSerialization | string,
},
): WFWorkflowAction => {
const {
unit = 'Minutes',
date = '',
} = options;

return {
WFWorkflowActionIdentifier: 'is.workflow.actions.gettimebetweendates',
WFWorkflowActionParameters: {
...(unit !== 'Minutes' && { WFTimeUntilUnit: unit }),
...(date !== '' && { WFTimeUntilReferenceDate: 'Other', WFTimeUntilCustomDate: date }),
},
};
};

export default withActionOutput(getTimeBetweenDates);
2 changes: 2 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import getNameOfEmoji from './getNameOfEmoji';
import getNetworkDetails from './getNetworkDetails';
import getPhoneNumbersFromInput from './getPhoneNumbersFromInput';
import getTextFromInput from './getTextFromInput';
import getTimeBetweenDates from './getTimeBetweenDates';
import getType from './getType';
import getURLsFromInput from './getURLsFromInput';
import getVariable from './getVariable';
Expand Down Expand Up @@ -165,6 +166,7 @@ export {
getNetworkDetails,
getPhoneNumbersFromInput,
getTextFromInput,
getTimeBetweenDates,
getType,
getURLsFromInput,
getVariable,
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/WF/WFTimeUntilReferenceDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type WFTimeUntilReferenceDate = (
'Right Now'
| 'Other'
);

export default WFTimeUntilReferenceDate;
12 changes: 12 additions & 0 deletions src/interfaces/WF/WFTimeUntilUnit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type WFTimeUntilUnit = (
'Total Time'
| 'Seconds'
| 'Minutes'
| 'Hours'
| 'Days'
| 'Weeks'
| 'Months'
| 'Years'
);

export default WFTimeUntilUnit;
1 change: 1 addition & 0 deletions src/interfaces/WF/WFWorkflowActionIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type WFWorkflowActionIdentifier = (
| 'is.workflow.actions.getrichtextfromhtml'
| 'is.workflow.actions.getrichtextfrommarkdown'
| 'is.workflow.actions.gettext'
| 'is.workflow.actions.gettimebetweendates'
| 'is.workflow.actions.getvalueforkey'
| 'is.workflow.actions.getvariable'
| 'is.workflow.actions.getwebpagecontents'
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/WF/WFWorkflowActionParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import WFSerialization from './WFSerialization';
import WFSkipBackBehavior from './WFSkipBackBehavior';
import WFStatisticsOperation from './WFStatisticsOperation';
import WFTimeFormatStyle from './WFTimeFormatStyle';
import WFTimeUntilReferenceDate from './WFTimeUntilReferenceDate';
import WFTimeUntilUnit from './WFTimeUntilUnit';

interface WFWorkflowActionParameters {
Advanced?: boolean;
Expand Down Expand Up @@ -105,6 +107,9 @@ interface WFWorkflowActionParameters {
WFStatisticsOperation?: WFSerialization | WFStatisticsOperation;
WFTextActionText?: WFSerialization | string;
WFTimeFormatStyle?: WFSerialization | WFTimeFormatStyle;
WFTimeUntilCustomDate?: WFSerialization | string;
WFTimeUntilReferenceDate?: WFTimeUntilReferenceDate;
WFTimeUntilUnit?: WFSerialization | WFTimeUntilUnit;
WFURLActionURL?: string;
WFVariable?: WFSerialization | string;
WFVariableName?: string;
Expand Down

0 comments on commit ace6d91

Please sign in to comment.