Add create(a, { props }) syntax (#42)

* Add new create syntax

* Add new create syntax to changelog

* Fix new create syntax test
This commit is contained in:
11poe 2024-11-16 21:20:49 +01:00 committed by GitHub
parent 8799988851
commit 44fe65ee5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 70 additions and 40 deletions

View file

@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
## [Unreleased]
### Added
- `create("ClassName", { props })` and `create(Instance, { props })` syntax.
--------------------------------------------------------------------------------
## [0.3.1] - 2024-10-09 ## [0.3.1] - 2024-10-09
### Added ### Added

View file

@ -40,44 +40,53 @@ local function clone_instance(instance: Instance)
end end
end end
local function create(class_or_instance: string|Instance): (Props) -> Instance local function create(class_or_instance: string | Instance, props: Props?): ((Props) -> Instance) | Instance
if type(class_or_instance) == "string" then local result: (Props) -> Instance
return create_instance(class_or_instance) if type(class_or_instance) == "string" then
elseif typeof(class_or_instance) == "Instance" then result = create_instance(class_or_instance)
return clone_instance(class_or_instance) elseif typeof(class_or_instance) == "Instance" then
else result = clone_instance(class_or_instance)
throw("bad argument #1, expected string or instance, got " .. typeof(class_or_instance)) else
return nil :: never throw("bad argument #1, expected string or instance, got " .. typeof(class_or_instance))
end return nil :: never
end
if props then
return result(props)
end
return result
end end
type Props = { [any]: any } type Props = { [any]: any }
type Create<Name, Instance> = ((Name, Props) -> Instance) & ((Name) -> (Props) -> Instance)
return (create :: any) :: return (create :: any) ::
( <T>(T & Instance) -> (Props) -> T ) & & ( <T>(T & Instance) -> (Props) -> T )
( ("Folder") -> (Props) -> Folder ) & & ( <T>(T & Instance, Props) -> T )
( ("BillboardGui") -> (Props) -> BillboardGui ) & & Create<"Folder", Folder>
( ("CanvasGroup") -> (Props) -> CanvasGroup ) & & Create<"BillboardGui", BillboardGui>
( ("Frame") -> (Props) -> Frame ) & & Create<"CanvasGroup", CanvasGroup>
( ("ImageButton") -> (Props) -> ImageButton ) & & Create<"Frame", Frame>
( ("ImageLabel") -> (Props) -> ImageLabel ) & & Create<"ImageButton", ImageButton>
( ("ScreenGui") -> (Props) -> ScreenGui ) & & Create<"ImageLabel", ImageLabel>
( ("ScrollingFrame") -> (Props) -> ScrollingFrame ) & & Create<"ScreenGui", ScreenGui>
( ("SurfaceGui") -> (Props) -> SurfaceGui ) & & Create<"ScrollingFrame", ScrollingFrame>
( ("TextBox") -> (Props) -> TextBox ) & & Create<"SurfaceGui", SurfaceGui>
( ("TextButton") -> (Props) -> TextButton ) & & Create<"TextBox", TextBox>
( ("TextLabel") -> (Props) -> TextLabel ) & & Create<"TextButton", TextButton>
( ("UIAspectRatioConstraint") -> (Props) -> UIAspectRatioConstraint ) & & Create<"TextLabel", TextLabel>
( ("UICorner") -> (Props) -> UICorner ) & & Create<"UIAspectRatioConstraint", UIAspectRatioConstraint>
( ("UIGradient") -> (Props) -> UIGradient ) & & Create<"UICorner", UICorner>
( ("UIGridLayout") -> (Props) -> UIGridLayout ) & & Create<"UIGradient", UIGradient>
( ("UIListLayout") -> (Props) -> UIListLayout ) & & Create<"UIGridLayout", UIGridLayout>
( ("UIPadding") -> (Props) -> UIPadding ) & & Create<"UIListLayout", UIListLayout>
( ("UIPageLayout") -> (Props) -> UIPageLayout ) & & Create<"UIPadding", UIPadding>
( ("UIScale") -> (Props) -> UIScale ) & & Create<"UIPageLayout", UIPageLayout>
( ("UISizeConstraint") -> (Props) -> UISizeConstraint ) & & Create<"UIScale", UIScale>
( ("UIStroke") -> (Props) -> UIStroke ) & & Create<"UISizeConstraint", UISizeConstraint>
( ("UITableLayout") -> (Props) -> UITableLayout ) & & Create<"UIStroke", UIStroke>
( ("UITextSizeConstraint") -> (Props) -> UITextSizeConstraint ) & & Create<"UITableLayout", UITableLayout>
( ("VideoFrame") -> (Props) -> VideoFrame ) & & Create<"UITextSizeConstraint", UITextSizeConstraint>
( ("ViewportFrame") -> (Props) -> ViewportFrame ) & & Create<"VideoFrame", VideoFrame>
( (string) -> (Props) -> Instance ) & Create<"ViewportFrame", ViewportFrame>
& Create<string, Instance>

View file

@ -721,6 +721,19 @@ TEST("create()", wrap_root(function()
local source = vide.source local source = vide.source
local cleanup = vide.cleanup local cleanup = vide.cleanup
do CASE "create(\"ClassName\", props) syntax"
local frame = create("Frame", { BackgroundTransparency = 0.5, Name = "Foo" })
CHECK(frame.BackgroundTransparency == 0.5)
CHECK(frame.Name == "Foo")
end
do CASE "create(Instance, props) syntax"
local frame0 = create("Frame", { BackgroundTransparency = 0.5, Name = "Foo" })
local frame = create(frame0, { BackgroundTransparency = 1 })
CHECK(frame.BackgroundTransparency == 1)
CHECK(frame.Name == "Foo")
end
do CASE "apply default properties" do CASE "apply default properties"
local defaults = require "../src/defaults" local defaults = require "../src/defaults"
local frame = create "Frame" {} :: Instance & { BorderSizePixel: any, BorderColor3: any } local frame = create "Frame" {} :: Instance & { BorderSizePixel: any, BorderColor3: any }