1 |
|
2 |
|
3 |
|
4 |
|
5 | var _ = require('lodash'),
|
6 | SchemaType = require('../schematype'),
|
7 | util = require('../util');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var SchemaArray = module.exports = function(options){
|
17 | options = options || {};
|
18 |
|
19 | SchemaType.call(this, options);
|
20 |
|
21 | if (options.hasOwnProperty('default')){
|
22 | var defaults = options.default,
|
23 | fn = typeof defaults === 'function';
|
24 | }
|
25 |
|
26 | this.default = function(){
|
27 | return fn ? defaults() : defaults || [];
|
28 | };
|
29 | };
|
30 |
|
31 | SchemaArray.__proto__ = SchemaType;
|
32 | SchemaArray.type = SchemaArray.prototype.type = Array;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | SchemaArray.prototype.__proto__ = SchemaType.prototype;
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | SchemaArray.prototype.checkRequired = function(value){
|
49 | return Array.isArray(value);
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | SchemaArray.prototype.cast = function(value){
|
61 | if (Array.isArray(value)){
|
62 | return value;
|
63 | } else {
|
64 | return [value];
|
65 | }
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | SchemaArray.prototype.compare = function(data, value){
|
78 | return util.compare(data, value);
|
79 | };
|
80 |
|
81 | SchemaArray.prototype.q$length = SchemaArray.prototype.q$size = function(data, value){
|
82 | if (!data) return false;
|
83 |
|
84 | var length = data.length;
|
85 |
|
86 | if (_.isObject(value)){
|
87 | var match = true,
|
88 | keys = Object.keys(value);
|
89 |
|
90 | for (var i = 0, len = keys.length; i < len; i++){
|
91 | var key = keys[i],
|
92 | rule = value[key];
|
93 |
|
94 | switch (key){
|
95 | case '$lt':
|
96 | match = length < rule;
|
97 | break;
|
98 |
|
99 | case '$lte':
|
100 | case '$max':
|
101 | match = length <= rule;
|
102 | break;
|
103 |
|
104 | case '$gt':
|
105 | match = length > rule;
|
106 | break;
|
107 |
|
108 | case '$gte':
|
109 | case '$min':
|
110 | match = length >= rule;
|
111 | break;
|
112 | }
|
113 |
|
114 | if (!match) break;
|
115 | }
|
116 |
|
117 | return match;
|
118 | } else {
|
119 | return length == value;
|
120 | }
|
121 | };
|
122 |
|
123 | SchemaArray.prototype.q$in = function(data, value){
|
124 | if (!data || !data.length) return false;
|
125 | if (!Array.isArray(value)) value = [value];
|
126 |
|
127 | var match = false;
|
128 |
|
129 | for (var i = 0, len = value.length; i < len; i++){
|
130 | if (data.indexOf(value[i]) > -1){
|
131 | match = true;
|
132 | break;
|
133 | }
|
134 | }
|
135 |
|
136 | return match;
|
137 | };
|
138 |
|
139 | SchemaArray.prototype.q$nin = function(data, value){
|
140 | if (!data) return false;
|
141 | if (!Array.isArray(value)) value = [value];
|
142 |
|
143 | var match = true;
|
144 |
|
145 | for (var i = 0, len = value.length; i < len; i++){
|
146 | if (data.indexOf(value[i]) > -1){
|
147 | match = false;
|
148 | break;
|
149 | }
|
150 | }
|
151 |
|
152 | return match;
|
153 | };
|
154 |
|
155 | SchemaArray.prototype.q$all = function(data, value){
|
156 | if (!data) return false;
|
157 | if (!Array.isArray(value)) value = [value];
|
158 |
|
159 | var match = true;
|
160 |
|
161 | for (var i = 0, len = value.length; i < len; i++){
|
162 | if (data.indexOf(value[i]) === -1){
|
163 | match = false;
|
164 | break;
|
165 | }
|
166 | }
|
167 |
|
168 | return match;
|
169 | };
|
170 |
|
171 | SchemaArray.prototype.u$push = function(data, value){
|
172 | if (!data) return [].concat(value);
|
173 |
|
174 | return [].concat(data, value);
|
175 | };
|
176 |
|
177 | SchemaArray.prototype.u$unshift = function(data, value){
|
178 | if (!data) return [].concat(value);
|
179 |
|
180 | return [].concat(value, data);
|
181 | };
|
182 |
|
183 | SchemaArray.prototype.u$pull = function(data, value){
|
184 | if (!data) return;
|
185 |
|
186 | if (Array.isArray(value)){
|
187 | return _.difference(data, value);
|
188 | } else {
|
189 | return _.without(data, value);
|
190 | }
|
191 | };
|
192 |
|
193 | SchemaArray.prototype.u$shift = function(data, value){
|
194 | if (!data) return;
|
195 |
|
196 | value = value === true ? 1 : +value;
|
197 |
|
198 | if (value > 0){
|
199 | return data.slice(value);
|
200 | } else {
|
201 | return data.slice(0, data.length + value);
|
202 | }
|
203 | };
|
204 |
|
205 | SchemaArray.prototype.u$pop = function(data, value){
|
206 | if (!data) return;
|
207 |
|
208 | value = value === true ? 1 : +value;
|
209 |
|
210 | if (value > 0){
|
211 | return data.slice(0, data.length - value);
|
212 | } else {
|
213 | return data.slice(-value, data.length);
|
214 | }
|
215 | };
|
216 |
|
217 | SchemaArray.prototype.u$addToSet = function(data, value){
|
218 | if (!Array.isArray(value)) value = [value];
|
219 | if (!data) return value;
|
220 |
|
221 | var arr = data.slice();
|
222 |
|
223 | value.forEach(function(item){
|
224 | if (arr.indexOf(item) == -1){
|
225 | arr.push(item);
|
226 | }
|
227 | });
|
228 |
|
229 | return arr;
|
230 | }; |
\ | No newline at end of file |