Chaining Function Scripts
How is the return value handled
If the function returns a value and that value is not a string, nothing happens; the value is ignored.
However, if the return value is a string, jko
will search for a script with that name and execute it.
Let's check following examples.
Example 1: A non‑string value is returned
export default {
scripts: {
functionScript: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
const result = a + b
console.log(`${a} + ${b} = ${result}`)
return result
}
}
}
$ jko functionScript 1 2
$ 1 + 2 = 3
(Actually, the output will be longer, but it has been simplified to emphasize functionality over presentation. To get the exact same output as in the example, use the following command: jko -l=e functionScript 1 2
)
Nothing additional happens.
Example 2: A string value is returned
export default {
scripts: {
functionScript: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
const result = a + b
console.log(`${a} + ${b} = ${result}`)
return result < 0 ? 'negative' : 'positive'
},
negative: 'echo It was negative.',
positive: 'echo It was positive.'
}
}
$ jko functionScript 1 2
$ 1 + 2 = 3
$ It was positive.
$ jko functionScript -2 1
$ -2 + 1 = -1
$ It was negative.
(Actually, the output will be longer, but it has been simplified to emphasize functionality over presentation. To get the exact same output as in the example, use the following command: jko -l=e functionScript 1 2
)
negative
script is executed byjko
whenresult
is negative.positive
script is executed byjko
whenresult
is positive or 0.
Chaining Function Script Calls
Chaining function script calls occurs when a function script returns the name of another function script.
export default {
scripts: {
functionScript: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
console.log(`${a} + ${b} = ${a + b}`)
return 'show'
},
show: function() {
console.log(`at ${new Date()}`)
return 'done'
},
done: function() {
console.log('Done!')
}
}
}
- Function Script
functionScript
calls Function Scriptshow
. - Function Script
show
calls Function Scriptdone
.
This chaining can go on and on.
Chained function scripts runs faster than Chained commands.
When It Stops?
Chaining can continue indefinitely. It stops when:
- the function script does not return a value or return a non-string value, or
- the last script in the chain is a traditional script.
e.g.
export default {
scripts: {
functionScript: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
console.log(`${a} + ${b} = ${a + b}`)
return 'show'
},
show: function() {
console.log(`at ${new Date()}`)
return 'done'
},
done: function() {
console.log('Done!')
}
}
}
Since the function script done
returns no value, the chaining stops.
e.g.
export default {
scripts: {
functionScript: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
console.log(`${a} + ${b} = ${a + b}`)
return 'show'
},
show: function() {
console.log(`at ${new Date()}`)
return 'done'
},
done: 'echo Done!',
}
}
Since done
is a traditional script, the chaining stops.
If the chain includes a traditional script, that script will always be the final one.
Arguments
When chaining function scripts, the initial arguments are available to every function script in the chain.
e.g.
export default {
scripts: {
operation1: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
console.log(`${a} + ${b} = ${a + b}`)
return 'operation2'
},
operation2: function(param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
console.log(`${a} - ${b} = ${a - b}`)
return 'operation3'
},
operation3: function(param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
console.log(`${a} * ${b} = ${a * b}`)
return 'done'
},
done: 'echo Done!'
}
}
$ jko operation1 1.2 3.4
$ 1.2 + 3.4 = 4.6
$ 1.2 - 3.4 = -2.2
$ 1.2 * 3.4 = 4.08
$ Done!
(Actually, the output will be longer, but it has been simplified to emphasize functionality over presentation. To get the exact same output as in the example, use the following command: jko -l=e functionScript 1 2
)
Note that the argument-sharing mechanism for chained function scripts differs from the jko_arg_#
values provided by the environment. While environment variables are shared among all descendant processes, the initial arguments are only available within the chain of function scripts
Passing values between Scripts
Using the Environment
If you need to pass values between scripts, you can set custom environment variables. These variables will be available to the next script invoked by the function script—whether it’s a traditional script or another function script.
- In a function script, use
process.your_custom_env_var
. - In a traditional script, you can access them via
$your_custom_env_var
;
Example 1: Using Custom Environment Variables in a function script
export default {
scripts: {
functionScript: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
// Set a custom environment variable, in this case called `jko_return_0`.
process.env.jko_return_0 = `${a} + ${b} = ${a + b}`
return 'show'
},
show: function() {
// Access custom environment variable
console.log(process.env.jko_return_0)
}
}
}
$ jko functionScript 1 2
$ 1 + 2 = 3
(Actually, the output will be longer, but it has been simplified to emphasize functionality over presentation. To get the exact same output as in the example, use the following command: jko -l=e functionScript 1 2
)
Example 2: Using Custom Environment Variables in a traditional script
export default {
scripts: {
functionScript: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
// Set a custom environment variable, in this case called `jko_return_0`.
process.env.jko_return_0 = `${a} + ${b} = ${a + b}`
return 'show'
},
show: 'echo $jko_return_0', // Access custom environment variable
}
}
$ jko functionScript 1 2
$ 1 + 2 = 3
(Actually, the output will be longer, but it has been simplified to emphasize functionality over presentation. To get the exact same output as in the example, use the following command: jko -l=e functionScript 1 2
)
Every value that is passed through an environment variable is automatically converted into a string.
Using Local Variables
When chaining function scripts from the same module, you can use local variables to share information between them.
Example: Using Local Variables in a function script
let jkoReturn0
export default {
scripts: {
functionScript: function (param1, param2) {
const a = parseFloat(param1)
const b = parseFloat(param2)
// Set local variable
jkoReturn0 = `${a} + ${b} = ${a + b}`
return 'show'
},
show: function() {
// Access local variable
console.log(jkoReturn0)
}
}
}
$ jko functionScript 1 2
$ 1 + 2 = 3
(Actually, the output will be longer, but it has been simplified to emphasize functionality over presentation. To get the exact same output as in the example, use the following command: jko -l=e functionScript 1 2
)
Since scripts are preprocessed, this approach doesn't work with traditional scripts. When the script is called, it reflects only the value that was initially set.