Skip to content

Instantly share code, notes, and snippets.

@tonykambo
tonykambo / Add numbers within an array.js
Created August 7, 2016 14:17
Ignores null values in array
var test=[50,30,null];
var sum = test.reduce(add,0);
function add(total, num) {
return total+num;
}
console.log(sum);
@tonykambo
tonykambo / Parsing Syslog message with Regex.js
Created August 7, 2016 14:15
Everything is between / / ( ) - captures the match in a variable (in this case the array \d+ - captures 1 or more digits \d{1} - matches exactly 1 digit \s{1} - matches exactly 1 space \S+ - matches a single character other than whitespace .+ - matches one or more characters (including whitespaces) \[ - escapes [ so it is treated as a literal ch…
var syslog = '<14>1 2016-08-07T03:25:39.198284+00:00 loggregator 765a363b-3467-436c-3d69-6b6789533456 [APP/0] - - 7 Aug 03:25:39 - [error] [function:faf0b791.050f48] This is an error from noderedapp';
var regSys = /<(\d+)>(\d{1})\s{1}(\S+)\s{1}(\S+)\s{1}(\S+)\s{1}(\S+)\s{1}-\s{1}-\s{1}.+-\s{1}\[(\S+)\]\s{1}\[(\S+)\]\s{1}(.+)/;
var breakdown = regSys.exec(syslog);
console.log(typeof regSys);
console.log(breakdown);
@tonykambo
tonykambo / Reduce decimal points in a float or decimal value.js
Created August 7, 2016 14:14
Redeuces to 2 decimal places. toFixed returns a string so run parseFloat again.
parseFloat(parseFloat("2039.43485").toFixed(2));