Search code examples
macrossweet.js

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.


Solution

  • 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" }