source: trip-planner-front/node_modules/stylus/lib/nodes/expression.js@ eed0bf8

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

initial commit

  • Property mode set to 100644
File size: 4.6 KB
Line 
1
2/*!
3 * Stylus - Expression
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('../nodes')
14 , utils = require('../utils');
15
16/**
17 * Initialize a new `Expression`.
18 *
19 * @param {Boolean} isList
20 * @api public
21 */
22
23var Expression = module.exports = function Expression(isList){
24 Node.call(this);
25 this.nodes = [];
26 this.isList = isList;
27};
28
29/**
30 * Check if the variable has a value.
31 *
32 * @return {Boolean}
33 * @api public
34 */
35
36Expression.prototype.__defineGetter__('isEmpty', function(){
37 return !this.nodes.length;
38});
39
40/**
41 * Return the first node in this expression.
42 *
43 * @return {Node}
44 * @api public
45 */
46
47Expression.prototype.__defineGetter__('first', function(){
48 return this.nodes[0]
49 ? this.nodes[0].first
50 : nodes.null;
51});
52
53/**
54 * Hash all the nodes in order.
55 *
56 * @return {String}
57 * @api public
58 */
59
60Expression.prototype.__defineGetter__('hash', function(){
61 return this.nodes.map(function(node){
62 return node.hash;
63 }).join('::');
64});
65
66/**
67 * Inherit from `Node.prototype`.
68 */
69
70Expression.prototype.__proto__ = Node.prototype;
71
72/**
73 * Return a clone of this node.
74 *
75 * @return {Node}
76 * @api public
77 */
78
79Expression.prototype.clone = function(parent){
80 var clone = new this.constructor(this.isList);
81 clone.preserve = this.preserve;
82 clone.lineno = this.lineno;
83 clone.column = this.column;
84 clone.filename = this.filename;
85 clone.nodes = this.nodes.map(function(node) {
86 return node.clone(parent, clone);
87 });
88 return clone;
89};
90
91/**
92 * Push the given `node`.
93 *
94 * @param {Node} node
95 * @api public
96 */
97
98Expression.prototype.push = function(node){
99 this.nodes.push(node);
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
111Expression.prototype.operate = function(op, right, val){
112 switch (op) {
113 case '[]=':
114 var self = this
115 , range = utils.unwrap(right).nodes
116 , val = utils.unwrap(val)
117 , len
118 , node;
119 range.forEach(function(unit){
120 len = self.nodes.length;
121 if ('unit' == unit.nodeName) {
122 var i = unit.val < 0 ? len + unit.val : unit.val
123 , n = i;
124 while (i-- > len) self.nodes[i] = nodes.null;
125 self.nodes[n] = val;
126 } else if (unit.string) {
127 node = self.nodes[0];
128 if (node && 'object' == node.nodeName) node.set(unit.string, val.clone());
129 }
130 });
131 return val;
132 case '[]':
133 var expr = new nodes.Expression
134 , vals = utils.unwrap(this).nodes
135 , range = utils.unwrap(right).nodes
136 , node;
137 range.forEach(function(unit){
138 if ('unit' == unit.nodeName) {
139 node = vals[unit.val < 0 ? vals.length + unit.val : unit.val];
140 } else if ('object' == vals[0].nodeName) {
141 node = vals[0].get(unit.string);
142 }
143 if (node) expr.push(node);
144 });
145 return expr.isEmpty
146 ? nodes.null
147 : utils.unwrap(expr);
148 case '||':
149 return this.toBoolean().isTrue
150 ? this
151 : right;
152 case 'in':
153 return Node.prototype.operate.call(this, op, right);
154 case '!=':
155 return this.operate('==', right, val).negate();
156 case '==':
157 var len = this.nodes.length
158 , right = right.toExpression()
159 , a
160 , b;
161 if (len != right.nodes.length) return nodes.false;
162 for (var i = 0; i < len; ++i) {
163 a = this.nodes[i];
164 b = right.nodes[i];
165 if (a.operate(op, b).isTrue) continue;
166 return nodes.false;
167 }
168 return nodes.true;
169 break;
170 default:
171 return this.first.operate(op, right, val);
172 }
173};
174
175/**
176 * Expressions with length > 1 are truthy,
177 * otherwise the first value's toBoolean()
178 * method is invoked.
179 *
180 * @return {Boolean}
181 * @api public
182 */
183
184Expression.prototype.toBoolean = function(){
185 if (this.nodes.length > 1) return nodes.true;
186 return this.first.toBoolean();
187};
188
189/**
190 * Return "<a> <b> <c>" or "<a>, <b>, <c>" if
191 * the expression represents a list.
192 *
193 * @return {String}
194 * @api public
195 */
196
197Expression.prototype.toString = function(){
198 return '(' + this.nodes.map(function(node){
199 return node.toString();
200 }).join(this.isList ? ', ' : ' ') + ')';
201};
202
203/**
204 * Return a JSON representation of this node.
205 *
206 * @return {Object}
207 * @api public
208 */
209
210Expression.prototype.toJSON = function(){
211 return {
212 __type: 'Expression',
213 isList: this.isList,
214 preserve: this.preserve,
215 lineno: this.lineno,
216 column: this.column,
217 filename: this.filename,
218 nodes: this.nodes
219 };
220};
Note: See TracBrowser for help on using the repository browser.