diff --git a/__tests__/actions/getTimeBetweenDates.spec.ts b/__tests__/actions/getTimeBetweenDates.spec.ts new file mode 100644 index 00000000..56bbc52f --- /dev/null +++ b/__tests__/actions/getTimeBetweenDates.spec.ts @@ -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); + }); +}); diff --git a/src/actions/getTimeBetweenDates.ts b/src/actions/getTimeBetweenDates.ts new file mode 100644 index 00000000..f294b03f --- /dev/null +++ b/src/actions/getTimeBetweenDates.ts @@ -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); diff --git a/src/actions/index.ts b/src/actions/index.ts index 1b9d3905..fe1dbb2e 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -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'; @@ -165,6 +166,7 @@ export { getNetworkDetails, getPhoneNumbersFromInput, getTextFromInput, + getTimeBetweenDates, getType, getURLsFromInput, getVariable, diff --git a/src/interfaces/WF/WFTimeUntilReferenceDate.ts b/src/interfaces/WF/WFTimeUntilReferenceDate.ts new file mode 100644 index 00000000..efe432ab --- /dev/null +++ b/src/interfaces/WF/WFTimeUntilReferenceDate.ts @@ -0,0 +1,6 @@ +type WFTimeUntilReferenceDate = ( + 'Right Now' + | 'Other' +); + +export default WFTimeUntilReferenceDate; diff --git a/src/interfaces/WF/WFTimeUntilUnit.ts b/src/interfaces/WF/WFTimeUntilUnit.ts new file mode 100644 index 00000000..658c9faa --- /dev/null +++ b/src/interfaces/WF/WFTimeUntilUnit.ts @@ -0,0 +1,12 @@ +type WFTimeUntilUnit = ( + 'Total Time' + | 'Seconds' + | 'Minutes' + | 'Hours' + | 'Days' + | 'Weeks' + | 'Months' + | 'Years' +); + +export default WFTimeUntilUnit; diff --git a/src/interfaces/WF/WFWorkflowActionIdentifier.ts b/src/interfaces/WF/WFWorkflowActionIdentifier.ts index c45429a9..dc608cec 100644 --- a/src/interfaces/WF/WFWorkflowActionIdentifier.ts +++ b/src/interfaces/WF/WFWorkflowActionIdentifier.ts @@ -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' diff --git a/src/interfaces/WF/WFWorkflowActionParameters.ts b/src/interfaces/WF/WFWorkflowActionParameters.ts index 77ce474c..d1f64759 100644 --- a/src/interfaces/WF/WFWorkflowActionParameters.ts +++ b/src/interfaces/WF/WFWorkflowActionParameters.ts @@ -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; @@ -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;