/
at.js
67 lines (43 loc) · 1.49 KB
/
at.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module.exports = {
friendlyName: 'Get character in string',
description: 'Get a character from a string at a particular position.',
sync: true,
sideEffects: 'cacheable',
inputs: {
string: {
example: 'villeriño',
description: 'The source string.',
required: true
},
at: {
friendlyName: 'Character position',
description: 'The index to look up within the string.',
extendedDescription: 'Strings are indexed starting from the left at 0. This value must be a non-negative integer.',
example: 7,
required: true
}
},
exits: {
success: {
outputFriendlyName: 'Character from string',
outputExample: 'ñ',
outputDescription: 'The character found at the specified index of the input string.'
},
notFound: {
friendlyName: 'Out of range',
description: 'The specified string didn\'t have a character at the specified index (i.e. it was too short).'
}
},
fn: function (inputs, exits) {
// If the index is not a non-negative integer, trigger `error`.
if (inputs.at !== Math.floor(inputs.at) || inputs.at < 0) {
return exits.error(new Error('The configured value for `at` must be a non-negative integer.'));
}
// If the index is >= than the string length, trigger `notFound`.
if (inputs.at >= inputs.string.length) {
return exits.notFound();
}
// Return the character at the specified index.
return exits.success(inputs.string[inputs.at]);
}
};