This repository contains handy macros , facades for upcoming javascript features(not finalized yet), polyfills when they make sense and some useful misc stuff.
// build.sbt
resolvers += Resolver.bintrayRepo("scalajs-plus", "maven")
libraryDependencies += "scalajs-plus" %%% "core" % "replaceThisWithLatestVersionNumberFromReleaseTags"
//works with scalajs >= 1.0.0-M2 and supports only scala 2.12
converts scala function parameters to javascript literal object.
def point(x:Int,y:Int,z:js.UndefOr[Int] = js.undefined) : js.Object = {
val p = FunctionObjectMacro()
p.asInstanceOf[js.Object]
}
val p = point(1,1) // {x:1,y:1}
val p = point(1,1,1) // {x:1,y:1,z:1}
Renaming Params
def voltage(i:Double,@rename("resistance")r:Double) : js.Object = {
val v = FunctionObjectMacro()
v.asInstanceOf[js.Object]
}
val v1 = volate(i = 2,r = 100) // {i: 2,resistance:100}
Excluding Params
def energy(@rename("mass") m:Double,c:Double,@exclude debug:Boolean):js.Object = {
if(debug) // do something
val e = FunctionObjectMacro()
e.asInstanceOf[js.Object]
}
val e = energy(m = 74,c = 3.18,debug = false) // {mass:74,c:3.18}
Facade for :https://github.com/tc39/proposal-observable (Ref Impl : https://github.com/zenparsing/zen-observable)
Polyfill for js.Object.assign
Example
val target = js.Dynamic.literal(id = 1, name = "hello")
val source1 = js.Dynamic.literal(id = 2, name2 = "hello2")
val source2 = js.Dynamic.literal(name = "hello3", age = 20)
val combine = ObjectAssign(target, source1, source2) // {id:2,name:"hello3",name2:"hello2",age:20}
Inline version of js.UndefOr[] , mostly used in conjunction with FunctionObjectMacro. More Details : scala-js/scala-js#2714
Example
def fun(value: OptionalParam[Int] = OptDefault) = value
expect(fun(2)).toEqual(OptSpecified(2).asInstanceOf[js.Any])
expect(fun()).toBe(OptDefault)
Readonly version of js.Array
Example
val a:ReadonlyArray[Int] = ReadonlyArray(1,2,3)
a.length //compiles fine
a.push() // coompile error
Blindly casts scalajs union type to js.Any
Example
val id : String | Int = 3
val o = js.Dynamic.literal(id = id) // compile error
import scalajsplus.DangerousUnionToJSAnyImplicit._ //error gone
//Nit : make sure that scope of this import as least as possible