boolean.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*!
  2. * Stylus - Boolean
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var Node = require('./node')
  10. , nodes = require('./');
  11. /**
  12. * Initialize a new `Boolean` node with the given `val`.
  13. *
  14. * @param {Boolean} val
  15. * @api public
  16. */
  17. var Boolean = module.exports = function Boolean(val){
  18. Node.call(this);
  19. if (this.nodeName) {
  20. this.val = !!val;
  21. } else {
  22. return new Boolean(val);
  23. }
  24. };
  25. /**
  26. * Inherit from `Node.prototype`.
  27. */
  28. Boolean.prototype.__proto__ = Node.prototype;
  29. /**
  30. * Return `this` node.
  31. *
  32. * @return {Boolean}
  33. * @api public
  34. */
  35. Boolean.prototype.toBoolean = function(){
  36. return this;
  37. };
  38. /**
  39. * Return `true` if this node represents `true`.
  40. *
  41. * @return {Boolean}
  42. * @api public
  43. */
  44. Boolean.prototype.__defineGetter__('isTrue', function(){
  45. return this.val;
  46. });
  47. /**
  48. * Return `true` if this node represents `false`.
  49. *
  50. * @return {Boolean}
  51. * @api public
  52. */
  53. Boolean.prototype.__defineGetter__('isFalse', function(){
  54. return ! this.val;
  55. });
  56. /**
  57. * Negate the value.
  58. *
  59. * @return {Boolean}
  60. * @api public
  61. */
  62. Boolean.prototype.negate = function(){
  63. return new Boolean(!this.val);
  64. };
  65. /**
  66. * Return 'Boolean'.
  67. *
  68. * @return {String}
  69. * @api public
  70. */
  71. Boolean.prototype.inspect = function(){
  72. return '[Boolean ' + this.val + ']';
  73. };
  74. /**
  75. * Return 'true' or 'false'.
  76. *
  77. * @return {String}
  78. * @api public
  79. */
  80. Boolean.prototype.toString = function(){
  81. return this.val
  82. ? 'true'
  83. : 'false';
  84. };
  85. /**
  86. * Return a JSON representaiton of this node.
  87. *
  88. * @return {Object}
  89. * @api public
  90. */
  91. Boolean.prototype.toJSON = function(){
  92. return {
  93. __type: 'Boolean',
  94. val: this.val
  95. };
  96. };