server.js

  1. /**
  2. * To work out which environment the site is on (dev/staging/live)
  3. * @module
  4. * @since 0.0.51
  5. */
  6. /**
  7. * Returns true if the code is being executed on a local development server; false otherwise
  8. * @returns {boolean}
  9. */
  10. const isDev = () => window.location.href.search(/(http:\/\/|https:\/\/)d51[a-z]+/g) > -1;
  11. /**
  12. * Returns true if the code is being executed on a staging server; false otherwise
  13. * @returns {boolean}
  14. */
  15. const isStaging = () => window.location.href.search(/(http:\/\/|https:\/\/)d51[0-9]+/g) > -1;
  16. /**
  17. * Returns true if the code is being executed on a live server; false if it's on a local dev or staging server
  18. * @returns {boolean}
  19. */
  20. const isLive = () => !isDev() && !isStaging();
  21. /**
  22. * Get the prefix of the url identifying the development/staging server.
  23. * @returns {string} eg: d51alfie.comcar.co.uk --> d51alfie, d5117.comcar.co.uk --> d5117, comcar.co.uk --> [empty string]
  24. */
  25. const extractDevUrlPrefix = () => {
  26. let str_dev_url_prefix = '';
  27. const arr_dev_url_prefix_with_dot = window.location.href.match(/d51[a-z0-9]+($|\.)/g);
  28. if (arr_dev_url_prefix_with_dot.length) {
  29. const str_dev_url_prefixWithDot = arr_dev_url_prefix_with_dot[0];
  30. str_dev_url_prefix = str_dev_url_prefixWithDot.replace(/\./g, '');
  31. }
  32. return str_dev_url_prefix;
  33. };
  34. export {
  35. isDev,
  36. isStaging,
  37. isLive,
  38. extractDevUrlPrefix,
  39. };