import R from 'ramda' /* eslint-disable */ String.prototype.replaceAll = function (search, replacement) { const target = this return target.replace(new RegExp(search, 'g'), replacement) } /* eslint-enable */ function camelize (str) { return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function (match) { if (+match === 0) return '' // or if (/\s+/.test(match)) for white spaces return match.toUpperCase() }) } export const fmtObj = R.curry( ( columnNames, row, options = { noSpacesInKeys: false, hyphenatedKeys: false, camelCaseKeys: false } ) => { const obj = {} const fmtColName = colName => { if (options.camelCaseKeys) { return camelize(colName) } else if (options.hyphenatedKeys) { return colName.toLowerCase().replaceAll(' ', '-') } else if (options.noSpacesInKeys) { return colName.replaceAll(' ', '') } else { return colName } } columnNames.forEach((columnName, idx) => { obj[fmtColName(columnName)] = row[idx] }) return obj } ) /* search for object with key in array. Return index if exists, or -1 if not */ export const idxSearcher = R.curry((attrName, searchValue, myArray) => { for (var i = 0; i < myArray.length; i++) { if (myArray[i][attrName] === searchValue) { return i } } return -1 }) /* more site specific functions. TODO: maybe move to another folder? */ export function fmtSheetTitle (name) { return name.replaceAll(' ', '-').toLowerCase() } export function fmtBlueprinterTitles (tabs) { const obj = {} Object.keys(tabs).forEach(tab => { const name = fmtSheetTitle(tab) obj[name] = tabs[tab] }) return obj } export function deriveFilename (sheet, tab) { return `${fmtSheetTitle(sheet)}-${fmtSheetTitle(tab)}.json` } export function bp (full) { const blueprint = { name: R.clone(full.name), sheet: R.clone(full.sheet), dialects: R.clone(full.dialects), routes: {} } Object.keys(full.routes).forEach(route => { blueprint.routes[route] = { options: R.clone(full.routes[route].options) } }) return blueprint } export function isFunction (functionToCheck) { return ( functionToCheck && {}.toString.call(functionToCheck) === '[object Function]' ) }