Ethan Kent
Ethan Kent

Reputation: 531

Sweet.js: Possible to expand a JS object into a JS object?

Is it possible to use Sweet.Js to expand

{ "foo": "bar" }

to

{ "bar": "foo" }

for example?

My naïve attempt here doesn’t work and I don’t see an example in the documentation.

Thanks.

Upvotes: 0

Views: 89

Answers (1)

timdisney
timdisney

Reputation: 5337

The trick is that : has special meaning in patterns so you need to escape it with $[:]

macro bar {
  rule { {$x $[:] $y} } =>  { {$y: $x} }
}

var o = bar { "bax": "quux" }

Upvotes: 1

Related Questions