enumItem.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
  3. var _isType = require("./isType");
  4. var isObject = _isType.isObject;
  5. var isString = _isType.isString;
  6. /**
  7. * Represents an Item of an Enum.
  8. * @param {String} key The Enum key.
  9. * @param {Number} value The Enum value.
  10. */
  11. var EnumItem = (function () {
  12. /*constructor reference so that, this.constructor===EnumItem//=>true */
  13. function EnumItem(key, value) {
  14. var options = arguments[2] === undefined ? {} : arguments[2];
  15. _classCallCheck(this, EnumItem);
  16. this.key = key;
  17. this.value = value;
  18. this._options = options;
  19. this._options.ignoreCase = this._options.ignoreCase || false;
  20. }
  21. /**
  22. * Checks if the flagged EnumItem has the passing object.
  23. * @param {EnumItem || String || Number} value The object to check with.
  24. * @return {Boolean} The check result.
  25. */
  26. EnumItem.prototype.has = function has(value) {
  27. if (EnumItem.isEnumItem(value)) {
  28. return (this.value & value.value) !== 0;
  29. } else if (isString(value)) {
  30. if (this._options.ignoreCase) {
  31. return this.key.toLowerCase().indexOf(value.toLowerCase()) >= 0;
  32. }
  33. return this.key.indexOf(value) >= 0;
  34. } else {
  35. return (this.value & value) !== 0;
  36. }
  37. };
  38. /**
  39. * Checks if the EnumItem is the same as the passing object.
  40. * @param {EnumItem || String || Number} key The object to check with.
  41. * @return {Boolean} The check result.
  42. */
  43. EnumItem.prototype.is = function is(key) {
  44. if (EnumItem.isEnumItem(key)) {
  45. return this.key === key.key;
  46. } else if (isString(key)) {
  47. if (this._options.ignoreCase) {
  48. return this.key.toLowerCase() === key.toLowerCase();
  49. }
  50. return this.key === key;
  51. } else {
  52. return this.value === key;
  53. }
  54. };
  55. /**
  56. * Returns String representation of this EnumItem.
  57. * @return {String} String representation of this EnumItem.
  58. */
  59. EnumItem.prototype.toString = function toString() {
  60. return this.key;
  61. };
  62. /**
  63. * Returns JSON object representation of this EnumItem.
  64. * @return {String} JSON object representation of this EnumItem.
  65. */
  66. EnumItem.prototype.toJSON = function toJSON() {
  67. return this.key;
  68. };
  69. /**
  70. * Returns the value to compare with.
  71. * @return {String} The value to compare with.
  72. */
  73. EnumItem.prototype.valueOf = function valueOf() {
  74. return this.value;
  75. };
  76. EnumItem.isEnumItem = function isEnumItem(value) {
  77. return value instanceof EnumItem || isObject(value) && value.key !== undefined && value.value !== undefined;
  78. };
  79. return EnumItem;
  80. })();
  81. module.exports = EnumItem;