Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Support Named Variables. Add appendVariable, getVariable, runSshScript, vibrate, waitToReturn actions. #8

Merged
merged 7 commits into from
Nov 21, 2018
12 changes: 12 additions & 0 deletions __tests__/utils/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
buildShortcut,
buildShortcutTemplate,
encodeShortcut,
testUUID,
withUUID,
withVariables,
} from '../../src/utils';
Expand All @@ -15,6 +16,9 @@ import {
import {
encodeShortcut as encodeShortcutDirect,
} from '../../src/utils/encodeShortcut';
import {
testUUID as testUUIDDirect,
} from '../../src/utils/testUUID';
import {
withUUID as withUUIDDirect,
} from '../../src/utils/withUUID';
Expand Down Expand Up @@ -48,6 +52,14 @@ describe('util index.ts file', () => {
expect(actual).toEqual(expected);
});

it('exports testUUID as a named function', () => {

const actual = testUUID.toString();
const expected = testUUIDDirect.toString();

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

it('exports withUUID as a named function', () => {

const actual = withUUID.toString();
Expand Down
18 changes: 18 additions & 0 deletions __tests__/utils/testUUID.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { testUUID } from '../../src/utils';

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

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

it('returns true for a valid UUID string', () => {
const actual = testUUID('b74c81a8-192a-463f-a0a6-2d327963714f');
expect(actual).toBe(true);
});

it('returns true for a invalid UUID string', () => {
const actual = testUUID('');
expect(actual).toBe(false);
});
});
106 changes: 103 additions & 3 deletions __tests__/utils/withVariables.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('withVariables function', () => {
expect(actual).toEqual(expected);
});

it('returns an attachment object when passed a single variable', () => {
it('returns an attachment object when passed a single magic variable', () => {
const uuid = 'b74c81a8-192a-463f-a0a6-2d327963714f';

const actual = withVariables`${uuid}`;
Expand All @@ -41,7 +41,7 @@ describe('withVariables function', () => {
expect(actual).toEqual(expected);
});

it('returns an attachment object when passed a variable in the middle of a string', () => {
it('returns an attachment object when passed a magic variable in the middle of a string', () => {
const uuid = 'b74c81a8-192a-463f-a0a6-2d327963714f';

const actual = withVariables`Hello, ${uuid} world!`;
Expand All @@ -61,7 +61,47 @@ describe('withVariables function', () => {
expect(actual).toEqual(expected);
});

it('returns an attachment object when passed multiple variables in a string', () => {
it('returns an attachment object when passed a single named variable', () => {
const name = 'Test Variable';

const actual = withVariables`${name}`;
const expected: WFSerialization = {
WFSerializationType: 'WFTextTokenString',
Value: {
string: '', // Object replacement character
attachmentsByRange: {
'{0, 1}': {
Type: 'Variable',
VariableName: 'Test Variable',
},
},
},
};

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

it('returns an attachment object when passed a named variable in the middle of a string', () => {
const name = 'Test Variable';

const actual = withVariables`Hello, ${name} world!`;
const expected: WFSerialization = {
WFSerializationType: 'WFTextTokenString',
Value: {
string: 'Hello,  world!', // Contains object replacement character
attachmentsByRange: {
'{7, 1}': {
Type: 'Variable',
VariableName: 'Test Variable',
},
},
},
};

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

it('returns an attachment object when passed multiple magic variables in a string', () => {
const uuid1 = 'b74c81a8-192a-463f-a0a6-2d327963714f';
const uuid2 = '4e8b6858-cf85-4ffe-9019-efb421248510';
const uuid3 = 'af9c2e97-8af2-4029-8664-18fb158dbd16';
Expand Down Expand Up @@ -96,4 +136,64 @@ describe('withVariables function', () => {
expect(actual).toEqual(expected);
});

it('returns an attachment object when passed multiple named variables in a string', () => {
const name1 = 'Test Variable1';
const name2 = 'Test Variable2';
const name3 = 'Test Variable3';
const name4 = 'Test Variable4';

const actual = withVariables`${name1} Going ${name2}${name3} to the ${name4} blacksmith`;
const expected: WFSerialization = {
WFSerializationType: 'WFTextTokenString',
Value: {
string: ' Going  to the  blacksmith', // Contains object replacement character
attachmentsByRange: {
'{0, 1}': {
Type: 'Variable',
VariableName: 'Test Variable1',
},
'{8, 1}': {
Type: 'Variable',
VariableName: 'Test Variable2',
},
'{9, 1}': {
Type: 'Variable',
VariableName: 'Test Variable3',
},
'{18, 1}': {
Type: 'Variable',
VariableName: 'Test Variable4',
},
},
},
};

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

it('returns an attachment object when passed a named variable and a magic variable in a string', () => {
const uuid = 'b74c81a8-192a-463f-a0a6-2d327963714f';
const name = 'Test Variable';

const actual = withVariables`${uuid} Going ${name} to the blacksmith`;
const expected: WFSerialization = {
WFSerializationType: 'WFTextTokenString',
Value: {
string: ' Going  to the blacksmith', // Contains object replacement character
attachmentsByRange: {
'{0, 1}': {
OutputUUID: 'b74c81a8-192a-463f-a0a6-2d327963714f',
Type: 'ActionOutput',
},
'{8, 1}': {
Type: 'Variable',
VariableName: 'Test Variable',
},
},
},
};

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

});
8 changes: 5 additions & 3 deletions src/interfaces/WF/Attachment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import AttachmentType from './AttachmentType';
import Type from './Type';

interface Attachment {
OutputUUID: string;
Type: AttachmentType;
OutputName?: string;
OutputUUID?: string;
Type: Type;
VariableName?: string;
}

export default Attachment;
5 changes: 0 additions & 5 deletions src/interfaces/WF/AttachmentType.ts

This file was deleted.

7 changes: 6 additions & 1 deletion src/interfaces/WF/Type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
type Type = (
'Ask'
'ActionOutput'
| 'Ask'
| 'Clipboard'
| 'CurrentDate'
| 'ExtensionInput'
| 'Variable'
);

export default Type;
7 changes: 5 additions & 2 deletions src/interfaces/WF/WFSerialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import WFDictionaryFieldValueItem from './WFDictionaryFieldValueItem';
import WFSerializationType from './WFSerializationType';

interface SerializationValue {
Type?: Type;
string?: string;
attachmentsByRange?: {
[key: string]: Attachment;
};
OutputUUID?: string;
OutputName?: string;
string?: string;
Type?: Type;
Value?: WFSerialization | boolean;
VariableName?: string;
WFDictionaryFieldValueItems?: WFDictionaryFieldValueItem[];
}

Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/WF/WFWorkflowType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type WFWorkflowType = (
'NCWidget'
'ActionExtension'
| 'NCWidget'
| 'WatchKit'
);

Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { buildShortcutTemplate } from './buildShortcutTemplate';
import { encodeShortcut } from './encodeShortcut';
import { flatten } from './flatten';
import { getItemType } from './getItemType';
import { testUUID } from './testUUID';
import { withUUID } from './withUUID';
import { withVariables } from './withVariables';

Expand All @@ -14,6 +15,7 @@ export {
encodeShortcut,
flatten,
getItemType,
testUUID,
withUUID,
withVariables,
};
8 changes: 8 additions & 0 deletions src/utils/testUUID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

/**
* Tests wether the passed string is a UUID
Archez marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} uuid
*/
export const testUUID = (uuid: string): boolean => {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(uuid);
};
16 changes: 12 additions & 4 deletions src/utils/withVariables.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { testUUID } from '.';
import WFSerialization from '../interfaces/WF/WFSerialization';

export const withVariables = (
Expand All @@ -22,10 +23,17 @@ export const withVariables = (

return {
...a,
[`{${lengthSoFar}, 1}`]: {
OutputUUID: vars[i],
Type: 'ActionOutput',
},
[`{${lengthSoFar}, 1}`]: (
testUUID(vars[i]) ? {
// Magic Variable
OutputUUID: vars[i],
Type: 'ActionOutput',
} : {
// Named Variable
VariableName: vars[i],
Type: 'Variable'
}
),
};
},
{},
Expand Down