all files / lib/ options.js

100% Statements 10/10
67.65% Branches 23/34
100% Functions 7/7
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                               
/*!
 * Sheetrock
 * Quickly connect to, query, and lazy-load data from Google Sheets.
 * https://chriszarate.github.io/sheetrock/
 * License: MIT
 */
 
import * as config from './config';
import SheetrockError from './error';
 
// Extract a DOM element from a possible jQuery blob.
function extractElement(blob) {
  const isObj = blob && typeof blob === 'object';
  const isJqueryObj = isObj && blob.jquery && blob.length;
 
  Iif (isJqueryObj && blob[0].nodeType && blob[0].nodeType === 1) {
    return blob[0];
  }
 
  return null;
}
 
// Support some legacy option names.
function translateLegacyOptions(options) {
  const newOptions = {};
 
  Object.keys(options).forEach((key) => {
    Iif (config.legacyOptions.hasOwnProperty(key)) {
      newOptions[config.legacyOptions[key]] = options[key];
    } else {
      newOptions[key] = options[key];
    }
  });
 
  return newOptions;
}
 
function setUserOptions(options) {
  const validatedOptions = {};
 
  // Look for valid DOM element target.
  validatedOptions.target = extractElement(options.target);
 
  // Correct bad integer values.
  validatedOptions.fetchSize = Math.max(0, parseInt(options.fetchSize, 10) || 0);
 
  // Require DOM element or a callback function. Otherwise, the data has nowhere to go.
  if (!validatedOptions.target && !options.callback) {
    throw new SheetrockError('No element targeted or callback provided.');
  }
 
  // Extend default options.
  return Object.assign({}, config.defaults, options, validatedOptions);
}
 
function setRequestOptions(options, hasData) {
  // If the user passed data, we don't want to validate the URL.
  if (hasData) {
    return {};
  }
 
  const url = options.url || '';
 
  // Get API endpoint, key, and gid from a Google Sheet URL.
  const sheetType = config.sheetTypes[
    Object.keys(config.sheetTypes).find(key => {
      const value = config.sheetTypes[key];
      return value.keyFormat.test(url) && value.gidFormat.test(url);
    })
  ];
 
  Iif (sheetType) {
    const sheetKey = url.match(sheetType.keyFormat)[1];
    return {
      key: sheetKey,
      gid: url.match(sheetType.gidFormat)[1],
      apiEndpoint: sheetType.apiEndpoint.replace('%key%', sheetKey),
    };
  }
 
  // Require a Sheet key and gid.
  throw new SheetrockError('No key/gid in the provided URL.');
}
 
class Options {
  constructor(options = {}, hasData = false) {
    this.user = setUserOptions(translateLegacyOptions(options));
    this.request = setRequestOptions(this.user, hasData);
  }
 
  // Generate a request ID that can be used as a caching key.
  get requestIndex() {
    return `${this.request.key}_${this.request.gid}_${this.user.query}`;
  }
}
 
export default Options;