source: trip-planner-front/node_modules/stylus/lib/nodes/object.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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