Skip to content

Commit 6ff62fc

Browse files
authored
jwarray validate array
1 parent 4c7c960 commit 6ff62fc

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

src/extensions/jwArray/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ class ArrayType {
7777
return new ArrayType([x], true)
7878
}
7979

80+
static validArray(x) {
81+
if (x instanceof ArrayType) return true;
82+
if (x instanceof Array) return true;
83+
if (typeof x == "object" && typeof x.toJSON == "function") return true;
84+
try {
85+
let parsed = JSON.parse(Cast.toString(x));
86+
if (parsed instanceof Array) return true;
87+
} catch {}
88+
return false;
89+
}
90+
8091
static forArray(x) {
8192
if (x instanceof ArrayType) return new ArrayType([...x.array])
8293
if (x instanceof Array) return new ArrayType([...x])
@@ -664,7 +675,20 @@ class Extension {
664675
}
665676
},
666677
...jwArray.Block
667-
}
678+
},
679+
"---",
680+
{
681+
opcode: 'validate',
682+
text: 'is [INPUT] a valid array?',
683+
blockType: BlockType.BOOLEAN,
684+
arguments: {
685+
INPUT: {
686+
type: ArgumentType.STRING,
687+
defaultValue: '["a", "b", "c"]',
688+
exemptFromNormalization: true
689+
}
690+
}
691+
},
668692
],
669693
menus: {
670694
list: {
@@ -949,6 +973,10 @@ class Extension {
949973
basicSort() {
950974
return 'noop'
951975
}
976+
977+
validate({INPUT}) {
978+
return jwArray.Type.validArray(INPUT);
979+
}
952980
}
953981

954982
module.exports = Extension

0 commit comments

Comments
 (0)