-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStruct.js
More file actions
297 lines (296 loc) · 7.32 KB
/
Copy pathStruct.js
File metadata and controls
297 lines (296 loc) · 7.32 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*=======================*/
/*js数据结构*/
;(function(root, factory)
{
if (typeof module !== 'undefined' && module.exports)
{
module.exports = factory();
} else if (typeof define === 'function' && define.amd)
{
define(factory)
} else
{
root.Struct = factory();
}
} (this, function ()
{"use strict";
/*
缩写解释
t = type 数据类型
s = struct 数据结构
p = param 参数
*/
function getType(obj)
{
//tostring会返回对应不同的标签的构造函数
var toString = Object.prototype.toString;
var map = {
'[object Boolean]' : 'boolean',
'[object Number]' : 'number',
'[object String]' : 'string',
'[object Function]' : 'function',
'[object Array]' : 'array',
'[object Date]' : 'date',
'[object RegExp]' : 'regExp',
'[object Undefined]': 'undefined',
'[object Null]' : 'null',
'[object Object]' : 'object'
};
if(obj instanceof Element) {
return 'element';
}
return map[toString.call(obj)];
}
function isFunction(fn)
{/*判断function*/
return getType(fn)=== 'function';
}
function isString(fn)
{/*判断字符串*/
return getType(fn)=== 'string';
}
function isNumber(fn)
{/*判断数字*/
return getType(fn)=== 'number' && !isNaN(fn);
}
function isBoolean(fn)
{/*判断布尔*/
return getType(fn)=== 'boolean';
}
function isArray(fn)
{/*判断数组*/
return getType(fn)=== 'array';
}
function isObject(fn)
{/*判断为object对象*/
return getType(fn)=== 'object';
}
function isJsonString(fn)
{/*判断json字符串*/
var r;
try {
r = JSON.parse(fn);
} catch (err) {
return false;
};
return isObject(r) || isArray(r);
}
function deepClone(data)
{/*深度拷贝*/
var type = getType(data)
, obj;
if(type === 'array'){
obj = [];
} else if(type === 'object'){
obj = {};
} else {
//不再具有下一层次
return data;
}
if(type === 'array'){
for(var i = 0, len = data.length; i < len; i++){
obj.push(deepClone(data[i]));
}
} else if(type === 'object'){
for(var key in data){
obj[key] = deepClone(data[key]);
}
}
return obj;
}
function s (structure)
{/*数据结构*/
if (this instanceof s === false)
{
return new s(structure);
}
var self = this;
self._structure = structure;
};
s.prototype._check = function (operation, st, data)
{/*operation 有两种 formatData or validate*/
var self = this
, result = true
, errors = []
, new_data = check(st, data);
for (var i=0; i<errors.length; i++)
{
console.log(errors[i]);
}
self.errors = errors;
if (operation === 'validate') return result;
if (operation === 'formatData') return new_data;
function check(st, data, k_name)
{/*开始检查*/
var obj;
if (st instanceof t)
{/*is type*/
var type = st;
for (var i=0; i<type._tasks.length; i++)
{/*遍历需要判断的类型*/
if (type._tasks[i](data) === false)
{
var name = type._tasks[i].__name__
, er = (k_name !== undefined? k_name: '') + ' : ' + s.errorMap[name]
, df = type._default;
errors.push(er);
result = false;
return deepClone(df);
}
}
return data;
} else if (st instanceof s)
{/*is Struct*/
return check(st._structure, data, k_name)
} else if (isObject( st))
{/*is obj*/
obj = {};
} else if (isArray( st))
{/*is array*/
obj = [];
} else { return deepClone(st)};
if (isObject( st))
{
if(!isObject(data)) data = {};
obj = deepClone(data);
for(var key in st)
{
obj[key] = check(st[key], data[key], (k_name !== undefined? k_name + '.' + key: key))
}
} else if (isArray( st))
{
if(!isArray(data)) data = [];
obj = deepClone(data);
for(var i=0; i<st.length; i++)
{
obj[i] = check(st[i], data[i], (k_name !== undefined? k_name + '[' + i + ']': 'array['+i+']'))
}
}
return obj;
}
};
s.prototype.formatData = function (data)
{/*根据default格式化数据*/
var self = this;
return self._check('formatData', self._structure, data);
};
s.prototype.validate = function (data)
{/*验证数据*/
var self = this
, result = true;
result = self._check('validate', self._structure, data);
return result;
};
var t = function ()
{/*数据类型*/
if (this instanceof t === false)
{
return new t();
}
var self = this;
self._tasks = [];
self._default = undefined;
};
Object.defineProperty(t.prototype, 'default',
{/*定义默认值*/
enumerable: true,
configurable: false,
get: function ()
{
return function(p)
{
this._default = p;
return this;
}
}
});
t.method = function (name, checker, hasParam)
{/*注册类型*/
if (hasParam === true)
{/*有参数类型*/
Object.defineProperty(t.prototype, name,
{
enumerable: true,
configurable: false,
get: function ()
{
if (!isFunction(checker())) throw 'The type that defines a parameter must be function return function';
return function ()
{
var _c = checker.apply(null, arguments);
_c.__name__ = name;
this._tasks.push(_c);
return this;
};
}
})
} else
{/*无参数类型*/
Object.defineProperty(t.prototype, name,
{
enumerable: true,
configurable: false,
get: function ()
{
checker.__name__ = name;
this._tasks.push(checker);
return this;
}
})
}
};
t.method('enum', function ()
{/*枚举*/
var arr = Array.prototype.slice.apply(arguments);
return function (param)
{
return arr.indexOf(param) > -1;
}
}, true);
t.method('string', function (p)
{/*字符串*/
return isString(p) === true;
});
t.method('number', function (p)
{/*字符串*/
return isNumber(p) === true;
});
t.method('int', function (p)
{/*整型*/
return isNumber(p) && (p%1 === 0);
});
t.method('bool', function (p)
{/*布尔true or false*/
return isBoolean(p) === true;
});
t.method('array', function (p)
{/*数组*/
return isArray(p) === true;
});
t.method('jsonString', function (p)
{/*json*/
return isJsonString(p) === true;
});
t.errorMap = {
/*错误对照表*/
'enum': 'The parameter must be a member of the enum',
'string': 'The parameter must be a string',
'number': 'The parameter must be a number and not NaN',
'int': 'The parameter must be a int',
'bool': 'The parameter must be a bool',
'array': 'The parameter must be a array',
'jsonString': 'The parameter must be a jsonString'
};
Object.defineProperty(s, 'type',
{
enumerable: true,
configurable: false,
get: function ()
{
return new t();
}
});
s.define = t.method; //开放自定义接口
s.errorMap = t.errorMap; //错误对照表
return s
}));