Skip to content

Instantly share code, notes, and snippets.

@jhsuZerion
jhsuZerion / countIf.js
Created August 29, 2018 00:29
Page-level JavaScript function to return the number of occurrences in an array of values
/**
* Return the number of occurrences in a list
* @param {Array} arr list of primitive values (string, numeric, boolean)
* @param {String|Numeric} x the value to be searched for
* @return {Int} the count of occurrences
*/
function countIf(arr,x) {
var count = 0;
if(arr && Array.isArray(arr)) {
for(var i=0; i<arr.length; i++) {
@jhsuZerion
jhsuZerion / genPassword.js
Created June 13, 2018 13:59
JavaScript function to generate randomized passwords
/**
* Generates a random password for a given length and complexity
* @param {int} len number of characters in the password
* @param {boolean} upper toggle to include uppercase letters
* @param {boolean} number toggle to include numeric digits
* @param {boolean} special toggle to include special characters
* @param {boolean} space toggle to include non-breaking space
* @return {string} password including minimum 1 character of each complexity
*/
function genPassword(len,upper,number,special,space) {
/**
* Returns the number of decimals in a numeric value
* @param {string or numeric} n value can be numeric data type or string
* @return {int} number of digits after decimal, false if input is NaN
*/
function numDecimals(n) {
if(typeof n === "undefined" || isNaN(n) === true) return false;
var input_value = String(n);
if(input_value.indexOf(".") === -1) {
/**
* Convert a whole number of minutes into an hour/minute string
* @param {number} m number of minutes, will be truncated
* @return {string} string of converted time in hours/minutes
* Example: 1hr, 1hr 40 min, 2 hrs 15 min
*/
function convertMinutesToHours(m) {
// truncate number of minutes
m = Math.floor(m);
/**
* Find the minimum value of an element in a subform
* @param {array} myArray Subform
* @param {string} column Data column name to aggregate
* @return {int|boolean} The minimum value found, or false if array is undefined or data column name is not found
*/
function getMin(myArray, column) {
if (typeof myArray == "undefined" || myArray == null || Array.isArray(myArray) === false) return false;
var min_value = false;
/**
* Find the maximum value of an element in a subform
* @param {array} myArray Subform
* @param {string} column Data column name to aggregate
* @return {int|boolean} The maximum value found, or false if array is undefined or data column name is not found
*/
function getMax(myArray, column) {
if (typeof myArray == "undefined" || myArray == null || Array.isArray(myArray) === false) return false;
var max_value = false;
@jhsuZerion
jhsuZerion / simpleCodeGenerator.js
Created February 22, 2018 22:34
Function to generate a set of unique alphanumeric codes with a prefixed timestamp
/**
* Generate a set of unique alphanumeric codes with a timestamped prefix
* prefix is parsed as an octal value
* remainder of the code is in the pattern A99, first character is a letter followed by two numbers
* every three characters are separated by a hyphen
* @param {int} num_codes the number of codes to generate
* @param {int} code_length the length of codes, does not include hyphens
* @return {array} list of codes
*/
function simpleCodeGenerator(num_codes,code_length) {
/**
* Parse the data from a Location Element and return the requested value
* @param {string} l stored string value from a Location Element
* @param {string} k name of the requested value
* @return {string} parsed value from the Location Element or empty string
*/
function parseLocation(l,k) {
if(typeof k === "undefined" || k.trim().length === 0) return "";
var location = {};
function haversineDistance(latitude1, longitude1, latitude2, longitude2, unit) {
if(typeof latitude1 === "undefined" || typeof longitude1 === "undefined" || typeof latitude2 === "undefined" || typeof longitude2 === "undefined") return -1;
unit = (typeof unit === "undefined") ? "km" : unit;
// convert to Number
latitude1 = Number(latitude1);
longitude1 = Number(longitude1);
latitude2 = Number(latitude2);
longitude2 = Number(longitude2);
var R = 6371;
// Radius of the earth in km
/**
* Build full name based on three string values, will not double space
* @param {string} f first name
* @param {string} m middle name
* @param {string} l last name
* @return {string} three strings joined by space
*/
function makePersonFullName(f,m,l) {
var full_name = [];
if(typeof f === "string" && f.trim().length > 0) { full_name.push(f); }