1 | # Doc Syntax
|
2 | This file will cover every detail of the doc syntax, inspired in JSON and designed to be very concise and expressive.
|
3 |
|
4 | ## Basic example
|
5 | user:
|
6 | name: 'John'
|
7 | password: '123'
|
8 | The equivalent JSON would be: `{"user": {"name": "John", "password": "123"}}`
|
9 |
|
10 | ## Properties are eval'd
|
11 | item:
|
12 | name: 'Chocolate' + ' ' + 'Cake'
|
13 | price: (314/100).toFixed(2) // prices must be like '3.14'
|
14 | All property values will be eval'd as plain JS with some more global help functions (like `randomStr()`)
|
15 |
|
16 | ## Simple value
|
17 | ['sugar', 'milk']
|
18 | Evaluate a single JS expression and the obj value will be this result. This can create values that are not of type 'object', like:
|
19 |
|
20 | Math.random()
|
21 |
|
22 | This syntax can also be used in a subdoc:
|
23 |
|
24 | itemId:
|
25 | randomId()
|
26 |
|
27 | Is the same as:
|
28 |
|
29 | itemId: randomId()
|
30 |
|
31 | ## Simple arrays
|
32 | Small arrays can be written directly in pure JS:
|
33 |
|
34 | tags: ['light', 'pink']
|
35 | Or with a syntax inspired in mardown lists:
|
36 |
|
37 | tags:
|
38 | * 'light'
|
39 | * 'pink'
|
40 | Or an array as root:
|
41 |
|
42 | * 3
|
43 | * 14
|
44 | * 15
|
45 | ## Arrays of objects
|
46 | messages:
|
47 | * group: 'family'
|
48 | num: 2
|
49 | * group: 'work'
|
50 | num: 12
|
51 |
|
52 | ## Unordered arrays
|
53 | Act the same as an array in most cases, except matching does not considered the order of the elements. That is:
|
54 | set:
|
55 | @ 3
|
56 | @ 14
|
57 | @ 15
|
58 | matches `[15, 3, 14]`.
|
59 |
|
60 | ## Arrays of arrays
|
61 | * * 1
|
62 | * 2
|
63 | * * 3
|
64 | * 4
|
65 | Means `[[1, 2], [3, 4]]`
|
66 |
|
67 | ## Mixins
|
68 | Mixins are used to derive a similar object from a base one. Suppose `user = {name: 'John', pass: '123'}`. Then,
|
69 |
|
70 | user with pass: '1234'
|
71 |
|
72 | will create the object `{name: 'John', pass: '1234'}`
|
73 |
|
74 | user without name
|
75 |
|
76 | will create the object `{pass: '123'}`
|
77 |
|
78 | To add and remove multiple properties:
|
79 |
|
80 | user without name, pass; with
|
81 | age: 36
|
82 | token: randomStr(16)
|
83 |
|
84 | `without` must appear before `with`
|
85 |
|
86 | ### Paths
|
87 | Mixins are not restricted to altering properties, they can also add/remove entire paths. Suppose `order = {items: [{name: 'a', price: 60}, {name: 'b', price: 63}], price: 123}`. Then,
|
88 |
|
89 | order without items.price
|
90 |
|
91 | will create the object `{items: [{name: 'a'}, {name: 'b'}], price: 123}`
|
92 |
|
93 | order without items.0.name, price
|
94 |
|
95 | will create the object `{items: [{price: 60}, {name: 'b', price: 63}]}`
|
96 |
|
97 | order with items.ok: true
|
98 |
|
99 | will create the object `{items: [{name: 'a', price: 60, ok: true}, {name: 'b', price: 63, ok: true}], price: 123}`
|
100 |
|
101 | order.items without 0
|
102 |
|
103 | gives `[{name: 'b', price: 63}]`
|
104 |
|
105 | ## Keys
|
106 | All keys must be valid JS identifiers (contain only letters, numbers, _ and $) and not start with numbers (except array positions in paths like `items.0`).
|
107 |
|
108 | Otherwise a key can be escaped with quotes, like: `"a very -strange kèy"` or `'even worse\', y u do this?'`. An example of a path with escaped key: `user."1 strange key"`. **NOTE: THIS IS A DRAFT**, escaped keys in paths don't work yet |
\ | No newline at end of file |