@joshfarrant/shortcuts-js
Version:
An iOS 12 Shortcuts creator
96 lines • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const uuidv4 = require("uuid/v4");
const utils_1 = require("../utils");
/** @ignore */
const conditionMap = new Map([
['=', 'Equals'],
['<', 'Is Less Than'],
['>', 'Is Greater Than'],
]);
/**
* @action If
* @section Actions > Scripting > Control Flow
* @icon Scripting
*
* Tests if any item passed as input matches the specified condition, and if so, runs the actions inside. Otherwise, the actions under “Otherwise” are run.
*
* ```js
* conditional({
* ifTrue: [
* comment({
* text: 'Do something when true',
* }),
* ],
* ifFalse: [
* comment({
* text: 'Do something different when false',
* }),
* ],
* input: '<',
* value: 27,
* });
* ```
*/
const conditional = ({ ifTrue = [], ifFalse = [], input = 'Contains', value = 'example', }) => {
const groupingIdentifier = uuidv4();
const ifAction = {
WFWorkflowActionIdentifier: 'is.workflow.actions.conditional',
WFWorkflowActionParameters: {
GroupingIdentifier: groupingIdentifier,
WFControlFlowMode: 0,
},
};
if (input && input !== 'Contains') {
const condition = (conditionMap.get(input) || input);
ifAction.WFWorkflowActionParameters.WFCondition = condition;
}
// Add correct property for string or number value
if (value) {
if (typeof value === typeof 0) {
ifAction.WFWorkflowActionParameters.WFNumberValue = value;
}
else {
ifAction.WFWorkflowActionParameters.WFConditionalActionString = value;
}
}
// Open the 'if' block
let actionArr = [
ifAction,
];
// If we've got some ifTrue actions, append them
if (ifTrue.length > 0) {
actionArr = [
...actionArr,
...ifTrue,
];
}
// If we've got some ifFalse actions, add an 'else' and append the ifFalse actions
if (ifFalse.length > 0) {
actionArr = [
...actionArr,
{
WFWorkflowActionIdentifier: 'is.workflow.actions.conditional',
WFWorkflowActionParameters: {
GroupingIdentifier: groupingIdentifier,
WFControlFlowMode: 1,
},
},
...ifFalse,
];
}
// Add the final action to close the 'if' block
actionArr = [
...actionArr,
{
WFWorkflowActionIdentifier: 'is.workflow.actions.conditional',
WFWorkflowActionParameters: {
GroupingIdentifier: groupingIdentifier,
WFControlFlowMode: 2,
},
},
];
return actionArr;
};
exports.default = utils_1.withActionOutput(conditional);
//# sourceMappingURL=conditional.js.map