Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework publishing to work with all popular package managers
- Loading branch information
Showing
3 changed files
with
176 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/cli/src/utils/getLastJsonObjectFromString.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { getLastJsonObjectFromString } from "./getLastJsonObjectFromString"; | ||
|
||
describe("getLastJsonObjectFromString", () => { | ||
it("should handle stringified object", () => { | ||
expect( | ||
getLastJsonObjectFromString(JSON.stringify({ test: "foo" })) | ||
).toEqual({ test: "foo" }); | ||
}); | ||
|
||
it("should handle stringified deep object", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
JSON.stringify({ | ||
test: "foo", | ||
bar: { baz: { qwe: "rty" }, arr: [1, 2, 3, 4] } | ||
}) | ||
) | ||
).toEqual({ | ||
test: "foo", | ||
bar: { baz: { qwe: "rty" }, arr: [1, 2, 3, 4] } | ||
}); | ||
}); | ||
|
||
it("should handle leading whitespace", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
` \n\n ${JSON.stringify({ test: "foo", baz: { qwe: "rty" } })}` | ||
) | ||
).toEqual({ test: "foo", baz: { qwe: "rty" } }); | ||
}); | ||
|
||
it("should handle trailing whitespace", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
`${JSON.stringify({ test: "foo", baz: { qwe: "rty" } })} \n\n ` | ||
) | ||
).toEqual({ test: "foo", baz: { qwe: "rty" } }); | ||
}); | ||
|
||
it("should handle trailing text", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
`${JSON.stringify({ test: "foo", baz: { qwe: "rty" } })} \n\n test` | ||
) | ||
).toEqual({ test: "foo", baz: { qwe: "rty" } }); | ||
}); | ||
|
||
it("should handle string with multiple objects", () => { | ||
expect( | ||
getLastJsonObjectFromString( | ||
`${JSON.stringify({ | ||
test: "foo", | ||
baz: { qwe: "rty" } | ||
})} \n\n ${JSON.stringify({ much: "awesome" })}` | ||
) | ||
).toEqual({ much: "awesome" }); | ||
}); | ||
|
||
it("should return `null` for an empty string", () => { | ||
expect(getLastJsonObjectFromString("")).toEqual(null); | ||
}); | ||
|
||
it("should return `null` for a string with a broken object", () => { | ||
expect(getLastJsonObjectFromString(`{"bar:"`)).toEqual(null); | ||
}); | ||
|
||
it("should return `null` for a string without an object", () => { | ||
expect(getLastJsonObjectFromString(`qwerty`)).toEqual(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const getLastJsonObjectFromString = (str: string) => { | ||
str = str.replace(/[^}]*$/, ""); | ||
|
||
while (str) { | ||
str = str.replace(/[^{]*/, ""); | ||
|
||
try { | ||
return JSON.parse(str); | ||
} catch (err) { | ||
// move past the potentially leading `{` so the regexp in the loop can try to match for the next `{` | ||
str = str.slice(1); | ||
} | ||
} | ||
return null; | ||
}; |