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