Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/exoego/scalajs-types-util
Scala.js types utility to facilitate common type transformations
https://github.com/exoego/scalajs-types-util
Last synced: 3 months ago
JSON representation
Scala.js types utility to facilitate common type transformations
- Host: GitHub
- URL: https://github.com/exoego/scalajs-types-util
- Owner: exoego
- License: apache-2.0
- Created: 2020-02-13T08:31:42.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-08-17T13:33:29.000Z (5 months ago)
- Last Synced: 2024-10-06T03:58:32.264Z (3 months ago)
- Language: Scala
- Homepage:
- Size: 616 KB
- Stars: 1
- Watchers: 4
- Forks: 2
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# scalajs-types-util
Scala.js types utility to facilitate common type transformations
## Support matrix
| | ScalaJS 0.6.28+ | ScalaJS 1.x |
| ---------- | :-----------------------------------: | :----------------: |
| Scala 2.13 | :heavy_check_mark: (v0.3.0 was final) | :heavy_check_mark: |
| Scala 2.12 | :heavy_check_mark: (v0.3.0 was final) | :heavy_check_mark: |
| Scala 2.11 | N/A | N/A |
| Scala 2.10 | N/A | N/A |## How to use
Add below line to your SBT project.
```sbt
libraryDependencies += "net.exoego" %%% "scalajs-types-util" % "0.3.0"
```### Factory macro
`@Factory` macro creates a highly-optimized factory method for JS trait, just like normal case classes.
Each factory methods are added to a corresponding companion object (if not exist, created automatically).JS trait is generally lighter and faster than JS class, since plain old JS object can be trait, but class need extra overheads.
However, creating a instance of JS trait in Scala.js is a bit error-prone or verbose.
`@Factory` macro improves the situation !See how to use it.
```scala
import scala.scalajs.js
import net.exoego.scalajs.types.util.Factory@Factory
trait Foo extends js.Object {
var x: Int
var y: js.UndefOr[String]
}val f = Foo(x = 1)
assert(f.x === 1)
assert(f.y === js.undefined)
```Type aliases are also supported.
```scala
import scala.scalajs.js
import net.exoego.scalajs.types.util.Factory@Factory
trait Foo extends js.Object {
var x: Foo.X
var y: Foo.Y
}
object Foo {
type X = Int
type y = js.UndefOr[String]
}val f = Foo(x = 1)
assert(f.x === 1)
assert(f.y === js.undefined)
```#### Inlining factory method or not
By default, factory methods will be inlined (marked with `@inline` annotation), so companion object may be
completely removed in `fullOptStage`.
Inlining may reduce the size of generated JS for many cases, but if same factory methods are used many times, may be inlining increase JS size.
In such case, you may avoid inlinining by setting `inline` parameter to `false`.```scala
@Factory(inline = false)
trait Base extends js.Object {
var foo: String
}
```
#### LimitationIf trait is defined inside object, `isTopLevel` argument must be `false`.
```scala
object Outer {
@Factory(isTopLevel = false)
trait Base extends js.Object {
var foo: String
}
}
```