Skip to content

Instantly share code, notes, and snippets.

@jhsuZerion
jhsuZerion / getSubformLength.js
Last active January 10, 2018 11:42
iFormBuilder Page-Level JavaScript Function
/**
* Returns the length of a Subform or -1 if no length can be found
* @param {array} s Subform element
* @return {int} length of the Subform, or -1
*/
function getSubformLength(s) {
if(typeof s === "undefined" || s == null || Array.isArray(s) === false) {
return -1;
} else {
return s.length;
@jhsuZerion
jhsuZerion / keepKeys.js
Created December 28, 2017 19:15
Function for creating an object based on a set of whitelisted keys
/**
* search through an object and return only whitelisted keys
* @param {object} r the original record
* @param {string} keep_list comma-separated list of keys to keep
* @return {object} object with only found whitelisted keys
*/
function keepKeys(r,keep_list) {
var obj = {};
for(var key in r) {
if(keep_list.indexOf(key) >= 0) {
/**
* Function to format a date object. Used to format Date, Time, Date/Time elements
* @param {object} d Date, Time, Date/Time element
* @param {string} s Format string
* @return {string} Formatted Date object
*/
function formatDate(d,s) {
if(typeof s === "undefined" || s.trim() === "") { return d.toISOString(); }
var full_months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
@jhsuZerion
jhsuZerion / getAge.js
Created October 31, 2017 15:36
Calculate age based on date element. To use this function, paste the below code in the Page-Level JavaScript section of your parent form. In the dynamic value of a Text, Number, or Read-Only element, type `getAge(dcn)` where dcn is the date element's data column name.
function getAge(d) {
var today = new Date();
var age = today.getFullYear() - d.getFullYear();
var m = today.getMonth() - d.getMonth();
if (m < 0 || (m === 0 && today.getDate() < d.getDate())) {
age--;
}
return age;
}
@jhsuZerion
jhsuZerion / jsonV10.js
Created August 26, 2017 15:17
Use this function when importing old records in JSON v9 to upscale the records to JSON v10.
/**
* upscales an iFormBuilder/Data Collector JSON v9.0 record to v10.0
* @param {object} r a valid iFormBuilder/Data Collector record
* @return {object} record with modified metadata to match JSON v10.0
*/
function jsonV10(r) {
function convert_dates(rec) {
var date_keys = ["created_date", "modified_date", "server_modified_date"];
for (var i = 0; i < date_keys.length; i++) {
@jhsuZerion
jhsuZerion / subform_to_multi_array.js
Created August 25, 2017 17:57
Convert a subform to a multi-dimensional array specified by the second parameter
/**
* function returns subform as 2D array
* @param {object} r valid JSON object
* @param {int} n integer greater than 1
* @return {object} 2D array where number of objects per row is equal to n
*/
function subform_to_multi_array(r,n) {
if(typeof n !== 'number' || n !== Math.floor(n) || n <= 1 || n > r.length) return r;
var cols = [];
var count = 0;
@jhsuZerion
jhsuZerion / split_location.js
Created August 21, 2017 13:00
Function to split the individual values from a Location Element. To use the function, pass the record as well as the data column name of the Location Element.
function split_location(r,dcn) {
if(!r.hasOwnProperty(dcn)) return r;
var l = r[dcn];
l = l.replace(/(\r\n|\n|\r)/gm,"");
var location_arr = l.split(",");
for(var i=0; i<location_arr.length; i++) {
var kvp = location_arr[i].split(":");
var key = kvp[0];
var value = (key === "Time") ? value = kvp[1] + ":" + kvp[2] + ":" + kvp[3] : kvp[1];
@jhsuZerion
jhsuZerion / remove_properties.js
Last active August 21, 2017 17:19
Function to remove properties supplied as an array of strings. Use this function to remove unwanted properties from an object. By default the function is case-sensitive and recursive. To modify either of these behaviors pass the value false for each.
/**
* Search through an object and delete any properties supplied in the list.
* Is case-sensitive and recursive by default. Both can be toggled by passing false.
* @param {Object} r original JS object
* @param {Array} properties list of keys/properties to remove
* @param {Boolean} [case_sensitive=true] set to false for case-insensitive
* @param {Boolean} [recursive=true] set to true for recursive
* @return {Object} modified JS object
*/
function remove_properties(r,properties,case_sensitive,recursive) {
@jhsuZerion
jhsuZerion / full_flatten.js
Created August 21, 2017 11:57
Dataflow Transform Code to fully flatten a record. Call the function in the Transform Return by typing full_flatten(record)
function full_flatten(data) {
// Edit these two variables to change the character(s) before and after the index value
var before_index = "_";
var after_index = "_";
// Do not edit below here
var result = {};
function recurse(cur, prop) {
if (Object(cur) !== cur) {
@jhsuZerion
jhsuZerion / remove_subforms.js
Created August 21, 2017 11:56
Function to remove any subform data from a record. Use this function when isolating parent data to be stored in a Reports or database table.
function remove_subforms(r) {
// loop through and store each key
for(var key in r) {
// if property is a subform (array)
if(Array.isArray(r[key])) {
// delete the property
delete r[key];
}
}