-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Object.transformFields/transformValues
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/scala/com/github/mdr/mash/ns/core/objectClass/TransformFieldsMethod.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.mdr.mash.ns.core.objectClass | ||
|
||
import com.github.mdr.mash.evaluator.ToStringifier | ||
import com.github.mdr.mash.functions.{ BoundParams, MashMethod, Parameter, ParameterModel } | ||
import com.github.mdr.mash.ns.core.ObjectClass | ||
import com.github.mdr.mash.runtime.{ MashObject, MashString, MashValue } | ||
|
||
object TransformFieldsMethod extends MashMethod("transformFields") { | ||
|
||
object Params { | ||
val F = Parameter( | ||
nameOpt = Some("f"), | ||
summaryOpt = Some("Function used to transform field names of the object"), | ||
descriptionOpt = Some( | ||
"""The function may take up to two arguments. If it takes only one argument, the field name is provided. | ||
| If takes two, the field name and value are provided.""".stripMargin)) | ||
} | ||
|
||
import Params._ | ||
|
||
val params = ParameterModel(Seq(F)) | ||
|
||
def apply(target: MashValue, boundParams: BoundParams): MashObject = { | ||
val obj = target.asInstanceOf[MashObject] | ||
boundParams.validateFunction1Or2(F) match { | ||
case Left(f) ⇒ | ||
MashObject.of(obj.immutableFields.map { case (name, value) ⇒ | ||
ToStringifier.stringify(f(MashString(name))) -> value | ||
}) | ||
case Right(f) ⇒ | ||
MashObject.of(obj.immutableFields.map { case (name, value) ⇒ | ||
ToStringifier.stringify(f(MashString(name), value)) -> value | ||
}) | ||
} | ||
} | ||
|
||
override def typeInferenceStrategy = ObjectClass | ||
|
||
override def summaryOpt: Option[String] = Some("Return a new object with the field names transformed") | ||
|
||
override def descriptionOpt = Some( | ||
"""Examples: | ||
| { foo: 3, bar: 4 }.transformFields (.toUpper) # { FOO: 3, BAR: 4 }""".stripMargin) | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/scala/com/github/mdr/mash/ns/core/objectClass/TransformValuesMethod.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.github.mdr.mash.ns.core.objectClass | ||
|
||
import com.github.mdr.mash.functions.{ BoundParams, MashMethod, Parameter, ParameterModel } | ||
import com.github.mdr.mash.ns.core.ObjectClass | ||
import com.github.mdr.mash.runtime.{ MashNumber, MashObject, MashString, MashValue } | ||
|
||
object TransformValuesMethod extends MashMethod("transformValues") { | ||
|
||
object Params { | ||
val F = Parameter( | ||
nameOpt = Some("f"), | ||
summaryOpt = Some("Function used to transform values of the object"), | ||
descriptionOpt = Some( | ||
"""The function may take up to two arguments. If it takes only one argument, the value is provided. | ||
| If takes two, the field name and value are provided.""".stripMargin)) | ||
} | ||
|
||
import Params._ | ||
|
||
val params = ParameterModel(Seq(F)) | ||
|
||
def apply(target: MashValue, boundParams: BoundParams): MashObject = { | ||
val obj = target.asInstanceOf[MashObject] | ||
boundParams.validateFunction1Or2(F) match { | ||
case Left(f) ⇒ MashObject.of(obj.immutableFields.mapValues(f)) | ||
case Right(f) ⇒ MashObject.of(obj.immutableFields.map { case (name, value) ⇒ name -> f(MashString(name), value) }) | ||
} | ||
} | ||
|
||
override def typeInferenceStrategy = ObjectClass | ||
|
||
override def summaryOpt: Option[String] = Some("Return a new object with the values transformed") | ||
|
||
override def descriptionOpt = Some( | ||
"""Examples: | ||
| { foo: 3, bar: 4 }.transformValues (n => n * n) # { foo: 9, bar: 16 }""".stripMargin) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters