/SwiftyJS

Interact with your JavaScript code like Swift with Swift Macro

Primary LanguageSwiftMIT LicenseMIT

SwiftyJS

Swift

Interact with your JavaScript code like Swift with Swift

Getting Started

To get started with SwiftyJS:

  1. Create a protocol ๐Ÿ–‹๏ธ
  2. Use @SwiftyJS macro โ‡๏ธ
  3. Set JavaScript code or file to generated class. โŒจ๏ธ
  4. And done ๐Ÿš€
@SwiftyJS
protocol DataCreator {
    var lastUser: User { get throws }
    
    func createUser() throws -> User
}

let js = DataCreatorJSBridge()
js.loadFrom(jsCode: """
    var lastUser = null;
    
    function createUser() {
        let user = {
            id: 10,
            name: "Yusuf",
            score:5.1,
        }

        lastUser = user;
        return user;  
    }
""")

print(try js.createUser())

// Output: User(id: "10", name: "Yusuf", score: 5.1)

try js.setLastUser(User(id: "7", name: "John", score: 1.3))
print(try js.lastUser)

// Output: User(id: "7", name: "John", score: 1.3)

Features

  • ๐Ÿ”ฅSeamless Swift-JavaScript Interaction: Integrate SwiftyJS into your Swift projects effortlessly. Define a Protocol with the @SwiftyJS macro and interact with JavaScript code seamlessly.
  • ๐Ÿ“Loading JavaScript Code: Load JavaScript code files or string using the {ProtocolName}JSBridge class.
  • ๐Ÿค–Call JavaScript Functions: Using the Generated Class, call JavaScript functions and get results in a Swift-like manner.
  • ๐ŸงพAccessing JavaScript Data: Seamlessly access JavaScript data from Swift using swift variable.
  • ๐Ÿ”„Updating JavaScript Data: SwiftyJS also allows you to update JavaScript data from Swift. You can set the variable with setter function.

Installation

For Xcode project

You can add SwiftyJS macro to your project as a package.

https://github.com/yusufozgul/SwiftyJS

For Swift Package Manager

In Package.swift add:

dependencies: [
    .package(url: "https://github.com/yusufozgul/SwiftyJS", from: "0.0.1"),
]

and then add the product to any target that needs access to the macro:

.product(name: "SwiftyJS", package: "SwiftyJS"),

๐ŸŽ‰ Acknowledgements

License

This library is released under the MIT license. See LICENSE for details.