utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* eslint-disable no-magic-numbers */
  2. "use strict";
  3. const fsAsync = require("fs").promises;
  4. const fs = require("fs");
  5. const path = require("path");
  6. /**
  7. * Check for intersection two arrays
  8. * @param {Array} arr1 array to
  9. * @param {Array} arr2 second array to combare against
  10. * @returns {boolean} is intersecting
  11. */
  12. exports.arraysHasIntersect = (arr1, arr2) => {
  13. let data = Array.isArray(arr1) ? arr1 : [arr1];
  14. let data2 = Array.isArray(arr2) ? arr2 : [arr2];
  15. for (const item of data)
  16. if (data2.indexOf(item) != -1) return true;
  17. return false;
  18. };
  19. exports.arraysIntersect = (arr1, arr2) => {
  20. let output = [];
  21. for (const item of arr1)
  22. if (arr2.indexOf(item) != -1) output.push(item);
  23. return output;
  24. };
  25. /**
  26. * Check for intersection of array values against string
  27. * @param {string} string string to be checked for intersection
  28. * @param {string} arr1 values to check
  29. * @returns {boolean} is intersecting
  30. */
  31. exports.stringHasIntersect = (string, arr) => {
  32. arr.forEach(item => {
  33. if (string.indexOf(item) !== -1) return true;
  34. });
  35. return false;
  36. };
  37. /**
  38. * Load JSON file in async style
  39. * @param {string} filepath path to file
  40. * @param {string} encoding file encoding
  41. * @returns {Object|false} return false if failed or Object if successful
  42. */
  43. exports.loadJsonAsync = async (filepath, encoding = "utf8") => {
  44. let result = false;
  45. try {
  46. result = await fsAsync.readFile(filepath, encoding);
  47. }
  48. // eslint-disable-next-line no-empty
  49. catch (ex) { }
  50. if (result) result = JSON.parse(result);
  51. return result;
  52. };
  53. /**
  54. * Get full path string
  55. * @param {string} str1 resolve elem 1
  56. * @param {string} str2 relative path
  57. * @returns {string} full path
  58. */
  59. exports.getFullPath = (str1, str2) => path.resolve(str1, str2);
  60. /**
  61. * Load JSON file iin sync way
  62. * @param {string} filepath path to file
  63. * @param {string} encoding file encoding
  64. * @returns {Object|false} return false if failed or Object if successful
  65. */
  66. exports.loadJson = (filepath, encoding = "utf8") => {
  67. try {
  68. return JSON.parse(fs.readFileSync(filepath, encoding));
  69. } catch (err) {
  70. return false;
  71. }
  72. };
  73. /**
  74. * Save JSON file synchronously with format
  75. * @param {string} obj object to save
  76. * @param {string} filepath path
  77. * @returns {undefined|false} returns false if error happened
  78. */
  79. exports.saveJson = (obj, filepath) => {
  80. try {
  81. fs.writeFileSync(filepath, JSON.stringify(obj, null, "\t"));
  82. } catch (err) {
  83. return false;
  84. }
  85. };
  86. /**
  87. * Return "skill base" for id
  88. * @param {number} skill skill id
  89. * @returns {number} returns false if error happened
  90. */
  91. exports.getSkillBase = (skill) => Math.floor(skill / 10000);