- API reference - TypeDoc
- Markdown reference - the full API and runtime behavior in one file. Also ships in the package.
- GitHub
Parse string into array of string elements.
"[ hello, world, [ 1, [2, [ 3 ]]]]" ➡️ [ "hello", "world", [ "1", [ "2", [ "3" ]]]]
npm i --save string-arrayconst stringArray = require("string-array");
const r1 = stringArray.parse("[]");
// r1 === { prefix: "", array: [], remain: "" }
const r2 = stringArray.parse("test[1,2,3]");
// r2 === { prefix: "test", array: ["1","2","3"], remain: "" }-
Elements are automatically treated as strings, so quotes
',"and backtick are taken as part of element. -
All leading and trailing whitespaces are automatically
trimed. -
Can't have
,or]in elements. A[in the middle of an element is kept as plain text, but the next]still closes the array, so[a[b], c]gives["a[b"]withc]left inremain.
stringArray.parse(""):stringArray.parse("[]"):
{
prefix: "",
array: [],
remain: ""
}stringArray.parse('test[1,2,"3"]'):
{
prefix: "test",
array: ["1","2", '"3"'],
remain: ""
}stringArray.parse("[hello, world, [ [ foo, bar ], 1, [ 2 ], 3 ] ] some other stuff [blah]"):
{
prefix: "",
array: ["hello", "world", [ ["foo", "bar"], "1", ["2"], "3" ] ],
remain: "some other stuff [blah]"
}More samples in test
str- string array to be parsednoPrefix- iftrue, then do not check for a prefixnoExtra- iftrue, then do not allow trailing text following a complete array instr
AssertionError("array missing [")- Also throws if
strdoesn't start with[andnoPrefixistrue
- Also throws if
AssertionError("array missing ]")AssertionError("array has extra ]")AssertionError("extra data at end of array")- ifnoExtraistrueand there are extra text following a complete array instr
Licensed under the Apache License, Version 2.0
