Diving straight in: there are a number of constraints around toString, which could carry different weight:
- Should be consistently (always or never) parseable by existing parse functions like JSON.parse and Number.parseFloat
- Should work seamlessly with sequence units
- Should be parseable by a custom parser
- Should be developer-readable
- Should build on top of existing serialization formats if possible
Any other constraints? Please comment below.
With that in mind, here are some possible syntaxes for these inputs:
- Unitless: { value: 1, unit: null }
- Meter: { value: 5, unit: "meter" }
- Sequence: { value: { pound: 7, ounce: 1 }, unit: "pound-and-ounce" }
| Name |
Unitless |
Meter |
Sequence |
| Stage 2 |
[1e+0 ~] |
[5e+0 meter] |
[7e+0 pound, 1e+0 ounce] |
| With IXDTF annotations |
1e+0[] |
5e+0[meter] |
7e+0,1e+0[pound-and-ounce] |
| Raw JSON |
{"value":1e+0,"unit":null} |
{"value":5e+0,"unit":"meter"} |
{"value":{"pound":7e+0,"ounce":1e+0},"unit":"pound-and-ounce"} |
| Nicer JS, A |
[{value:1e+0}] |
[{value:5e+0,unit:"meter"}] |
[{value:7e+0,unit:"pound"},{value:1e+0,unit:"ounce"}] |
| Nicer JS, B |
[1e+0] |
[{meter:5e+0}] |
[{pound:7e+0},{ounce:1e+0}] |
| Inspired by ISO 8601 P-string |
A1e+0 |
A5e+0meter |
A7e+0pound1e+0ounce |
| P-string with well-known abbreviations |
A1e+0 |
A5e+0M |
A7e+0L1e+0O |
| P-string with annotations |
A1e+0 |
A5e+0[meter] |
A7e+0[pound]1e+0[ounce] |
Please feel free to offer more ideas.
Diving straight in: there are a number of constraints around toString, which could carry different weight:
Any other constraints? Please comment below.
With that in mind, here are some possible syntaxes for these inputs:
Please feel free to offer more ideas.