object.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*!
  2. * Stylus - Object
  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. , nativeObj = {}.constructor;
  12. /**
  13. * Initialize a new `Object`.
  14. *
  15. * @api public
  16. */
  17. var Object = module.exports = function Object(){
  18. Node.call(this);
  19. this.vals = {};
  20. this.keys = {};
  21. };
  22. /**
  23. * Inherit from `Node.prototype`.
  24. */
  25. Object.prototype.__proto__ = Node.prototype;
  26. /**
  27. * Set `key` to `val`.
  28. *
  29. * @param {String} key
  30. * @param {Node} val
  31. * @return {Object} for chaining
  32. * @api public
  33. */
  34. Object.prototype.setValue = function(key, val){
  35. this.vals[key] = val;
  36. return this;
  37. };
  38. /**
  39. * Alias for `setValue` for compatible API
  40. */
  41. Object.prototype.set = Object.prototype.setValue;
  42. /**
  43. * Set `key` to `val`.
  44. *
  45. * @param {String} key
  46. * @param {Node} val
  47. * @return {Object} for chaining
  48. * @api public
  49. */
  50. Object.prototype.setKey = function(key, val){
  51. this.keys[key] = val;
  52. return this;
  53. };
  54. /**
  55. * Return length.
  56. *
  57. * @return {Number}
  58. * @api public
  59. */
  60. Object.prototype.__defineGetter__('length', function() {
  61. return nativeObj.keys(this.vals).length;
  62. });
  63. /**
  64. * Get `key`.
  65. *
  66. * @param {String} key
  67. * @return {Node}
  68. * @api public
  69. */
  70. Object.prototype.get = function(key){
  71. return this.vals[key] || nodes.null;
  72. };
  73. /**
  74. * Has `key`?
  75. *
  76. * @param {String} key
  77. * @return {Boolean}
  78. * @api public
  79. */
  80. Object.prototype.has = function(key){
  81. return key in this.vals;
  82. };
  83. /**
  84. * Operate on `right` with the given `op`.
  85. *
  86. * @param {String} op
  87. * @param {Node} right
  88. * @return {Node}
  89. * @api public
  90. */
  91. Object.prototype.operate = function(op, right){
  92. switch (op) {
  93. case '.':
  94. case '[]':
  95. return this.get(right.hash);
  96. case '==':
  97. var vals = this.vals
  98. , a
  99. , b;
  100. if ('object' != right.nodeName || this.length != right.length)
  101. return nodes.false;
  102. for (var key in vals) {
  103. a = vals[key];
  104. b = right.vals[key];
  105. if (a.operate(op, b).isFalse)
  106. return nodes.false;
  107. }
  108. return nodes.true;
  109. case '!=':
  110. return this.operate('==', right).negate();
  111. default:
  112. return Node.prototype.operate.call(this, op, right);
  113. }
  114. };
  115. /**
  116. * Return Boolean based on the length of this object.
  117. *
  118. * @return {Boolean}
  119. * @api public
  120. */
  121. Object.prototype.toBoolean = function(){
  122. return nodes.Boolean(this.length);
  123. };
  124. /**
  125. * Convert object to string with properties.
  126. *
  127. * @return {String}
  128. * @api private
  129. */
  130. Object.prototype.toBlock = function(){
  131. var str = '{'
  132. , key
  133. , val;
  134. for (key in this.vals) {
  135. val = this.get(key);
  136. if ('object' == val.first.nodeName) {
  137. str += key + ' ' + val.first.toBlock();
  138. } else {
  139. switch (key) {
  140. case '@charset':
  141. str += key + ' ' + val.first.toString() + ';';
  142. break;
  143. default:
  144. str += key + ':' + toString(val) + ';';
  145. }
  146. }
  147. }
  148. str += '}';
  149. return str;
  150. function toString(node) {
  151. if (node.nodes) {
  152. return node.nodes.map(toString).join(node.isList ? ',' : ' ');
  153. } else if ('literal' == node.nodeName && ',' == node.val) {
  154. return '\\,';
  155. }
  156. return node.toString();
  157. }
  158. };
  159. /**
  160. * Return a clone of this node.
  161. *
  162. * @return {Node}
  163. * @api public
  164. */
  165. Object.prototype.clone = function(parent){
  166. var clone = new Object;
  167. clone.lineno = this.lineno;
  168. clone.column = this.column;
  169. clone.filename = this.filename;
  170. var key;
  171. for (key in this.vals) {
  172. clone.vals[key] = this.vals[key].clone(parent, clone);
  173. }
  174. for (key in this.keys) {
  175. clone.keys[key] = this.keys[key].clone(parent, clone);
  176. }
  177. return clone;
  178. };
  179. /**
  180. * Return a JSON representation of this node.
  181. *
  182. * @return {Object}
  183. * @api public
  184. */
  185. Object.prototype.toJSON = function(){
  186. return {
  187. __type: 'Object',
  188. vals: this.vals,
  189. keys: this.keys,
  190. lineno: this.lineno,
  191. column: this.column,
  192. filename: this.filename
  193. };
  194. };
  195. /**
  196. * Return "{ <prop>: <val> }"
  197. *
  198. * @return {String}
  199. * @api public
  200. */
  201. Object.prototype.toString = function(){
  202. var obj = {};
  203. for (var prop in this.vals) {
  204. obj[prop] = this.vals[prop].toString();
  205. }
  206. return JSON.stringify(obj);
  207. };