scalajs-types-util


License

License

Categories

Categories

JavaScript Languages Scala Net
GroupId

GroupId

net.exoego
ArtifactId

ArtifactId

scalajs-types-util_sjs1_2.13
Last Version

Last Version

0.3.0
Release Date

Release Date

Type

Type

jar
Description

Description

scalajs-types-util
scalajs-types-util
Project URL

Project URL

https://github.com/exoego/scalajs-types-util
Project Organization

Project Organization

net.exoego
Source Code Management

Source Code Management

https://github.com/exoego/scalajs-types-util

Download scalajs-types-util_sjs1_2.13

How to add to project

<!-- https://jarcasting.com/artifacts/net.exoego/scalajs-types-util_sjs1_2.13/ -->
<dependency>
    <groupId>net.exoego</groupId>
    <artifactId>scalajs-types-util_sjs1_2.13</artifactId>
    <version>0.3.0</version>
</dependency>
// https://jarcasting.com/artifacts/net.exoego/scalajs-types-util_sjs1_2.13/
implementation 'net.exoego:scalajs-types-util_sjs1_2.13:0.3.0'
// https://jarcasting.com/artifacts/net.exoego/scalajs-types-util_sjs1_2.13/
implementation ("net.exoego:scalajs-types-util_sjs1_2.13:0.3.0")
'net.exoego:scalajs-types-util_sjs1_2.13:jar:0.3.0'
<dependency org="net.exoego" name="scalajs-types-util_sjs1_2.13" rev="0.3.0">
  <artifact name="scalajs-types-util_sjs1_2.13" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.exoego', module='scalajs-types-util_sjs1_2.13', version='0.3.0')
)
libraryDependencies += "net.exoego" % "scalajs-types-util_sjs1_2.13" % "0.3.0"
[net.exoego/scalajs-types-util_sjs1_2.13 "0.3.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.13.3
org.scala-js : scalajs-library_2.13 jar 1.0.1
org.scalameta : scalameta_sjs1_2.13 jar 4.3.16

test (2)

Group / Artifact Type Version
org.scala-js : scalajs-test-bridge_2.13 jar 1.0.1
org.scalatest : scalatest_sjs1_2.13 jar 3.1.2

Project Modules

There are no modules declared in this project.

scalajs-types-util

Scala.js types utility to facilitate common type transformations

Support matrix

ScalaJS 0.6.28+ ScalaJS 1.x
Scala 2.13 ✔️ (v0.3.0 was final) ✔️
Scala 2.12 ✔️ (v0.3.0 was final) ✔️
Scala 2.11 N/A N/A
Scala 2.10 N/A N/A

How to use

Add below line to your SBT project.

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.

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.

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.

@Factory(inline = false)
trait Base extends js.Object {
  var foo: String
}

Limitation

If trait is defined inside object, isTopLevel argument must be false.

object Outer {
    @Factory(isTopLevel = false)
    trait Base extends js.Object {
      var foo: String
    }
}

Versions

Version
0.3.0
0.2.2
0.2.1
0.2.0
0.1.0