soiz1 commited on
Commit
41fa438
·
verified ·
1 Parent(s): c5d91d7

Update src/extensions/jwArray/index.js

Browse files
Files changed (1) hide show
  1. src/extensions/jwArray/index.js +49 -2
src/extensions/jwArray/index.js CHANGED
@@ -40,7 +40,10 @@ class ArrayType {
40
  array = []
41
 
42
  constructor(array = []) {
43
- this.array = array
 
 
 
44
  }
45
 
46
  static toArray(x) {
@@ -67,7 +70,7 @@ class ArrayType {
67
  if (typeof x.jwArrayHandler == "function") {
68
  return x.jwArrayHandler()
69
  }
70
- return Cast.toString(x)
71
  case "undefined":
72
  return "null"
73
  case "number":
@@ -108,6 +111,15 @@ class ArrayType {
108
  return root
109
  }
110
 
 
 
 
 
 
 
 
 
 
111
  get length() {
112
  return this.array.length
113
  }
@@ -185,6 +197,18 @@ class Extension {
185
  hideFromPalette: true, //doesn't work for some reason
186
  ...jwArray.Block
187
  },
 
 
 
 
 
 
 
 
 
 
 
 
188
  {
189
  opcode: 'split',
190
  text: 'split [STRING] by [DIVIDER]',
@@ -324,6 +348,18 @@ class Extension {
324
  },
325
  ...jwArray.Block
326
  },
 
 
 
 
 
 
 
 
 
 
 
 
327
  "---",
328
  {
329
  opcode: 'forEachI',
@@ -394,6 +430,10 @@ class Extension {
394
  return jwArray.Type.toArray(LIST)
395
  }
396
 
 
 
 
 
397
  split({STRING, DIVIDER}) {
398
  STRING = Cast.toString(STRING)
399
  DIVIDER = Cast.toString(DIVIDER)
@@ -470,6 +510,13 @@ class Extension {
470
  return ARRAY
471
  }
472
 
 
 
 
 
 
 
 
473
  forEachI({}, util) {
474
  let arr = util.thread.stackFrames[0].jwArray
475
  return arr ? Cast.toNumber(arr[0]) + 1 : 0
 
40
  array = []
41
 
42
  constructor(array = []) {
43
+ this.array = array.map(v => {
44
+ if (v instanceof Array) return new ArrayType([...v])
45
+ return v
46
+ })
47
  }
48
 
49
  static toArray(x) {
 
70
  if (typeof x.jwArrayHandler == "function") {
71
  return x.jwArrayHandler()
72
  }
73
+ return "Object"
74
  case "undefined":
75
  return "null"
76
  case "number":
 
111
  return root
112
  }
113
 
114
+ flat(depth = 1) {
115
+ depth = Math.floor(depth)
116
+ if (depth < 1) return this
117
+ return new ArrayType(this.array.reduce((o, v) => {
118
+ if (v instanceof ArrayType) return [...o, ...v.flat(depth - 1).array]
119
+ return [...o, v]
120
+ }, []))
121
+ }
122
+
123
  get length() {
124
  return this.array.length
125
  }
 
197
  hideFromPalette: true, //doesn't work for some reason
198
  ...jwArray.Block
199
  },
200
+ {
201
+ opcode: 'parse',
202
+ text: 'parse [INPUT] as array',
203
+ arguments: {
204
+ INPUT: {
205
+ type: ArgumentType.STRING,
206
+ defaultValue: '["a", "b", "c"]',
207
+ exemptFromNormalization: true
208
+ }
209
+ },
210
+ ...jwArray.Block
211
+ },
212
  {
213
  opcode: 'split',
214
  text: 'split [STRING] by [DIVIDER]',
 
348
  },
349
  ...jwArray.Block
350
  },
351
+ {
352
+ opcode: 'flat',
353
+ text: 'flat [ARRAY] with depth [DEPTH]',
354
+ arguments: {
355
+ ARRAY: jwArray.Argument,
356
+ DEPTH: {
357
+ type: ArgumentType.NUMBER,
358
+ defaultValue: 1
359
+ }
360
+ },
361
+ ...jwArray.Block
362
+ },
363
  "---",
364
  {
365
  opcode: 'forEachI',
 
430
  return jwArray.Type.toArray(LIST)
431
  }
432
 
433
+ parse({INPUT}) {
434
+ return jwArray.Type.toArray(INPUT)
435
+ }
436
+
437
  split({STRING, DIVIDER}) {
438
  STRING = Cast.toString(STRING)
439
  DIVIDER = Cast.toString(DIVIDER)
 
510
  return ARRAY
511
  }
512
 
513
+ flat({ARRAY, DEPTH}) {
514
+ ARRAY = jwArray.Type.toArray(ARRAY)
515
+ DEPTH = Cast.toNumber(DEPTH)
516
+
517
+ return ARRAY.flat(DEPTH)
518
+ }
519
+
520
  forEachI({}, util) {
521
  let arr = util.thread.stackFrames[0].jwArray
522
  return arr ? Cast.toNumber(arr[0]) + 1 : 0