forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTypes.js
More file actions
471 lines (429 loc) · 16.1 KB
/
BaseTypes.js
File metadata and controls
471 lines (429 loc) · 16.1 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import {SuperMap} from '../SuperMap';
/**
*@namespace SuperMap
*@category BaseTypes Namespace
*/
/**
* @function SuperMap.inherit
* @description 除了 C 和 P 两个必要参数外,可以传递任意数量的对象,这些对象都将继承C。
* @memberOf SuperMap
* @param {Object} C - 继承的类。
* @param {Object} P - 被继承的父类。
*/
SuperMap.inherit = function (C, P) {
var F = function () {
};
F.prototype = P.prototype;
C.prototype = new F;
var i, l, o;
for (i = 2, l = arguments.length; i < l; i++) {
o = arguments[i];
if (typeof o === "function") {
o = o.prototype;
}
SuperMap.Util.extend(C.prototype, o);
}
};
/**
* @function SuperMap.mixin
* @description 实现多重继承。
* @memberOf SuperMap
* @param {Class|Object} ...mixins - 继承的类。
*/
SuperMap.mixin = function (...mixins) {
class Mix {
constructor(options) {
for (var index = 0; index < mixins.length; index++) {
copyProperties(this, new mixins[index](options));
}
}
}
for (var index = 0; index < mixins.length; index++) {
var mixin = mixins[index];
copyProperties(Mix, mixin);
copyProperties(Mix.prototype, mixin.prototype);
copyProperties(Mix.prototype, new mixin());
}
return Mix;
function copyProperties(target, source) {
var ownKeys = Object.getOwnPropertyNames(source);
if (Object.getOwnPropertySymbols) {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source));
}
for (var index = 0; index < ownKeys.length; index++) {
var key = ownKeys[index];
if (key !== "constructor"
&& key !== "prototype"
&& key !== "name" && key !== "length") {
let desc = Object.getOwnPropertyDescriptor(source, key);
if (window["ActiveXObject"]) {
Object.defineProperty(target, key, desc || {});
} else {
Object.defineProperty(target, key, desc);
}
}
}
}
};
/**
* @name String
* @namespace
* @memberOf SuperMap
* @category BaseTypes Util
* @description 字符串操作的一系列常用扩展函数。
*/
export var StringExt = SuperMap.String = {
/**
* @function SuperMap.String.startsWith
* @description 判断目标字符串是否以指定的子字符串开头。
* @param {string} str - 目标字符串。
* @param {string} sub - 查找的子字符串。
* @returns {boolean} 目标字符串以指定的子字符串开头,则返回 true;否则返回 false。
*/
startsWith: function (str, sub) {
return (str.indexOf(sub) == 0);
},
/**
* @function SuperMap.String.contains
* @description 判断目标字符串是否包含指定的子字符串。
* @param {string} str - 目标字符串。
* @param {string} sub - 查找的子字符串。
* @returns {boolean} 目标字符串中包含指定的子字符串,则返回 true;否则返回 false。
*/
contains: function (str, sub) {
return (str.indexOf(sub) != -1);
},
/**
* @function SuperMap.String.trim
* @description 删除一个字符串的开头和结尾处的所有空白字符。
* @param {string} str - (可能)存在空白字符填塞的字符串。
* @returns {string} 删除开头和结尾处空白字符后的字符串。
*/
trim: function (str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
},
/**
* @function SuperMap.String.camelize
* @description 骆驼式("-")连字符的字符串处理。
* 例如:"chicken-head" becomes "chickenHead",
* "-chicken-head" becomes "ChickenHead"。
* @param {string} str - 要处理的字符串,原始内容不应被修改。
* @returns {string}
*/
camelize: function (str) {
var oStringList = str.split('-');
var camelizedString = oStringList[0];
for (var i = 1, len = oStringList.length; i < len; i++) {
var s = oStringList[i];
camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
}
return camelizedString;
},
/**
* @function SuperMap.String.format
* @description 提供带 ${token} 标记的字符串, 返回 context 对象属性中指定标记的属性值。
* @example
* 示例:
* (code)
* 1、template = "${value,getValue}";
* context = {value: {getValue:function(){return Math.max.apply(null,argument);}}};
* args = [2,23,12,36,21];
* 返回值:36
* (end)
* 示例:
* (code)
* 2、template = "$${{value,getValue}}";
* context = {value: {getValue:function(){return Math.max.apply(null,argument);}}};
* args = [2,23,12,36,21];
* 返回值:"${36}"
* (end)
* 示例:
* (code)
* 3、template = "${a,b}";
* context = {a: {b:"format"}};
* args = null;
* 返回值:"format"
* (end)
* 示例:
* (code)
* 3、template = "${a,b}";
* context = null;
* args = null;
* 返回值:"${a.b}"
* (end)
* @param {string} template - 带标记的字符串将要被替换。参数 template 格式为"${token}",此处的 token 标记会替换为 context["token"] 属性的值。
* @param {Object} [context=window] - 带有属性的可选对象的属性用于匹配格式化字符串中的标记。如果该参数为空,将使用 window 对象。
* @param {Array} [args] - 可选参数传递给在 context 对象上找到的函数。
* @returns {string} 从 context 对象属性中替换字符串标记位的字符串。
*/
format: function (template, context, args) {
if (!context) {
context = window;
}
// Example matching:
// str = ${foo.bar}
// match = foo.bar
var replacer = function (str, match) {
var replacement;
// Loop through all subs. Example: ${a.b.c}
// 0 -> replacement = context[a];
// 1 -> replacement = context[a][b];
// 2 -> replacement = context[a][b][c];
var subs = match.split(/\.+/);
for (var i = 0; i < subs.length; i++) {
if (i == 0) {
replacement = context;
}
replacement = replacement[subs[i]];
}
if (typeof replacement === "function") {
replacement = args ?
replacement.apply(null, args) :
replacement();
}
// If replacement is undefined, return the string 'undefined'.
// This is a workaround for a bugs in browsers not properly
// dealing with non-participating groups in regular expressions:
// http://blog.stevenlevithan.com/archives/npcg-javascript
if (typeof replacement == 'undefined') {
return 'undefined';
} else {
return replacement;
}
};
return template.replace(SuperMap.String.tokenRegEx, replacer);
},
/**
* @member {RegExp} [SuperMap.String.tokenRegEx]
* @description 寻找带 token 的字符串,默认为 tokenRegEx=/\$\{([\w.]+?)\}/g。
* @example
* Examples: ${a}, ${a.b.c}, ${a-b}, ${5}
*/
tokenRegEx: /\$\{([\w.]+?)\}/g,
/**
* @member {RegExp} [SuperMap.String.numberRegEx]
* @description 判断一个字符串是否只包含一个数值,默认为 numberRegEx=/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/。
*/
numberRegEx: /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/,
/**
* @function SuperMap.String.isNumeric
* @description 判断一个字符串是否只包含一个数值。
* @example
* (code)
* SuperMap.String.isNumeric("6.02e23") // true
* SuperMap.String.isNumeric("12 dozen") // false
* SuperMap.String.isNumeric("4") // true
* SuperMap.String.isNumeric(" 4 ") // false
* (end)
* @returns {boolean} 字符串包含唯一的数值,返回 true;否则返回 false。
*/
isNumeric: function (value) {
return SuperMap.String.numberRegEx.test(value);
},
/**
* @function SuperMap.String.numericIf
* @description 把一个看似数值型的字符串转化为一个数值。
* @returns {(number|string)} 如果能转换为数值则返回数值,否则返回字符串本身。
*/
numericIf: function (value) {
return SuperMap.String.isNumeric(value) ? parseFloat(value) : value;
}
};
/**
* @name Number
* @memberOf SuperMap
* @namespace
* @category BaseTypes Util
* @description 数值操作的一系列常用扩展函数。
*/
export var NumberExt = SuperMap.Number = {
/**
* @member {string} [SuperMap.Number.decimalSeparator='.']
* @description 格式化数字时默认的小数点分隔符。
* @constant
*/
decimalSeparator: ".",
/**
* @member {string} [SuperMap.Number.thousandsSeparator=',']
* @description 格式化数字时默认的千位分隔符。
* @constant
*/
thousandsSeparator: ",",
/**
* @function SuperMap.Number.limitSigDigs
* @description 限制浮点数的有效数字位数。
* @param {number} num - 浮点数。
* @param {integer} sig - 有效位数。
* @returns {number} 将数字四舍五入到指定数量的有效位数。
*/
limitSigDigs: function (num, sig) {
var fig = 0;
if (sig > 0) {
fig = parseFloat(num.toPrecision(sig));
}
return fig;
},
/**
* @function SuperMap.Number.format
* @description 数字格式化输出。
* @param {number} num - 数字。
* @param {integer} [dec=0] - 数字的小数部分四舍五入到指定的位数。设置为 null 值时小数部分不变。
* @param {string} [tsep=','] - 千位分隔符。
* @param {string} [dsep='.'] - 小数点分隔符。
* @returns {string} 数字格式化后的字符串。
*/
format: function (num, dec, tsep, dsep) {
dec = (typeof dec != "undefined") ? dec : 0;
tsep = (typeof tsep != "undefined") ? tsep :
SuperMap.Number.thousandsSeparator;
dsep = (typeof dsep != "undefined") ? dsep :
SuperMap.Number.decimalSeparator;
if (dec != null) {
num = parseFloat(num.toFixed(dec));
}
var parts = num.toString().split(".");
if (parts.length === 1 && dec == null) {
// integer where we do not want to touch the decimals
dec = 0;
}
var integer = parts[0];
if (tsep) {
var thousands = /(-?[0-9]+)([0-9]{3})/;
while (thousands.test(integer)) {
integer = integer.replace(thousands, "$1" + tsep + "$2");
}
}
var str;
if (dec == 0) {
str = integer;
} else {
var rem = parts.length > 1 ? parts[1] : "0";
if (dec != null) {
rem = rem + new Array(dec - rem.length + 1).join("0");
}
str = integer + dsep + rem;
}
return str;
}
};
if (!Number.prototype.limitSigDigs) {
/**
* APIMethod: Number.limitSigDigs
* 限制浮点数的有效数字位数.
* @param {integer} sig -有效位数。
* @returns {integer} 将数字四舍五入到指定数量的有效位数。
* 如果传入值 为 null、0、或者是负数, 返回值 0。
*/
Number.prototype.limitSigDigs = function (sig) {
return NumberExt.limitSigDigs(this, sig);
};
}
/**
* @name Function
* @memberOf SuperMap
* @namespace
* @category BaseTypes Util
* @description 函数操作的一系列常用扩展函数。
*/
export var FunctionExt = SuperMap.Function = {
/**
* @function SuperMap.Function.bind
* @description 绑定函数到对象。方便创建 this 的作用域。
* @param {function} func - 输入函数。
* @param {Object} object - 对象绑定到输入函数(作为输入函数的 this 对象)。
* @returns {function} object 参数作为 func 函数的 this 对象。
*/
bind: function (func, object) {
// create a reference to all arguments past the second one
var args = Array.prototype.slice.apply(arguments, [2]);
return function () {
// Push on any additional arguments from the actual function call.
// These will come after those sent to the bind call.
var newArgs = args.concat(
Array.prototype.slice.apply(arguments, [0])
);
return func.apply(object, newArgs);
};
},
/**
* @function SuperMap.Function.bindAsEventListener
* @description 绑定函数到对象,在调用该函数时配置并使用事件对象作为第一个参数。
* @param {function} func - 用于监听事件的函数。
* @param {Object} object - this 对象的引用。
* @returns {function}
*/
bindAsEventListener: function (func, object) {
return function (event) {
return func.call(object, event || window.event);
};
},
/**
* @function SuperMap.Function.False
* @description 该函数仅仅返回 false。该函数主要是避免在 IE8 以下浏览中 DOM 事件句柄的匿名函数问题。
* @example
* document.onclick = SuperMap.Function.False;
* @returns {boolean}
*/
False: function () {
return false;
},
/**
* @function SuperMap.Function.True
* @description 该函数仅仅返回 true。该函数主要是避免在 IE8 以下浏览中 DOM 事件句柄的匿名函数问题。
* @example
* document.onclick = SuperMap.Function.True;
* @returns {boolean}
*/
True: function () {
return true;
},
/**
* @function SuperMap.Function.Void
* @description 可重用函数,仅仅返回 "undefined"。
* @returns {undefined}
*/
Void: function () {
}
};
/**
* @name Array
* @memberOf SuperMap
* @namespace
* @category BaseTypes Util
* @description 数组操作的一系列常用扩展函数。
*/
export var ArrayExt = SuperMap.Array = {
/**
* @function SuperMap.Array.filter
* @description 过滤数组,提供了 ECMA-262 标准中 Array.prototype.filter 函数的扩展。详见:{@link http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter}
* @param {Array} array - 要过滤的数组。
* @param {function} callback - 数组中的每一个元素调用该函数。</br>
* 如果函数的返回值为 true,该元素将包含在返回的数组中。该函数有三个参数: 数组中的元素,元素的索引,数组自身。</br>
* 如果设置了可选参数 caller,在调用 callback 时,使用可选参数 caller 设置为 callback 的参数。</br>
* @param {Object} [caller] - 在调用 callback 时,使用参数 caller 设置为 callback 的参数。
* @returns {Array} callback 函数返回 true 时的元素将作为返回数组中的元素。
*/
filter: function (array, callback, caller) {
var selected = [];
if (Array.prototype.filter) {
selected = array.filter(callback, caller);
} else {
var len = array.length;
if (typeof callback != "function") {
throw new TypeError();
}
for (var i = 0; i < len; i++) {
if (i in array) {
var val = array[i];
if (callback.call(caller, val, i, array)) {
selected.push(val);
}
}
}
}
return selected;
}
};