🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

string-array

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-array

Parse string into array of string elements

1.0.1
latest
Source
npm
Version published
Weekly downloads
13K
-5.18%
Maintainers
1
Weekly downloads
 
Created
Source

NPM version Build Status Dependency Status devDependency Status

String Array

Parse string into array of string elements.

"[ hello, world, [ 1, [2, [ 3 ]]]]" :arrow_right: [ "hello", "world", [ "1", [ "2", [ "3" ]]]]

Install

npm i --save string-array

Usage

const 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 these characters in elements: , [ ]

Examples

  • 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

Parameters

stringArray.parse(str, noPrefix, noExtra)

  • str - string array to be parsed
  • noPrefix - if true, then do not check for a prefix
  • noExtra - if true, then do not allow trailing text following a complete array in str

Throws

  • AssertionError("array missing [")
    • Also throws if str doesn't start with [ and noPrefix is true
  • AssertionError("array missing ]")
  • AssertionError("array has extra ]")
  • AssertionError("extra data at end of array") - if noExtra is true and there are extra text following a complete array in str

License

Licensed under the Apache License, Version 2.0

Keywords

string

FAQs

Package last updated on 03 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts