Skip to content

Commit

Permalink
single-element arrays via trailing comma
Browse files Browse the repository at this point in the history
  • Loading branch information
anselanza committed Aug 3, 2021
1 parent ca3a48d commit b6f0bc3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Array-like strings (currently, only comma-separated values are intepreted like t
* `"test,one,two,three"` becomes `["test","one","two","three"]` (an array of strings)
* `"0,1,2,3"` becomes `[0,1,2,3]` (an array of numbers)

Single-element arrays need you to provide a trailing comma to cue the parser appropriately:
* `"1.1,"` becomes `[1.1]` (single-element array of numbers)
* `"someString,"` becomes `["someString"]` (single-element array of strings)

This module was originally inspired by the experience of using a configuration module ([rc](https://www.npmjs.com/package/rc)) and having to check things like `active === false || active === 'false'` repeatedly. I have therefore provided an example of this use case [below](#example-in-rc-config).

## Usage
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parse-strings-in-object",
"version": "1.3.5",
"version": "1.4.0",
"description": "Convert string values in object to boolean and numbers",
"keywords": [
"json",
Expand Down
19 changes: 19 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ describe("convert string representations of an arrays into real arrays", () => {
expect(result).toEqual({ list: ["test", "one", "two", "three"]})
});

test("single-element array (string)", () => {
const before = { list: "one," };
const result = parser(before) as { list: string[] };
expect(Array.isArray(result.list)).toBeTruthy();
expect(typeof result.list).toBe("object");
expect(result.list.length).toEqual(1);
expect(result).toEqual({ list: ["one"]});
})

test("single-element array (number)", () => {
const before = { list: "0.05," };
const result = parser(before) as { list: number[] };
expect(Array.isArray(result.list)).toBeTruthy();
expect(typeof result.list).toBe("object");
expect(result.list.length).toEqual(1);
expect(result.list[0]).toEqual(0.05);
expect(result).toEqual({ list: [0.05]})
});

test("array of numnbers", () => {
const before = { list: "0,1,2,4,8" };
const result = parser(before) as { list: number[] };
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ const isArrayLikeString = (s: any): boolean => {
}

const arrayLikeStringToArray = (s: string, token: string = ",") =>
s.split(token).map(element => element.trim());
s.split(token).map(element => element.trim()).filter(element => element !== "" && element !== null);

export = parseKeys;

0 comments on commit b6f0bc3

Please sign in to comment.