question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feat: Include simple arithmetic in JSON

See original GitHub issue

As a user when I have following JSON file:

{
    "base": {
        "value": 8
    },
    "spacing-unit": {
        "magnitude": "3",
        "value": "{base.value} * {spacing-unit.magnitude.value}"
    }
}

I would like the result:

{
    "base": {
        "value: 8
    },
    "spacing-unit": {
        "magnitude: "3"
        "value: 24
    }
}

Reading through CONTRIBUTING.md, it’s noted “Do not mutate property names or values in a format.”

Let me know if this feature is possible. Else, any chance of myself or the style-dictionary time including some sort of transformer, for this behaviour

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
showeredcommented, May 15, 2020

Perhaps consider using Javascript instead of JSON. It gives you some flexibility in your code rather than having to rely on style-dictionary to do the magic.

For example:

const buttonBaseSize = 10;

module.exports = {
  size: {
    button: {
      base: {
        height: { value: buttonBaseSize },
      },
      large: {
        height: { value: buttonBaseSize * 2 },
      }
    }
  }
}

You might run in to issues with brand/platform overrides, but this is the best I can suggest right now!

1reaction
tonyjwaltcommented, May 9, 2020

@danilovaz, your question references CSS custom properties.

Are those also tokens? Do you want the references maintained in your output?

It seems like what you want is this:

{
    "test": {
        "value": "calc({size.icon.small.value} * {size.icon.proportion.value})"
    }
}

which, assuming those references are also tokens, should generate this token:

test: calc(16*10)

or with a CSS-deep format (does not come with style dictionary currently, but could be extended) you could get this token:

test: calc(var(--size-icon-small) * var(--size-icon-proportion));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Arithmetic operations in JSON elements - IBM
Example Description Expression Result Addition Basic addition /attr1 + /attr2 5 Subtraction Basic subtraction /attr1 ‑ /attr2 ‑3 Multiplication Basic multiplication /attr1 * /attr2 8
Read more >
Working with JSON data in PowerShell - Scripting Blog
So there you have it, working with JSON data can be easy once you turn it into PowerShell objects. Hopefully this helps you...
Read more >
Is it possible to do expressions/calculations in json?
No, it isn't possible to do math or any kind of expression in JSON as JSON is just a ...
Read more >
Indexing, Querying, and Full-Text Search of JSON Documents ...
Let's look at another example, one where you have a large JSON, but only require a subpart of that document in your application....
Read more >
The Book of Leaves: A Beautiful Stop Motion Film Featuring ...
If I didn't have the perfect shape, it meant I just had to collect more leaves. Above, you can see the result of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found