1
- function addTypeDict ( p5 , fn ) {
1
+ function addData ( p5 , fn ) {
2
+ fn . append = function ( array , value ) {
3
+ array . push ( value ) ;
4
+ return array ;
5
+ } ;
6
+
7
+ fn . arrayCopy = function ( src , srcPosition , dst , dstPosition , length ) {
8
+ // the index to begin splicing from dst array
9
+ let start ;
10
+ let end ;
11
+
12
+ if ( typeof length !== 'undefined' ) {
13
+ end = Math . min ( length , src . length ) ;
14
+ start = dstPosition ;
15
+ src = src . slice ( srcPosition , end + srcPosition ) ;
16
+ } else {
17
+ if ( typeof dst !== 'undefined' ) {
18
+ // src, dst, length
19
+ // rename so we don't get confused
20
+ end = dst ;
21
+ end = Math . min ( end , src . length ) ;
22
+ } else {
23
+ // src, dst
24
+ end = src . length ;
25
+ }
26
+
27
+ start = 0 ;
28
+ // rename so we don't get confused
29
+ dst = srcPosition ;
30
+ src = src . slice ( 0 , end ) ;
31
+ }
32
+
33
+ // Since we are not returning the array and JavaScript is pass by reference
34
+ // we must modify the actual values of the array
35
+ // instead of reassigning arrays
36
+ Array . prototype . splice . apply ( dst , [ start , end ] . concat ( src ) ) ;
37
+ } ;
38
+
39
+ fn . concat = ( list0 , list1 ) => list0 . concat ( list1 ) ;
40
+
41
+ fn . reverse = list => list . reverse ( ) ;
42
+
43
+ fn . shorten = function ( list ) {
44
+ list . pop ( ) ;
45
+ return list ;
46
+ } ;
47
+
48
+ fn . sort = function ( list , count ) {
49
+ let arr = count ? list . slice ( 0 , Math . min ( count , list . length ) ) : list ;
50
+ const rest = count ? list . slice ( Math . min ( count , list . length ) ) : [ ] ;
51
+ if ( typeof arr [ 0 ] === 'string' ) {
52
+ arr = arr . sort ( ) ;
53
+ } else {
54
+ arr = arr . sort ( ( a , b ) => a - b ) ;
55
+ }
56
+ return arr . concat ( rest ) ;
57
+ } ;
58
+
59
+ fn . splice = function ( list , value , index ) {
60
+ // note that splice returns spliced elements and not an array
61
+ Array . prototype . splice . apply ( list , [ index , 0 ] . concat ( value ) ) ;
62
+
63
+ return list ;
64
+ } ;
65
+
66
+ fn . subset = function ( list , start , count ) {
67
+ if ( typeof count !== 'undefined' ) {
68
+ return list . slice ( start , start + count ) ;
69
+ } else {
70
+ return list . slice ( start , list . length ) ;
71
+ }
72
+ } ;
73
+
74
+ fn . join = function ( list , separator ) {
75
+ return list . join ( separator ) ;
76
+ } ;
77
+
78
+ fn . match = function ( str , reg ) {
79
+ return str . match ( reg ) ;
80
+ } ;
81
+
82
+ fn . matchAll = function ( str , reg ) {
83
+ const re = new RegExp ( reg , 'g' ) ;
84
+ let match = re . exec ( str ) ;
85
+ const matches = [ ] ;
86
+ while ( match !== null ) {
87
+ matches . push ( match ) ;
88
+ // matched text: match[0]
89
+ // match start: match.index
90
+ // capturing group n: match[n]
91
+ match = re . exec ( str ) ;
92
+ }
93
+ return matches ;
94
+ } ;
95
+
96
+ fn . split = function ( str , delim ) {
97
+ return str . split ( delim ) ;
98
+ } ;
99
+
100
+ fn . trim = function ( str ) {
101
+ if ( str instanceof Array ) {
102
+ return str . map ( this . trim ) ;
103
+ } else {
104
+ return str . trim ( ) ;
105
+ }
106
+ } ;
107
+
2
108
fn . createStringDict = function ( key , value ) {
3
109
return new p5 . StringDict ( key , value ) ;
4
110
} ;
@@ -196,4 +302,8 @@ function addTypeDict(p5, fn){
196
302
return this . _keyTest ( - 1 ) ;
197
303
}
198
304
} ;
305
+ }
306
+
307
+ if ( typeof p5 !== undefined ) {
308
+ p5 . registerAddon ( addData ) ;
199
309
}
0 commit comments