utils.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. /* eslint object-shorthand: 0 */
  3. /* eslint no-param-reassign: 0 */
  4. /* eslint import/no-unresolved: 0 */
  5. var electron = require('electron');
  6. var app = electron.app || electron.remote.app;
  7. var userData = app.getPath('userData');
  8. var path = require('path');
  9. function isFunction(functionToCheck) {
  10. var getType = {};
  11. return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
  12. }
  13. function tryParseJson(stringJson) {
  14. var object = void 0;
  15. try {
  16. object = JSON.parse(stringJson);
  17. } catch (e) {
  18. return e;
  19. }
  20. return object;
  21. }
  22. function tryStringifyJson(objectJson) {
  23. if (typeof objectJson === 'string') {
  24. return objectJson;
  25. }
  26. var string = void 0;
  27. try {
  28. string = JSON.stringify(objectJson);
  29. } catch (e) {
  30. return e;
  31. }
  32. return string;
  33. }
  34. function getElectronFullPath(filePath) {
  35. return path.join(userData, filePath);
  36. }
  37. function addDotJsonIfNeeded(filePath) {
  38. if (filePath.substring(filePath.length - 5, filePath.length) === '.json') {
  39. return filePath;
  40. }
  41. return filePath + '.json';
  42. }
  43. function processPath(filePath) {
  44. return addDotJsonIfNeeded(getElectronFullPath(filePath));
  45. }
  46. function processPathNoJson(filePath) {
  47. return getElectronFullPath(filePath);
  48. }
  49. module.exports = {
  50. isFunction: isFunction,
  51. tryParseJson: tryParseJson,
  52. tryStringifyJson: tryStringifyJson,
  53. processPath: processPath,
  54. processPathNoJson: processPathNoJson
  55. };