parsers.js 6.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. function numberParser(key) {
  3. return function (opt) {
  4. const intOpt = parseInt(opt);
  5. if (!Number.isInteger(intOpt)) {
  6. throw new Error(`Key ${key} has invalid value ${opt}`);
  7. }
  8. return intOpt;
  9. };
  10. }
  11. function numberOrBoolParser(key) {
  12. return function (opt) {
  13. if (typeof opt === 'boolean') {
  14. return opt;
  15. }
  16. if (opt === 'true') {
  17. return true;
  18. }
  19. if (opt === 'false') {
  20. return false;
  21. }
  22. return numberParser(key)(opt);
  23. };
  24. }
  25. function numberOrStringParser(key) {
  26. return function (opt) {
  27. if (typeof opt === 'string') {
  28. return opt;
  29. }
  30. return numberParser(key)(opt);
  31. };
  32. }
  33. function objectParser(opt) {
  34. if (typeof opt == 'object') {
  35. return opt;
  36. }
  37. return JSON.parse(opt);
  38. }
  39. function arrayParser(opt) {
  40. if (Array.isArray(opt)) {
  41. return opt;
  42. } else if (typeof opt === 'string') {
  43. return opt.split(',');
  44. } else {
  45. throw new Error(`${opt} should be a comma separated string or an array`);
  46. }
  47. }
  48. function moduleOrObjectParser(opt) {
  49. if (typeof opt == 'object') {
  50. return opt;
  51. }
  52. try {
  53. return JSON.parse(opt);
  54. } catch (e) {
  55. /* */
  56. }
  57. return opt;
  58. }
  59. function booleanParser(opt) {
  60. if (opt == true || opt == 'true' || opt == '1') {
  61. return true;
  62. }
  63. return false;
  64. }
  65. function nullParser(opt) {
  66. if (opt == 'null') {
  67. return null;
  68. }
  69. return opt;
  70. }
  71. module.exports = {
  72. numberParser,
  73. numberOrBoolParser,
  74. numberOrStringParser,
  75. nullParser,
  76. booleanParser,
  77. moduleOrObjectParser,
  78. arrayParser,
  79. objectParser
  80. };
  81. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJudW1iZXJQYXJzZXIiLCJrZXkiLCJvcHQiLCJp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