S
SolidJSβ€’7mo ago
innards7

Question about store `range` update API

I've been trying to update the docs for the store page and I noticed an API decision I found surprising. The path syntax for specifying a range of indices to update in an array { from: 3, to: 7 } is inclusive for both start and end. Most similar JS APIs I can think of (.slice for example) are start inclusive, end exclusive. Does anyone know why the index range API works this way? I thought it might be worth calling out in the docs, but I'm also just curious even if it's not.
6 Replies
bigmistqke
bigmistqkeβ€’7mo ago
What does inclusive mean in this context?
REEEEE
REEEEEβ€’7mo ago
I think it means that it will target index 3 and 7 and everything in between while .slice will target index 3 and everything until 7 Unless you mean something else πŸ˜…
innards7
innards7OPβ€’7mo ago
Yes, that's exactly right. [3, 7] updates 3, 4, 5, 6, and 7, whereas something like .slice would take 3, 4, 5, and 6. As a concrete example of what that means, if you update { from: x, to: array.length } you get TypeError: Cannot read properties of undefined since it goes one past the final index of the array.
bigmistqke
bigmistqkeβ€’7mo ago
Gotcha! Thanks for the explanation!
peerreynders
peerreyndersβ€’7mo ago
It's probably worth noting that the spec uses the parameter names start and end for slice. end is considered the index directly following the last inclusive index. Also slice is a copying operation. splice is the mutating operation and it used start and deleteCount, not start, end and therefore side stepping the entire half-open, inclusive debate; opting instead to be clear about the (maximum) number of elements that will be deleted. So fortunately the names from and to were used instead. The existence of the optional by property probably tipped the balance in favour of the inclusive range because it focuses entirely on the elements to be impacted rather than having to figure out β€œthe first index that won't be impacted (anymore)”
// Ex. update('data', { from: 3, to: 12, by: 2 }, 'label', l => l + ' !!!');
const { from = 0, to = current.length - 1, by = 1 } = part;
for (let i = from; i <= to; i += by) {
updatePath(current, [i].concat(path), traversed);
}
return;
// Ex. update('data', { from: 3, to: 12, by: 2 }, 'label', l => l + ' !!!');
const { from = 0, to = current.length - 1, by = 1 } = part;
for (let i = from; i <= to; i += by) {
updatePath(current, [i].concat(path), traversed);
}
return;
innards7
innards7OPβ€’7mo ago
I see, thanks for the info!
Want results from more Discord servers?
Add your server