Skip to content

Commit 07cbf26

Browse files
committed
Type splitOnFirstEquals with two items
Also add some test cases.
1 parent 63c1bff commit 07cbf26

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

src/node/util.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -542,15 +542,12 @@ export const loadAMDModule = async <T>(amdPath: string, exportName: string): Pro
542542
return module[exportName] as T
543543
}
544544

545-
export function splitOnFirstEquals(str: string): string[] {
546-
// we use regex instead of "=" to ensure we split at the first
547-
// "=" and return the following substring with it
548-
// important for the hashed-password which looks like this
549-
// $argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY
550-
// 2 means return two items
551-
// Source: https://stackoverflow.com/a/4607799/3015595
552-
// We use the ? to say the the substr after the = is optional
545+
/**
546+
* Split a string on the first equals. The result will always be an array with
547+
* two items regardless of how many equals there are. The second item will be
548+
* undefined if empty or missing.
549+
*/
550+
export function splitOnFirstEquals(str: string): [string, string | undefined] {
553551
const split = str.split(/=(.+)?/, 2)
554-
555-
return split
552+
return [split[0], split[1]]
556553
}

test/unit/node/util.test.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -603,26 +603,39 @@ describe("constructOpenOptions", () => {
603603
})
604604

605605
describe("splitOnFirstEquals", () => {
606-
it("should split on the first equals", () => {
607-
const testStr = "enabled-proposed-api=test=value"
608-
const actual = util.splitOnFirstEquals(testStr)
609-
const expected = ["enabled-proposed-api", "test=value"]
610-
expect(actual).toEqual(expect.arrayContaining(expected))
611-
})
612-
it("should split on first equals regardless of multiple equals signs", () => {
613-
const testStr =
614-
"hashed-password=$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY"
615-
const actual = util.splitOnFirstEquals(testStr)
616-
const expected = [
617-
"hashed-password",
618-
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
619-
]
620-
expect(actual).toEqual(expect.arrayContaining(expected))
621-
})
622-
it("should always return the first element before an equals", () => {
623-
const testStr = "auth="
624-
const actual = util.splitOnFirstEquals(testStr)
625-
const expected = ["auth"]
626-
expect(actual).toEqual(expect.arrayContaining(expected))
606+
const tests = [
607+
{
608+
name: "empty",
609+
key: "",
610+
value: "",
611+
},
612+
{
613+
name: "split on first equals",
614+
key: "foo",
615+
value: "bar",
616+
},
617+
{
618+
name: "split on first equals even with multiple equals",
619+
key: "foo",
620+
value: "bar=baz",
621+
},
622+
{
623+
name: "split with empty value",
624+
key: "foo",
625+
value: "",
626+
},
627+
{
628+
name: "split with no value",
629+
key: "foo",
630+
value: undefined,
631+
},
632+
]
633+
tests.forEach((test) => {
634+
it("should ${test.name}", () => {
635+
const input = test.key && typeof test.value !== "undefined" ? `${test.key}=${test.value}` : test.key
636+
const [key, value] = util.splitOnFirstEquals(input)
637+
expect(key).toStrictEqual(test.key)
638+
expect(value).toStrictEqual(test.value || undefined)
639+
})
627640
})
628641
})

0 commit comments

Comments
 (0)