Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active November 26, 2019 14:47
Squirrel array reduce() example
local sourceArray = [10, 21, 30, 43];
// Combine all the values in the array
local reduction = sourceArray.reduce(function(previousValue, currentValue){
return (previousValue + currentValue);
});
server.log(reduction);
// Displays "104"
local sourceArray = [10, 21, 30, 43];
// Combine all the values in the array
local reduction = sourceArray.reduce(@(p, c) p + c);
server.log(reduction);
// Displays "104"
// Decode an X.509 certificate .pem file
// Remove the armor, concatenate the lines and base64 decode the text
function decodePem(pemString) {
local lines = split(pemString, "\n");
// We really ought to iterate over the array until we find a starting line,
// and then look for the matching ending line, rather than assuming its on line 0
if ((lines[0] == "-----BEGIN PRIVATE KEY-----" &&
lines[lines.len() - 1] == "-----END PRIVATE KEY-----") ||
(lines[0] == "-----BEGIN RSA PRIVATE KEY-----" &&
lines[lines.len() - 1] == "-----END RSA PRIVATE KEY-----") ||
(lines[0] == "-----BEGIN PUBLIC KEY-----" &&
lines[lines.len() - 1] == "-----END PUBLIC KEY-----")) {
local all = lines.slice(1, lines.len() - 1).reduce(@(a, b) a + b);
return http.base64decode(all);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment