-
Notifications
You must be signed in to change notification settings - Fork 52
Default value #5
Comments
I actually originally supported an optional third argument, Here are the different potential behaviors for
Then, there's the question of whether or not we should return The difference between these are pretty subtle and easy to get mixed up. In order to avoid confusion, it would be better for callers to explicitly expand the defaulting logic inline. Also, we should think of Happy to discuss further, but closing this for now. Thanks for the proposal, though! |
In @ppoliani 's example: const DEFAULT_FRIENDS = [{ name: "Pavlos" }]
idx(props, _ => _.user.friends[0].friends, DEFAULT_FRIENDS) There are five chances to get props == undefined
props.user == undefined
props.user.friends == undefined
props.user.friends[0] == undefined
props.user.friends[0].friends == undefined We can pass the fourth argument about how to handle
LOOKUPWe can define default values for levels: const DEFAULT_FRIENDS = [{ name: "Pavlos" }]
const defaultValues = {
0: [], // for level 0 to level 3
4: DEFAULT_FRIENDS // for level 4
}
const result = idx(props, _ => _.user.friends[0].friends, defaultValues, 'LOOKUP') So we can get different default values for different undefined levels: props == null
? [] // level 0 default value
: props.user == null
? [] // level 1 default value
: props.user.friends == null
? [] // level 2 default value
: props.user.friends[0] == null
? [] // level 3 default value
: props.user.friends[0].friends == null
? [{ name: "Pavlos" }] // level 4 default value
: props.user.friends[0].friends The // now we only provide level 4 default value
const defaultValues = {
4: DEFAULT_FRIENDS
}
const result = idx(props, _ => _.user.friends[0].friends, defaultValues, 'LOOKUP')
// results
props == null
? undefined // level 0 default value
: props.user == null
? undefined // level 1 default value
: props.user.friends == null
? undefined // level 2 default value
: props.user.friends[0] == null
? undefined // level 3 default value
: props.user.friends[0].friends == null
? [{ name: "Pavlos" }] // level 4 default value
: props.user.friends[0].friends EXACTLYWhen we use const DEFAULT_FRIENDS = [{ name: "Pavlos" }]
const defaultValues = {
0: [], // only for level 0
4: DEFAULT_FRIENDS // only for level 4
}
const result = idx(props, _ => _.user.friends[0].friends, defaultValues, 'EXACTLY') props == null
? [] // level 0 default value
: props.user == null
? undefined // level 1 default value
: props.user.friends == null
? undefined // level 2 default value
: props.user.friends[0] == null
? undefined // level 3 default value
: props.user.friends[0].friends == null
? [{ name: "Pavlos" }] // level 4 default value
: props.user.friends[0].friends It won't look up the default value in other levels. ANYWe provide only a default value for all const DEFAULT_FRIENDS = [{ name: "Pavlos" }]
const result = idx(props, _ => _.user.friends[0].friends, DEFAULT_FRIENDS, 'ANY') props == null
? [{ name: "Pavlos" }] // level 0 default value
: props.user == null
? [{ name: "Pavlos" }] // level 1 default value
: props.user.friends == null
? [{ name: "Pavlos" }] // level 2 default value
: props.user.friends[0] == null
? [{ name: "Pavlos" }] // level 3 default value
: props.user.friends[0].friends == null
? [{ name: "Pavlos" }] // level 4 default value
: props.user.friends[0].friends If you don't pass the fourth argument to const DEFAULT_FRIENDS = [{ name: "Pavlos" }]
const result = idx(props, _ => _.user.friends[0].friends, DEFAULT_FRIENDS) // Use 'ANY' strategy How about this proposal? |
@xareelee Thanks for the detailed proposal here and in claudepache/es-optional-chaining#16. However, I personally think it overcomplicates the problem that the optional chaining / existential operator seeks to solve. Since |
What do you think about having a default value in case there is an error while accessing a property.
Essentially, the function signature might be like:
function idx<Ti, Tv, Td>(input: Ti, accessor: (input: Ti) => Tv, defaultValue: ?Td): ?Tv
And then we could use it as:
Which can be transformed to
The text was updated successfully, but these errors were encountered: