Skip to content
This repository has been archived by the owner on May 2, 2021. It is now read-only.

Commit

Permalink
Change _repeat_ to use _index
Browse files Browse the repository at this point in the history
  • Loading branch information
timdp committed Apr 8, 2018
1 parent 34340f4 commit 772bf67
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 12 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ Options:
* `index`: additional identifier for the current index

You would mainly use `as` and `index` in nested structures, as each structure
will override the parent's `_` reference.
will override the parent's `_` and `index` references.

#### Filter

Expand All @@ -282,19 +282,23 @@ Options:
* `index`: additional identifier for the current index

You would mainly use `as` and `index` in nested structures, as each structure
will override the parent's `_` reference.
will override the parent's `_` and `_index` references.

#### Repeat

If you just need to repeat an expression a fixed number of times without
providing an array as input, you can use the `_repeat_` structure. Like `_map_`
and `_filter_`, it produces an array that can be passed to the `<-` or the `++`
operator.
providing an array as input, you can use the `_repeat_` structure. Think of it
as shorthand for `_map_` with an array from 0 to the number of iterations minus
one. The `_index` variable references the current index.

Options:

* `_repeat_`: number of iterations
* `body`: projection expression
* `index`: additional identifier for the current index

You would mainly use `index` in nested structures, as each structure will
override the parent's `_index` reference.

#### If

Expand Down
2 changes: 1 addition & 1 deletion demo/json/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"++": {
"_repeat_": 5,
"body": {
"key${_}": "value${_}"
"key${_index}": "value${_index}"
}
},
"users": {
Expand Down
2 changes: 1 addition & 1 deletion demo/yaml/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 1.2.3
++:
_repeat_: 5
body:
"key${_}": "value${_}"
"key${_index}": "value${_index}"
users:
<-:
_map_: "users"
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ controls.filter = (arg, { as, index, where }, data) => {
]
}

controls.repeat = (arg, { body }, data) => {
controls.repeat = (arg, { body, index }, data) => {
const times = (typeof arg === 'string')
? evaluateExpression(arg, data)
: arg
return [
true,
Array.apply(null, { length: times })
.map((_, i) => render(body, Object.assign({}, data, { _: i })))
.map((_, idx) => {
const augmentedData = Object.assign({}, data, { _index: idx })
if (index != null) {
augmentedData[index] = idx
}
return render(body, augmentedData)
})
]
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/repeat-expression/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"++": {
"_repeat_": "times",
"body": {
"item${_}": true
"item${_index}": true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/repeat-index-key-value/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"++": {
"_repeat_": 3,
"body": {
"key${_}": "value${_}"
"key${_index}": "value${_index}"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/repeat-key-interpolated/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"++": {
"_repeat_": 3,
"body": {
"item${_}": true
"item${_index}": true
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/snapshots/main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,27 @@ Generated by [AVA](https://ava.li).
},
}

## repeat-nested

> Snapshot 1
{
outer: {
inner0: [
'value:0:0',
'value:0:1',
],
inner1: [
'value:1:0',
'value:1:1',
],
inner2: [
'value:2:0',
'value:2:1',
],
},
}

## template-strings

> Snapshot 1
Expand Down
Binary file modified test/snapshots/main.js.snap
Binary file not shown.

0 comments on commit 772bf67

Please sign in to comment.