This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tatami.coffee
3242 lines (2735 loc) · 86 KB
/
tatami.coffee
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict"
__util = do ->
"use strict"
# Config of library
LIB_CONFIG =
name: "Ronin"
version: "0.3.1"
__proc = do ( window ) ->
# Save a reference to some core methods.
toString = {}.toString
# default settings
settings =
validator: ->
return true
# storage for internal usage
storage =
# map of object types
types: {}
###
# Fill the map object-types, and add methods to detect object-type.
#
# @private
# @method objectTypes
# @return {Object}
###
objectTypes = ->
types = "Boolean Number String Function Array Date RegExp Object".split " "
for type in types
do ( type ) ->
# populate the storage.types map
storage.types["[object #{type}]"] = lc = type.toLowerCase()
if type is "Number"
handler = ( target ) ->
return if isNaN(target) then false else @type(target) is lc
else
handler = ( target ) ->
return @type(target) is lc
# add methods such as isNumber/isBoolean/...
storage.methods["is#{type}"] = handler
return storage.types
###
# 判断某个对象是否有自己的指定属性
#
# !!! 不能用 object.hasOwnProperty(prop) 这种方式,低版本 IE 不支持。
#
# @private
# @method hasOwnProp
# @param obj {Object} Target object
# @param prop {String} Property to be tested
# @return {Boolean}
###
hasOwnProp = ( obj, prop ) ->
return if not obj? then false else Object::hasOwnProperty.call obj, prop
###
# 为指定 object 或 function 定义属性
#
# @private
# @method defineProp
# @param target {Object}
# @return {Boolean}
###
defineProp = ( target ) ->
prop = "__#{LIB_CONFIG.name.toLowerCase()}__"
value = true
# throw an exception in IE9-
try
Object.defineProperty target, prop,
__proto__: null
value: value
catch error
target[prop] = value
return true
###
# 批量添加 method
#
# @private
# @method batch
# @param handlers {Object} data of a method
# @param data {Object} data of a module
# @param host {Object} the host of methods to be added
# @return
###
batch = ( handlers, data, host ) ->
methods = storage.methods
if methods.isArray(data) or (methods.isPlainObject(data) and not methods.isArray(data.handlers))
methods.each data, ( d ) ->
batch d?.handlers, d, host
else if methods.isPlainObject(data) and methods.isArray(data.handlers)
methods.each handlers, ( info ) ->
attach info, data, host
return host
###
# 构造 method
#
# @private
# @method attach
# @param set {Object} data of a method
# @param data {Object} data of a module
# @param host {Object} the host of methods to be added
# @return
###
attach = ( set, data, host ) ->
name = set.name
methods = storage.methods
if set.expose isnt false and not methods.isFunction host[name]
handler = set.handler
value = if hasOwnProp(set, "value") then set.value else data.value
validators = [
set.validator
data.validator
settings.validator
->
return true
]
break for validator in validators when methods.isFunction validator
method = ->
return if methods.isFunction(handler) and validator.apply(host, arguments) is true then handler.apply(host, arguments) else value;
host[name] = method
return host
storage.methods =
# ====================
# Core methods
# ====================
###
# 扩展指定对象
#
# @method mixin
# @param unspecified {Mixed}
# @return {Object}
###
mixin: ->
args = arguments
length = args.length
target = args[0] or {}
i = 1
deep = false
# Handle a deep copy situation
if @type(target) is "boolean"
deep = target
target = args[1] or {}
# skip the boolean and the target
i = 2
# Handle case when target is a string or something (possible in deep copy)
target = {} if typeof target isnt "object" and not @isFunction target
# 只传一个参数时,扩展自身
if length is 1
target = this
i--
while i < length
opts = args[i]
# Only deal with non-null/undefined values
if opts?
for name, copy of opts
src = target[name]
# 阻止无限循环
if copy is target
continue
# Recurse if we're merging plain objects or arrays
if deep and copy and (@isPlainObject(copy) or (copyIsArray = @isArray(copy)))
if copyIsArray
copyIsArray = false
clone = if src and @isArray(src) then src else []
else
clone = if src and @isPlainObject(src) then src else {}
# Never move original objects, clone them
target[name] = @mixin deep, clone, copy
# Don't bring in undefined values
else if copy isnt undefined
target[name] = copy
i++
return target
###
# 遍历
#
# @method each
# @param object {Object/Array/Array-Like/Function/String}
# @param callback {Function}
# @return {Mixed}
###
each: ( object, callback ) ->
if @isArray(object) or @isArrayLike(object) or @isString(object)
index = 0
while index < object.length
ele = if @isString(object) then object.charAt(index) else object[index]
break if callback.apply(ele, [ele, index++, object]) is false
else if @isObject(object) or @isFunction(object)
break for name, value of object when callback.apply(value, [value, name, object]) is false
return object
###
# 获取对象类型
#
# @method type
# @param object {Mixed}
# @return {String}
###
type: ( object ) ->
if arguments.length is 0
result = null
else
result = if not object? then String(object) else storage.types[toString.call(object)] || "object"
return result
###
# 切割 Array-Like Object 片段
#
# @method slice
# @param target {Array-Like}
# @param begin {Integer}
# @param end {Integer}
# @return
###
slice: ( target, begin, end ) ->
if not target?
result = []
else
end = Number end
args = [(Number(begin) || 0)]
args.push(end) if arguments.length > 2 and not isNaN(end)
result = [].slice.apply target, args
return result
###
# 判断某个对象是否有自己的指定属性
#
# @method hasProp
# @param prop {String} Property to be tested
# @param obj {Object} Target object
# @return {Boolean}
###
hasProp: ( prop, obj ) ->
return hasOwnProp.apply this, [(if arguments.length < 2 then this else obj), prop]
# ====================
# Extension of detecting type of variables
# ====================
###
# 判断是否为 window 对象
#
# @method isWindow
# @param object {Mixed}
# @return {Boolean}
###
isWindow: ( object ) ->
return object and @isObject(object) and "setInterval" of object
###
# 判断是否为 DOM 对象
#
# @method isElement
# @param object {Mixed}
# @return {Boolean}
###
isElement: ( object ) ->
return object and @isObject(object) and object.nodeType is 1
###
# 判断是否为数字类型(字符串)
#
# @method isNumeric
# @param object {Mixed}
# @return {Boolean}
###
isNumeric: ( object ) ->
return not @isArray(object) and not isNaN(parseFloat(object)) and isFinite(object)
###
# Determine whether a number is an integer.
#
# @method isInteger
# @param object {Mixed}
# @return {Boolean}
###
isInteger: ( object ) ->
return @isNumeric(object) and /^-?[1-9]\d*$/.test(object)
###
# 判断对象是否为纯粹的对象(由 {} 或 new Object 创建)
#
# @method isPlainObject
# @param object {Mixed}
# @return {Boolean}
###
isPlainObject: ( object ) ->
# This is a copy of jQuery 1.7.1.
# Must be an Object.
# Because of IE, we also have to check the presence of the constructor property.
# Make sure that DOM nodes and window objects don't pass through, as well
if not object or not @isObject(object) or object.nodeType or @isWindow(object)
return false
try
# Not own constructor property must be Object
if object.constructor and not @hasProp("constructor", object) and not @hasProp("isPrototypeOf", object.constructor.prototype)
return false
catch error
# IE8,9 will throw exceptions on certain host objects
return false
key for key of object
return key is undefined or @hasProp(key, object)
###
# Determin whether a variable is considered to be empty.
#
# A variable is considered empty if its value is or like:
# - null
# - undefined
# - ""
# - []
# - {}
#
# @method isEmpty
# @param object {Mixed}
# @return {Boolean}
#
# refer: http://www.php.net/manual/en/function.empty.php
###
isEmpty: ( object ) ->
result = false
# null, undefined and ""
if not object? or object is ""
result = true
# array and array-like object
else if (@isArray(object) or @isArrayLike(object)) and object.length is 0
result = true
# plain object
else if @isObject(object)
result = true
for name of object
result = false
break
return result
###
# 是否为类数组对象
#
# 类数组对象(Array-Like Object)是指具备以下特征的对象:
# -
# 1. 不是数组(Array)
# 2. 有自动增长的 length 属性
# 3. 以从 0 开始的数字做属性名
#
# @method isArrayLike
# @param object {Mixed}
# @return {Boolean}
###
isArrayLike: ( object ) ->
result = false
if @isObject(object) and not @isWindow object
length = object.length
result = true if object.nodeType is 1 and length or
not @isArray(object) and
not @isFunction(object) and
(length is 0 or @isNumber(length) and length > 0 and (length - 1) of object)
return result
objectTypes()
__proc = ( data, host ) ->
return batch data?.handlers, data, host ? {}
storage.methods.each storage.methods, ( handler, name )->
# defineProp handler
__proc[name] = handler
return __proc
# Save a reference to some core methods.
toString = {}.toString
# Regular expressions
NAMESPACE_EXP = /^[0-9A-Z_.]+[^_.]?$/i
# storage for internal usage
storage =
regexps:
date:
iso8601: /// ^
(\d{4})\-(\d{2})\-(\d{2}) # date
(?:
T(\d{2})\:(\d{2})\:(\d{2})(?:(?:\.)(\d{3}))? # time
(Z|[+-]\d{2}\:\d{2})? # timezone
)?
$ ///
object:
array: /\[(.*)\]/
number: /(-?[0-9]+)/
modules:
Core: {}
# ====================
# Built-in methods from Miso
# ====================
storage.modules.Core.BuiltIn =
# handlers: (name: name, handler: func for name, func of Miso when func.__miso__ is true)
handlers: (name: name, handler: func for name, func of __proc)
###
# Compare objects' values or references.
#
# @private
# @method compareObjects
# @param base {Array/Object}
# @param target {Array/Object}
# @param strict {Boolean}
# @param connate {Boolean}
# @return {Boolean}
###
compareObjects = ( base, target, strict, connate ) ->
result = false
plain = @isPlainObject base
if (plain or connate) and strict
result = target is base
else
if plain
isRun = compareObjects.apply this, [@keys(base), @keys(target), false, true]
else
isRun = target.length is base.length
if isRun
@each base, ( n, i ) =>
type = @type n
t = target[i]
# 有包装对象的原始类型
if @inArray type, ["string", "number", "boolean"] > -1
n_str = n + ""
t_str = t + ""
t_type = @type t
illegalNums = ["NaN", "Infinity", "-Infinity"]
if type is "number" and (@inArray(n_str, illegalNums) > -1 or @inArray(t_str, illegalNums) > -1)
return result = false
else
return result = if strict is true then t is n else t_str is n_str
# 无包装对象的原始类型
else if @inArray(type, ["null", "undefined"]) > -1
return result = t is n
else if @inArray(type, ["date", "regexp", "function"]) > -1
return result = if strict then t is n else t.toString() is n.toString()
else if @inArray(type, ["array", "object"]) > -1
return result = compareObjects.apply this, [n, t, strict, connate]
return result
###
# 将 Array、Object 转化为字符串
#
# @private
# @method stringifyCollection
# @param collection {Array/Plain Object}
# @return {String}
###
stringifyCollection = ( collection ) ->
if @isArray collection
stack = (@stringify ele for ele in collection)
else
stack = ("\"#{key}\":#{@stringify val}" for key, val of collection)
return stack.join ","
storage.modules.Core.Global =
handlers: [
{
###
# 扩充对象
#
# @method extend
# @param data {Plain Object/Array}
# @param host {Object}
# @return {Object}
###
name: "extend"
handler: ( data, host ) ->
return __proc data, host ? this
},
{
###
# 别名
#
# @method alias
# @param name {String}
# @return
###
name: "alias"
handler: ( name ) ->
# 通过 _alias 判断是否已经设置过别名
# 设置别名时要将原来的别名释放
# 如果设置别名了,是否将原名所占的空间清除?(需要征求别人意见)
if @isString name
window[name] = this if window[name] is undefined
return window[String(name)]
},
{
###
# 更改 LIB_CONFIG.name
#
# @method mask
# @param guise {String} New name for library
# @return {Boolean}
###
name: "mask"
handler: ( guise ) ->
if @hasProp guise, window
console.error "'#{guise}' has existed as a property of Window object." if window.console
else
lib_name = @__meta__.name
window[guise] = window[lib_name]
# IE9- 不能用 delete 关键字删除 window 的属性
try
result = delete window[lib_name]
catch error
window[lib_name] = undefined
result = true
@__meta__.name = guise
return result
validator: ( guise ) ->
return @isString guise
value: false
},
{
###
# Returns the namespace specified and creates it if it doesn't exist.
# Be careful when naming packages.
# Reserved words may work in some browsers and not others.
#
# @method namespace
# @param [hostObj] {Object} Host object namespace will be added to
# @param [ns_str_1] {String} The first namespace string
# @param [ns_str_2] {String} The second namespace string
# @param [ns_str_*] {String} Numerous namespace string
# @param [isGlobal] {Boolean} Whether set window as the host object
# @return {Object} A reference to the last namespace object created
###
name: "namespace"
handler: ->
args = arguments
ns = {}
hostObj = args[0]
# Determine the host object.
(hostObj = if args[args.length - 1] is true then window else this) if not @isPlainObject hostObj
@each args, ( arg ) =>
if @isString(arg) and /^[0-9A-Z_.]+[^_.]?$/i.test(arg)
obj = hostObj
@each arg.split("."), ( part, idx, parts ) =>
if not obj?
return false
if not @hasProp part, obj
obj[part] = if idx is parts.length - 1 then null else {}
obj = obj[part]
return true
ns = obj
return true
return ns
},
{
###
# Compares two objects for equality.
#
# @method equal
# @param base {Mixed}
# @param target {Mixed}
# @param strict {Boolean} whether compares the two objects' references
# @return {Boolean}
###
name: "equal"
handler: ( base, target, strict ) ->
result = false
baseType = @type base
if @type(target) is baseType
plain_b = @isPlainObject base
if plain_b and @isPlainObject(target) or baseType isnt "object"
# 是否为“天然”的数组(以别于后来将字符串等转换成的数组)
connate = @isArray base
if not plain_b and not connate
base = [base]
target = [target]
# If 'strict' is true, then compare the objects' references, else only compare their values.
strict = false if not @isBoolean strict
result = compareObjects.apply(this, [base, target, strict, connate])
return result
validator: ->
return arguments.length > 1
value: false
},
{
###
# Returns a random integer between min and max, inclusive.
# If you only pass one argument, it will return a number between 0 and that number.
#
# @method random
# @param min {Number}
# @param max {Number}
# @return {Number}
###
name: "random"
handler: ( min, max ) ->
if not max?
max = min
min = 0
return min + Math.floor Math.random() * (max - min + 1)
},
{
###
# 字符串化
#
# @method stringify
# @param target {Variant}
# @return {String}
###
name: "stringify"
handler: ( target ) ->
switch @type target
when "object"
result = if @isPlainObject(target) then "{#{stringifyCollection.call this, target}}" else result = ""
when "array"
result = "[#{stringifyCollection.call this, target}]"
when "function", "date", "regexp"
result = target.toString()
when "string"
result = "\"#{target}\""
else
try
result = String target
catch e
result = ""
return result
}
# ,
# {
# name: "parse"
# handler: ( target ) ->
# target = @trim target
# result = target
# @each storage.regexps.object, ( r, o ) =>
# re_t = new RegExp "^#{r.source}$"
# if re_t.test target
# switch o
# when "array"
# re_g = new RegExp "#{r.source}", "g"
# re_c = /(\[.*\])/
# r = re_g.exec target
# result = []
# while r?
# @each r[1].split(","), ( unit, idx ) =>
# result.push @parse unit
# break;
# when "number"
# result *= 1
# return false
# else
# return true
# return result
# validator: ( target ) ->
# return @isString target
# value: ""
# }
]
# /**
# * 恢复原名
# *
# * @method revert
# * @return
# */
# // "revert": function() {},
# /**
# * 新建对象子集
# *
# * @method create
# * @param namespace {String} Name of sub-collection
# * @param object {Object} Object added to the main object
# * @return
# */
# /*create: function( namespace, object ) {
# this.namespace = object;
# },*/
# /**
# * 打印信息到控制台
# */
# // "log": function() {}
storage.modules.Core.Object =
handlers: [
{
###
# Get a set of keys/indexes.
# It will return a key or an index when pass the 'value' parameter.
#
# @method keys
# @param object {Object/Function} 被操作的目标
# @param value {Mixed} 指定值
# @return {Array/String}
#
# refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
###
name: "keys"
handler: ( object, value ) ->
keys = []
@each object, ( v, k ) ->
if v is value
keys = k
return false
else
keys.push k
return if @isArray(keys) then keys.sort() else keys
validator: ( object ) ->
return object isnt null and not (object instanceof Array) and typeof object in ["object", "function"]
value: []
}
]
# ====================
# Array
# ====================
###
# Determine whether an object is an array.
#
# @private
# @method isCollection
# @param target {Array/Object}
# @return {Boolean}
###
isArr = ( object ) ->
return object instanceof Array
###
# Determine whether an object is an array or a plain object.
#
# @private
# @method isCollection
# @param target {Array/Object}
# @return {Boolean}
###
isCollection = ( target ) ->
return @isArray(target) or @isPlainObject(target)
###
# Return the maximum (or the minimum) element (or element-based computation).
# Can't optimize arrays of integers longer than 65,535 elements.
# See [WebKit Bug 80797](https://bugs.webkit.org/show_bug.cgi?id=80797)
#
# @private
# @method getMaxMin
# @param initialValue {Number} Default return value of function
# @param funcName {String} Method's name of Math object
# @param collection {Array/Object} A collection to be manipulated
# @param callback {Function} Callback for every element of the collection
# @param [context] {Mixed} Context of the callback
# @return {Number}
###
getMaxMin = ( initialValue, funcName, collection, callback, context ) ->
result = value: initialValue, computed: initialValue
if isCollection.call this, collection
existCallback = @isFunction callback
if not existCallback and @isArray(collection) and collection[0] is +collection[0] and collection.length < 65535
return Math[funcName].apply Math, collection
@each collection, ( val, idx, list ) ->
computed = if existCallback then callback.apply(context, [val, idx, list]) else val
result = value: val, computed: computed if funcName is "max" and computed > result.computed or funcName is "min" and computed < result.computed
return result.value
###
# A internal usage to flatten a nested array.
#
# @private
# @method flattenArray
# @param array {Array}
# @return {Mixed}
###
flattenArray = ( array ) ->
lib = this
arr = []
if lib.isArray array
lib.each array, ( n, i ) ->
arr = arr.concat flattenArray.call lib, n
else
arr = array
return arr
###
# 获取小数点后面的位数
#
# @private
# @method floatLength
# @param number {Number}
# @return {Integer}
###
floatLength = ( number ) ->
rfloat = /^([-+]?\d+)\.(\d+)$/
return (if rfloat.test(number) then (number + "").match(rfloat)[2] else "").length
###
# Create an array contains specified range.
#
# @private
# @method range
# @param from {Number/String}
# @param to {Number/String}
# @param step {Number}
# @param callback {Function}
# @return {Array}
###
range = ( begin, end, step, callback ) ->
array = []
while begin <= end
array.push if callback then callback(begin) else begin
begin += step
return array
###
# Filter elements in a set.
#
# @private
# @method filterElement
# @param target {Array/Object/String} operated object
# @param callback {Function} callback to change unit's value
# @param context {Mixed} context of callback
# @param method {Function} Array's prototype method
# @param func {Function} callback for internal usage
# @return {Array/Object/String} 与被过滤的目标相同类型
###
filterElement = ( target, callback, context, method, func ) ->
result = null
lib = this
if lib.isFunction callback
arrOrStr = lib.type(target) in ["array", "string"]
# default context is the window object
context = window if not context?
# use Array's prototype method
if lib.isFunction(method) && arrOrStr
result = method.apply target, [callback, context]
else
plainObj = lib.isPlainObject target
if plainObj
result = {}
else if arrOrStr
result = []
if result isnt null
lib.each target, ( ele, idx ) ->
cbVal = callback.apply context, [ele, idx, if lib.isString(target) then new String(target) else target]
func result, cbVal, ele, idx, plainObj, arrOrStr
return true
result = result.join("") if lib.isString target
return result
storage.modules.Core.Array =
value: []
validator: ( object ) ->
return isArr object
handlers: [
{
###
# 元素在数组中的位置
#
# @method inArray
# @param element {Mixed} 待查找的数组元素
# @param array {Array} 数组
# @param from {Integer} 起始索引
# @return {Integer}
###
name: "inArray"
handler: ( element, array, from ) ->
index = -1
indexOf = Array::indexOf
length = array.length
from = if from then (if from < 0 then Math.max(0, length + from) else from) else 0
if indexOf
index = indexOf.apply array, [element, from]
else
while from < length
if from of array and array[from] is element