Skip to content

Commit feb1382

Browse files
committed
Pass standard “Array.map” arguments to “repeat”.
The “repeat” directive is just a shim to create arrays of _entries_. We may remove this from the interface in the future, but we do need it to function as expected in the interim! Most callers rely on just the first “value” argument, but there’s no good reason not to expose the `(value, index, array)`-triple that a standard callback would receive from “Array.map”.
1 parent bae7dab commit feb1382

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

test/test-template-engine.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,38 @@ describe('html updaters', () => {
12411241
assert(container.querySelector('#c').textContent === 'fuzz');
12421242
container.remove();
12431243
});
1244+
1245+
it('repeat callbacks are provided args from underlying “.map” call', () => {
1246+
// The identify callback is written in a bizarre way to test that all the
1247+
// expected function arguments are actually passed in. If they weren’t you
1248+
// would get duplicated key errors or undefined key errors.
1249+
const getTemplate = ({ items }) => {
1250+
return html`
1251+
<div id="target">
1252+
${repeat(items, (_, index, array) => array?.[index]?.id, (_, index, array) => {
1253+
return html`<div id="${array?.[index]?.id}" class="item">${array?.[index]?.id}</div>`;
1254+
})}
1255+
</div>
1256+
`;
1257+
};
1258+
const container = document.createElement('div');
1259+
document.body.append(container);
1260+
render(container, getTemplate({ items: [{ id: 'foo' }, { id: 'bar'}, { id: 'baz' }] }));
1261+
const foo = container.querySelector('#foo');
1262+
const bar = container.querySelector('#bar');
1263+
const baz = container.querySelector('#baz');
1264+
assert(container.querySelector('#target').childElementCount === 3);
1265+
assert(!!foo);
1266+
assert(!!bar);
1267+
assert(!!baz);
1268+
assert(container.querySelector('#target').children[0] === foo);
1269+
assert(container.querySelector('#target').children[1] === bar);
1270+
assert(container.querySelector('#target').children[2] === baz);
1271+
assert(foo.textContent === 'foo');
1272+
assert(bar.textContent === 'bar');
1273+
assert(baz.textContent === 'baz');
1274+
container.remove();
1275+
});
12441276
});
12451277

12461278
describe('updater errors', () => {

x-template.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ class TemplateEngine {
126126
throw new Error(`Unexpected repeat callback "${callback}" provided, expected a function.`);
127127
}
128128
return identify
129-
? items.map(item => [identify(item), callback(item)])
130-
: items.map(item => callback(item)); // Just a basic array.
129+
? items.map((...args) => [identify(...args), callback(...args)])
130+
: items.map((...args) => callback(...args)); // Just a basic array.
131131
}
132132

133133
// Deprecated. Will remove in future release.

0 commit comments

Comments
 (0)