Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/runtime/internal/keyed_each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,18 @@ export function update_keyed_each(old_blocks, dirty, get_key, dynamic, ctx, list
}

export function validate_each_keys(ctx, list, get_context, get_key) {
const keys = new Set();
const keys = new Map();
for (let i = 0; i < list.length; i++) {
const key = get_key(get_context(ctx, list, i));
if (keys.has(key)) {
throw new Error('Cannot have duplicate keys in a keyed each');
let value = '';
try {
value = `with value '${String(key)}' `;
} catch (e) {
// can't stringify
}
throw new Error(`Cannot have duplicate keys in a keyed each: Keys at index ${keys.get(key)} and ${i} ${value}are duplicates`);
}
keys.add(key);
keys.set(key, i);
}
}
2 changes: 1 addition & 1 deletion test/runtime/samples/keyed-each-dev-unique/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export default {
dev: true
},

error: 'Cannot have duplicate keys in a keyed each'
error: 'Cannot have duplicate keys in a keyed each: Keys at index 0 and 3 with value \'1\' are duplicates'
};