-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslang.js
15853 lines (13837 loc) · 450 KB
/
eslang.js
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
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./web/lib/app.js");
/******/ })
/************************************************************************/
/******/ ({
/***/ "./es/compiler.js":
/*!************************!*\
!*** ./es/compiler.js ***!
\************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
module.exports = function compilerIn ($void) {
var $ = $void.$
var Tuple$ = $void.Tuple
var warn = $void.$warn
var $export = $void.export
var tokenizer = $.tokenizer
var isApplicable = $void.isApplicable
var formatPattern = $void.formatPattern
var sharedSymbolOf = $void.sharedSymbolOf
var symbolPairing = $.symbol.pairing
var symbolSubject = $.symbol.subject
var symbolString = sharedSymbolOf('string')
var symbolFormat = sharedSymbolOf('format')
var symbolToString = sharedSymbolOf('to-string')
var makeSourceUri = function (uri, version) {
return !uri || typeof uri !== 'string' ? ''
: !version || typeof version !== 'string' ? uri
: uri + '@' + version
}
var compiler = $export($, 'compiler', function (evaluate, srcUri) {
if (!isApplicable(evaluate)) {
return $.compile
}
var srcText = ''
if (!srcUri || typeof srcUri !== 'string') {
srcUri = ''
}
var stack, sourceStack, waiter, lastToken, openingLine, openingOffset
resetContext()
function resetContext () {
stack = [[]]
sourceStack = [[[[0, 0, 0]]]]
waiter = null
lastToken = ['space', '', [0, 0, 0]]
openingLine = -1
openingOffset = 0
}
var tokenizing = tokenizer(compileToken, srcUri)
return function compiling (text) {
srcText = text && typeof text === 'string' ? text : ''
if (tokenizing(text)) {
return stack.length
}
// reset compiling context.
waiter && waiter()
if (stack.length > 1) {
warn('compiler', 'open statements are not properly closed.',
[lastToken, srcUri || srcText])
endAll(null, lastToken[2])
}
tryToRaise()
resetContext()
return 0
}
function compileToken (type, value, source) {
var endingLine = source[source.length - 2]
if (endingLine !== openingLine) {
openingLine = endingLine
openingOffset = stack[stack.length - 1].length
}
if (!waiter || !waiter(type, value, source)) {
parseToken(type, value, source)
}
lastToken = [type, value, source]
}
function parseToken (type, value, source) {
switch (type) {
case 'value':
pushValue(value, source)
break
case 'symbol':
pushSymbol(value, source)
break
case 'punctuation':
pushPunctuation(value, source)
break
case 'format':
pushFormat(value, source)
break
case 'space':
if (value === '\n') {
tryToRaise()
}
break
case 'comment':
// comment document should be put in specs.
break
default:
// do nothing for a free space.
break
}
}
function tryToRaise () {
while (stack[0].length > 0) {
evaluate([stack[0].shift(), sourceStack[0].splice(0, 1)])
}
}
function pushValue (value, source) {
stack[stack.length - 1].push(value)
sourceStack[sourceStack.length - 1].push(source)
}
function pushSymbol (value, source) {
switch (value.key) {
case ',':
// a free comma functions only as a stronger visual indicator like
// a whitespace, so it will be just skipped in building AST.
if (lastToken[0] === 'symbol' && lastToken[1].key === ',') {
pushValue(null, source)
}
break
case ';':
endLine(value, source)
if (!crossingLines()) {
closeLine(value, source)
}
break
default:
pushValue(value, source)
}
}
function pushPunctuation (value, source) {
switch (value) {
case '(': // begin a new clause
stack.push([])
sourceStack.push([[source]])
break
case ')':
// wait for next token to decide
waiter = endingWaiter
break
default: // just skip unknown punctuation as some placeholders.
break
}
}
function pushFormat (pattern, source) {
var args = formatPattern(pattern)
if (!(args.length > 1)) {
if (pattern.indexOf('"') < 0) {
warn('compiler', 'unnecessary format string.',
pattern, ['format', pattern, source, srcUri || srcText])
}
return pushValue(args[0], source)
}
var beginning = source.slice(0, 3).concat(source.slice(1, 2))
var ending = source.slice(0, 1).concat(source.slice(-2))
stack.push([symbolString, symbolFormat])
sourceStack.push([[beginning], beginning, beginning])
pushValue(args[0], source)
for (var i = 1; i < args.length; i++) {
var code = $.compile(args[i])
pushValue(code.$.length > 0 ? code.$[0] : null, ending)
}
endTopWith(ending)
}
function endingWaiter (type, value, source) {
waiter = null // wait only once.
if (type !== 'symbol') {
endClause()
return false // stop waiting
}
switch (value.key) {
case '.':
if (stack.length > 1) {
endMatched(value, source)
} else {
warn('compiler', 'extra enclosing ")." is found and ignored.',
[lastToken, ['symbol', value, source], srcUri || srcText])
}
return true
default:
endClause()
return false
}
}
function endTopWith (source) {
// create a tuple for the top clause, and
var statement = stack.pop()
// append ending token(s)' source info.
var sourceMap = sourceStack.pop()
sourceMap[0].push(source || lastToken[2])
while (statement.length > 2 &&
tryToFoldStatement(statement, sourceMap)
);
// push it to the end of container clause.
sourceMap[0].unshift(srcUri || srcText)
stack[stack.length - 1].push(new Tuple$(statement, false, sourceMap))
// since the source has been saved into the tuple, only keeps its overall range.
sourceStack[sourceStack.length - 1].push(sourceMap[0].slice(1))
}
function tryToFoldStatement (statement, sourceMap) { // sweeter time.
var max = statement.length - 1
for (var i = 1; i < max; i++) {
if (statement[i] === symbolPairing && statement[i + 1] === symbolPairing) {
statement.splice(i, 2)
sourceMap.splice(i + 1, 2)
foldStatement(statement, sourceMap, i)
return true
}
}
return false
}
function foldStatement (statement, sourceMap, length) {
// (x :: y) => ($(x) y)
var expr = statement.splice(0, length)
// re-arrange source map
var exprSrcMap = sourceMap.splice(1, length + 1)
var beginning = exprSrcMap[0].slice(0, 3)
var ending = exprSrcMap[exprSrcMap.length - 1]
exprSrcMap.unshift(beginning.concat(ending.slice(-2)))
// (x ::) => ($(x) to-string)
if (statement.length < 1) {
statement.push(symbolToString)
sourceMap.push(ending.slice(0, 1).concat(ending.slice(-2)))
}
exprSrcMap[0].unshift(srcUri || srcText)
statement.unshift(symbolSubject, new Tuple$(expr, false, exprSrcMap))
sourceMap.splice(1, 0,
beginning.concat(beginning.slice(1)), exprSrcMap[0].slice(1)
)
}
function endClause () {
if (stack.length < 2) {
warn('compiler', 'extra enclosing parentheses is found and ignored.',
[lastToken, srcUri || srcText])
return // allow & ignore extra enclosing parentheses
}
endTopWith()
}
function endMatched (value, source) {
if (stack.length < 2) {
warn('compiler', 'extra ")," is found and ignored.',
[lastToken, ['symbol', value, source], srcUri || srcText])
return // allow & ignore extra enclosing parentheses
}
lastToken[2][0] >= 0 // the indent value of ')'
? endIndent(value, source) : endLine(value, source)
}
function endLine (value, source) { // sugar time
var depth = stack.length - 1
while (depth > 0) {
var startSource = sourceStack[depth][0][0] // start source.
if (startSource[1] < source[1]) { // comparing line numbers.
break
}
endTopWith(source)
depth = stack.length - 1
}
}
function crossingLines () {
var depth = sourceStack.length - 1
var srcOffset = openingOffset + 1
var topSource = sourceStack[depth]
return topSource.length > srcOffset &&
openingLine > topSource[srcOffset][1]
}
function closeLine (value, source) { // sweeter time.
var depth = stack.length - 1
stack.push(stack[depth].splice(openingOffset))
var src = sourceStack[depth].splice(openingOffset + 1)
src.length > 0 ? src.unshift(src[0]) : src.push(source)
sourceStack.push(src)
endTopWith(source)
openingOffset = stack[depth].length
}
function endIndent (value, source) { // sugar time
var endingIndent = lastToken[2][0]
var depth = stack.length - 1
while (depth > 0) {
var indent = sourceStack[depth][0][0][0]
// try to looking for and stop with the first matched indent.
if (indent >= 0 && indent <= endingIndent) {
if (indent === endingIndent) {
endTopWith(source)
}
break
}
endTopWith(source)
depth = stack.length - 1
}
}
function endAll (value, source) { // sugar time
while (stack.length > 1) {
endTopWith(source)
}
}
})
// a simple memory cache
var cache = {
code: Object.create(null),
versions: Object.create(null),
get: function (uri, version) {
return !uri || typeof uri !== 'string' ? null
: !version || typeof version !== 'string' ? this.code[uri]
: this.versions[uri] === version ? this.code[uri] : null
},
set: function (code, uri, version) {
if (uri && typeof uri === 'string') {
this.code[uri] = code
if (version && typeof version === 'string') {
this.versions[uri] = version
}
}
return code
}
}
// a helper function to compile a piece of source code.
$export($, 'compile', function (text, uri, version) {
var code = cache.get(uri, version)
if (code) {
return code
}
var srcUri = makeSourceUri(uri || text, version)
var list = []
var src = [[[srcUri, 0, 0, 0]]]
var compiling = compiler(function collector (expr) {
list.push(expr[0])
src.push(expr[1])
}, srcUri)
if (compiling(text) > 1) {
compiling('\n') // end any pending waiter.
}
compiling() // notify the end of stream.
code = new Tuple$(list, true, src)
return cache.set(code, uri, version)
})
}
/***/ }),
/***/ "./es/generic/array.js":
/*!*****************************!*\
!*** ./es/generic/array.js ***!
\*****************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
function offsetOf (length, index) {
index >>= 0
return index >= 0 ? index : index + length
}
function beginOf (length, from) {
from = offsetOf(length, from)
return from < 0 ? 0 : from
}
function endOf (length, to) {
return typeof to === 'undefined' ? length : beginOf(length, to)
}
function checkSpacing (s, i, last) {
switch (i - last) {
case 1: return
case 2: s.push('*'); return
case 3: s.push('*', '*'); return
case 4: s.push('*', '*', '*'); return
default: s.push('...')
}
}
module.exports = function arrayIn ($void) {
var $ = $void.$
var Type = $.array
var $Symbol = $.symbol
var Tuple$ = $void.Tuple
var Symbol$ = $void.Symbol
var link = $void.link
var thisCall = $void.thisCall
var iterateOf = $void.iterateOf
var boolValueOf = $void.boolValueOf
var isApplicable = $void.isApplicable
var protoValueOf = $void.protoValueOf
var EncodingContext$ = $void.EncodingContext
var symbolComma = $Symbol.comma
var symbolLiteral = $Symbol.literal
var symbolPairing = $Symbol.pairing
// create an empty array.
link(Type, 'empty', function () {
return []
}, true)
// create an array of the arguments
link(Type, 'of', function (x, y, z) {
switch (arguments.length) {
case 0: return []
case 1: return [x]
case 2: return [x, y]
case 3: return [x, y, z]
default: return Array.prototype.slice.call(arguments)
}
}, true)
// create an array with items from iterable arguments, or the argument itself
// if its value is not iterable.
var arrayFrom = link(Type, 'from', function () {
var list = []
for (var i = 0; i < arguments.length; i++) {
var source = arguments[i]
if (Array.isArray(source)) {
list = list.concat(source)
} else {
var next = iterateOf(source)
if (!next) {
list.push(source)
} else {
var item = next()
while (typeof item !== 'undefined' && item !== null) {
list.push(Array.isArray(item) ? item.length > 0 ? item[0] : null : item)
item = next()
}
}
}
}
return list
}, true)
var proto = Type.proto
// return the length of this array.
link(proto, 'length', function () {
return this.length
})
// return the amount of elements.
link(proto, ['count', 'for-each'], function (filter) {
var counter = 0
if (isApplicable(filter)) {
this.forEach(function (v, i, arr) {
typeof v !== 'undefined' &&
boolValueOf(filter.call(arr, v, i)) && counter++
})
} else {
this.forEach(function (v) {
typeof v !== 'undefined' && counter++
})
}
return counter
})
// Mutability
link(proto, 'seal', function () {
return Object.freeze(this)
})
link(proto, 'is-sealed', function () {
return Object.isFrozen(this)
})
var stopSignal = new Error('tracing.stopped')
// call a handler for each element until it returns a truthy value.
var trace = link(proto, 'trace', function (tracer) {
if (isApplicable(tracer)) {
try {
this.forEach(function (v, i, arr) {
if (typeof v !== 'undefined' && boolValueOf(tracer.call(arr, v, i))) {
throw stopSignal
}
})
} catch (err) {
if (err !== stopSignal) throw err
}
}
return this
})
// like trace, but to traverse all element from the end.
var retrace = link(proto, 'retrace', function (tracer) {
if (isApplicable(tracer)) {
try {
this.reduceRight(function (_, v, i, s) {
if (typeof v !== 'undefined' && boolValueOf(tracer.call(s, v, i))) {
throw stopSignal
}
}, this)
} catch (err) {
if (err !== stopSignal) throw err
}
}
return this
})
// generate an iterator function to traverse all array items.
link(proto, 'iterate', function (begin, end) {
begin = beginOf(this.length, begin)
end = endOf(this.length, end)
var list = this
var indices = []
trace.call(this, function (_, i) {
return i >= end || (
(i >= begin && indices.push(i)) && false
)
})
var current
begin = 0; end = indices.length
return function next (inSitu) {
if (typeof current !== 'undefined' &&
typeof inSitu !== 'undefined' && boolValueOf(inSitu)) {
return current
}
if (begin >= end) {
return null
}
var index = indices[begin++]
return (current = [list[index], index])
}
})
// to create a shallow copy of this instance with all items,
// or selected items in a range.
link(proto, 'copy', function (begin, count) {
begin = beginOf(this.length, begin)
count = typeof count === 'undefined' ? this.length : count >> 0
if (count < 0) {
count = 0
}
return this.slice(begin, begin + count)
})
link(proto, 'slice', function (begin, end) {
return this.slice(beginOf(this.length, begin), endOf(this.length, end))
})
// create a new array with items in this array and argument values.
link(proto, 'concat', function () {
return this.concat(Array.prototype.slice.call(arguments))
})
// append more items to the end of this array
var appendFrom = link(proto, ['append', '+='], function () {
var isSparse
for (var i = 0; i < arguments.length; i++) {
var src = arguments[i]
src = Array.isArray(src) ? src : arrayFrom(src)
this.push.apply(this, src)
isSparse = isSparse || src.isSparse
}
return this
})
// create a new array with items in this array and argument arrays.
link(proto, ['merge', '+'], function () {
return appendFrom.apply(this.slice(), arguments)
})
// getter by index
var getter = link(proto, 'get', function (index) {
index = offsetOf(this.length, index)
return index >= 0 ? this[index] : null
})
// setter by index
var setter = link(proto, 'set', function (index, value) {
index = offsetOf(this.length, index)
return index < 0 ? null
: (this[index] = typeof value === 'undefined' ? null : value)
})
// reset one or more entries by indices
link(proto, 'reset', function (index) {
var length = this.length
for (var i = 0; i < arguments.length; i++) {
index = offsetOf(length, arguments[i]);
(index >= 0) && (delete this[index])
}
return this
})
// remove all entries or some values from this array.
link(proto, 'clear', function (value) {
var argc = arguments.length
if (argc < 1) {
this.splice(0)
return this
}
var args = Array.prototype.slice.call(arguments)
retrace.call(this, function (v, i) {
for (var j = 0; j < argc; j++) {
if (thisCall(v, 'equals', args[j])) {
this.splice(i, 1); return
}
}
})
return this
})
// remove one or more values to create a new array.
link(proto, 'remove', function (value) {
var argc = arguments.length
if (argc < 1) {
return this.slice()
}
var args = Array.prototype.slice.call(arguments)
var result = []
var removed = 0
trace.call(this, function (v, i) {
var keep = true
for (var j = 0; j < argc; j++) {
if (thisCall(v, 'equals', args[j])) {
keep = false; break
}
}
keep ? (result[i - removed] = v) : removed++
})
return result
})
// replace all occurrences of a value to another value or reset them.
link(proto, 'replace', function (value, newValue) {
if (typeof value === 'undefined') {
return this
}
typeof newValue === 'undefined' ? retrace.call(this, function (v, i) {
thisCall(v, 'equals', value) && delete this[i]
}) : trace.call(this, function (v, i) {
thisCall(v, 'equals', value) && (this[i] = newValue)
})
return this
})
// check the existence of an element by a filter function
link(proto, 'has', function (filter) {
if (!isApplicable(filter)) { // as an index number
return typeof this[offsetOf(this.length, filter)] !== 'undefined'
}
var found = false
trace.call(this, function (v, i) {
return (found = boolValueOf(filter.call(this, v, i)))
})
return found
})
// check the existence of a value
link(proto, 'contains', function (value) {
if (typeof value === 'undefined') {
return false
}
var found = false
trace.call(this, function (v, i) {
return (found = thisCall(v, 'equals', value))
})
return found
})
// swap two value by offsets.
link(proto, 'swap', function (i, j) {
var length = this.length
i = offsetOf(length, i)
j = offsetOf(length, j)
if (i === j || i < 0 || i >= length || j < 0 || j >= length) {
return false
}
var tmp = this[i]
typeof this[j] === 'undefined' ? delete this[i] : this[i] = this[j]
typeof tmp === 'undefined' ? delete this[j] : this[j] = tmp
return true
})
// retrieve the first n element(s).
link(proto, 'first', function (count, filter) {
if (typeof count === 'undefined') {
return this[0]
}
if (isApplicable(count)) {
var found
trace.call(this, function (v, i) {
return boolValueOf(count.call(this, v, i)) ? (found = v) || true : false
})
return found
}
count >>= 0
if (count <= 0) {
return []
}
var result = []
if (isApplicable(filter)) {
trace.call(this, function (v, i) {
if (boolValueOf(filter.call(this, v, i))) {
result.push(v)
return (--count) <= 0
} // else return false
})
} else {
trace.call(this, function (v) {
result.push(v)
return (--count) <= 0
})
}
return result
})
// find the index of first occurrence of a value.
var indexOf = link(proto, 'first-of', function (value) {
if (typeof value === 'undefined') {
return null
}
var found = null
trace.call(this, function (v, i) {
return v === value || thisCall(v, 'equals', value)
? (found = i) || true : false
})
return found
})
// retrieve the last n element(s).
link(proto, 'last', function (count, filter) {
if (typeof count === 'undefined') {
return this[this.length - 1]
}
if (isApplicable(count)) {
var found
retrace.call(this, function (v, i) {
return boolValueOf(count.call(this, v, i)) ? (found = v) || true : false
})
return found
}
count >>= 0
if (count <= 0) {
return []
}
var result = []
if (isApplicable(filter)) {
retrace.call(this, function (v, i) {
if (!boolValueOf(filter.call(this, v, i))) return
result.unshift(v); count--
return count <= 0
})
} else {
retrace.call(this, function (v) {
result.unshift(v); count--
return count <= 0
})
}
return result
})
// find the index of the last occurrence of a value.
link(proto, 'last-of', function (value) {
if (typeof value === 'undefined') {
return null
}
var found = null
retrace.call(this, function (v, i) {
return v === value || thisCall(v, 'equals', value)
? (found = i) || true : false
})
return found
})
// edit current array
link(proto, 'insert', function (index, item) {
index = beginOf(this.length, index)
if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments, 2)
args.unshift(index, 0, item)
this.splice.apply(this, args)
} else {
this.splice(index, 0, item)
}
return this
})
link(proto, 'delete', function (index, count) {
index = offsetOf(this.length, index)
count = typeof count === 'undefined' ? 1 : count >> 0
index >= 0 && this.splice(index, count)
return this
})
link(proto, 'splice', function (index, count) {
if ((index >>= 0) < -this.length) {
if (arguments.length < 3) {
return []
}
var args = Array.prototype.slice.call(arguments)
args[0] = 0; args[1] = 0
return this.splice.apply(this, args)
}
switch (arguments.length) {
case 0:
return this.splice()
case 1:
return this.splice(index)
case 2:
return this.splice(index, count)
default:
return this.splice.apply(this, arguments)
}
})
// stack operations.
link(proto, 'push', function () {
Array.prototype.push.apply(this, arguments)
return this
})
link(proto, 'pop', function (count) {
return typeof count === 'undefined' ? this.pop()
: (count >>= 0) >= this.length ? this.splice(0)
: count > 0 ? this.splice(this.length - count)
: this.splice(-1)
})
// queue operations.
link(proto, 'enqueue', function () {
this.unshift.apply(this, arguments)
return this
})
proto.dequeue = proto.pop // dequeue is only an alias of pop.
// reverse the order of all elements
link(proto, 'reverse', function () {
return this.reverse()
})
// re-arrange elements in an array.
var comparerOf = function (reversing, comparer) {
return reversing ? function (a, b) {
var order = comparer(a, b)
return order > 0 ? -1 : order < 0 ? 1 : 0
} : function (a, b) {
var order = comparer(a, b)
return order > 0 ? 1 : order < 0 ? -1 : 0
}
}
var ascComparer = function (a, b) {
var order = thisCall(a, 'compares-to', b)
return order > 0 ? 1 : order < 0 ? -1 : 0
}
var descComparer = function (a, b) {
var order = thisCall(a, 'compares-to', b)
return order > 0 ? -1 : order < 0 ? 1 : 0
}
link(proto, 'sort', function (order, comparer) {
var reversing = false
if (typeof order === 'function') {
comparer = order
} else if ((order >> 0) > 0) {
reversing = true
}
var comparing = typeof comparer === 'function'
? comparerOf(reversing, comparer)
: reversing ? descComparer : ascComparer
return this.sort(comparing)
})
// collection operations
link(proto, 'find', function (filter) {
var result = []
if (isApplicable(filter)) {
trace.call(this, function (v, i) {
boolValueOf(filter.call(this, v, i)) && result.push(i)
})
} else { // pick all valid indices.
trace.call(this, function (v, i) { result.push(i) })
}
return result
})
link(proto, 'select', function (filter) {
return isApplicable(filter) ? this.filter(function (v, i) {
return typeof v !== 'undefined' && boolValueOf(filter.call(this, v, i))
}, this) : this.filter(function (v) {
return typeof v !== 'undefined' // pick all valid indices.
}, this)
})
link(proto, 'map', function (converter) {
return isApplicable(converter)
? this.map(function (v, i) {
if (typeof v !== 'undefined') {
return converter.call(this, v, i)
}
}, this) : this.slice()
})
link(proto, 'reduce', function (value, reducer) {
if (!isApplicable(reducer)) {
if (!isApplicable(value)) {
return value
}