Skip to content

Commit

Permalink
Refactor MergeProps
Browse files Browse the repository at this point in the history
  • Loading branch information
nafg committed Dec 20, 2021
1 parent 46745fc commit de86c1a
Showing 1 changed file with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,43 @@ private[simplefacade] object MergeProps {
}

private def AnyJsFunction(f: AnyJsFunction): AnyJsFunction = f
private val shouldMergeArray = (key: String) => key == "children"
private val shouldMergeObject = (key: String) => key == "style"
private val shouldMergeString = (key: String) => key == "className"
private val shouldMergeFunc = (key: String) => key.startsWith("on")
private def mergeChildrenArrays(value1: js.Any, value2: js.Any) =
if (value1.asInstanceOf[js.Array[js.Any]].length == 0) value2
else if (value2.asInstanceOf[js.Array[js.Any]].length == 0) value1
else
value1
.asInstanceOf[js.Array[js.Any]]
.concat(value2.asInstanceOf[js.Array[js.Any]])
private def mergeClassNames(value1: js.Any, value2: js.Any): js.Any =
if (value1.asInstanceOf[String] == "") value2
else if (value2.asInstanceOf[String] == "") value1
else value1.toString + " " + value2
private def mergeStyleObjects(value1: js.Any, value2: js.Any) =
js.Object.assign(
js.Object(),
value1.asInstanceOf[js.Object],
value2.asInstanceOf[js.Object]
)
private def mergeEventHandlers(value1: js.Any, value2: js.Any): js.ThisFunction =
AnyJsFunction { (_this, args) =>
value1.asInstanceOf[js.Function].call(_this, args: _*)
value2.asInstanceOf[js.Function].call(_this, args: _*)
}
private def merge(key: String, value1: js.Any, value2: js.Any): js.Any =
if (js.isUndefined(value1))
value2
else if (js.isUndefined(value2))
value1
else if (js.typeOf(value1) != js.typeOf(value2))
value2
else if (js.typeOf(value1) == "array" && shouldMergeArray(key)) {
if (value1.asInstanceOf[js.Array[js.Any]].length == 0) value2
else if (value2.asInstanceOf[js.Array[js.Any]].length == 0) value1
else value1.asInstanceOf[js.Array[js.Any]].concat(value2.asInstanceOf[js.Array[js.Any]])
}
else if (js.typeOf(value1) == "string" && shouldMergeString(key)) {
if (value1.asInstanceOf[String] == "") value2
else if (value2.asInstanceOf[String] == "") value1
else value1.toString + " " + value2
}
else if (js.typeOf(value1) == "object" && shouldMergeObject(key))
js.Object.assign(
js.Object(),
value1.asInstanceOf[js.Object],
value2.asInstanceOf[js.Object]
)
else if (js.typeOf(value1) == "function" && shouldMergeFunc(key))
AnyJsFunction { (_this, args) =>
value1.asInstanceOf[js.Function].call(_this, args: _*)
value2.asInstanceOf[js.Function].call(_this, args: _*)
}
else if (js.typeOf(value2) == "array" && key == "children")
mergeChildrenArrays(value1, value2)
else if (js.typeOf(value2) == "string" && key == "className")
mergeClassNames(value1, value2)
else if (js.typeOf(value2) == "object" && key == "style")
mergeStyleObjects(value1, value2)
else if (js.typeOf(value2) == "function" && key.startsWith("on"))
mergeEventHandlers(value1, value2)
else
value2
def apply(propsObjects: js.Array[js.Object]): js.Object =
Expand Down

0 comments on commit de86c1a

Please sign in to comment.