-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
86 additions
and
8 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
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
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { defineRoom, RoomConstructor } from "./defineRoom.js" | ||
export { Room } from "./base.js" | ||
export { RoomDefinition } from "./roomType.js" | ||
export { VariantsConfig } from "./gameVariants.js" |
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
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
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,17 @@ | ||
import { variantParser } from "../variantParser.js" | ||
|
||
describe("deep object", () => { | ||
it("parses single deep field", () => { | ||
const data = { | ||
"foo.bar": true, | ||
"card.suit": "S", | ||
"card.rank": "A", | ||
first: 1, | ||
} | ||
const result = variantParser(data) | ||
expect(result["foo"]["bar"]).toBe(true) | ||
expect(result["card"]["suit"]).toBe("S") | ||
expect(result["card"]["rank"]).toBe("A") | ||
expect(result["first"]).toBe(1) | ||
}) | ||
}) |
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,49 @@ | ||
function deepAssign( | ||
object: Record<string, any>, | ||
path: string[], | ||
value: unknown, | ||
): void { | ||
if (path.length === 1) { | ||
// as deep as it gets, set | ||
object[path[0]] = value | ||
return | ||
} | ||
|
||
const key = path.shift() | ||
|
||
if (object[key] === undefined) { | ||
object[key] = {} | ||
} else if (typeof object[key] !== "object") { | ||
throw new Error( | ||
`Expected to access "${path.join(".")} but it's of type ${typeof object[ | ||
key | ||
]}"`, | ||
) | ||
} | ||
|
||
deepAssign(object[key], path, value) | ||
} | ||
|
||
export const variantParser = <T extends Record<string, unknown>>( | ||
value: unknown, | ||
): T => { | ||
// Expected flat object of props. Possible sub objects with dot notation | ||
|
||
if (typeof value !== "object") { | ||
throw new Error("Variant data not an object, cannot parse") | ||
} | ||
|
||
const result = {} | ||
|
||
Object.keys(value).forEach((key) => { | ||
const isPath = key.includes("$") | ||
|
||
if (!isPath) { | ||
result[key] = value[key] | ||
} else { | ||
deepAssign(result, key.split("$"), value[key]) | ||
} | ||
}) | ||
|
||
return result as T | ||
} |
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
8 changes: 6 additions & 2 deletions
8
packages/server/src/room/gameVariants.ts → packages/types/src/variants.d.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