-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected path behavior #128
Comments
Preferably, there would be some way to escape the value so that decimals aren't split. Javascript's native implementation, gun.path("[0.123]") Maybe we could have floats coerce into a string like that under the hood, too... |
|
One could pass all keys through https://github.com/deanlandolt/bytewise by default, |
@thoka - Thank you so much for all of your pointers and suggestions! I've added a "Potentially Unexpected Behavior" section to the path documentation and @amark will also be adding a console notification. The current implementation is the way we want the core to work, but we would love some extensions that provide alternative approaches for gun users. Until extensions are coded, our goal is to ensure users know why the system is behaving the way it is. |
There is a potentially better solution here, where we normalize everything to an array. If it's not an array and a string, then we split it on periods, turning it into an array. This would mean that only strings are split, preventing the odd behavior with floats or other data types. By consequence, you could have a bracket like syntax by passing an array instead of a string. Since it's wrapped in an array, no implicit string splitting is done, finally allowing you to use dots in your path names. Normal Javascript var obj = {
'10.15.20': 'data'
}
obj['10.15.20'] // 'data' Gun syntax var obj = gun.get('obj')
// brackety syntax
obj.path(['10.15.20']) // path('10.15.20') Whereas if we wanted to split on the period, we could explicitly pass a single string: // or the usual string split
obj.path('10.15.20') // path(10).path(15).path(20) Here's the code that could enable that: var gun = this;
// if it's a string, not an array...
if (typeof input === 'string') {
// split on dot notation
input = input.split('.');
}
// otherwise it's not a string,
// but not an array either
if (!(input instanceof Array)) {
// turn it into an array
input = [input];
}
// if there's more than one item in
// the array, then call `.path` on each item.
if (input.length > 1) {
input.forEach(function (path) {
gun = gun.path([path]);
});
return gun;
}
// do normal `.path` things below I'd really like to see something like this in the next release of gun 😀 |
Path has now become a wrapper for |
If you pass a string into path, the decimal will break it into two separate
path
calls. This is not always the desired behavior, especially when considering floating point numbers:set
is never called because the float isString
ed, and then it reads aspath(0).path(123)
, where0
is not defined, leaving the rest of the chain unfulfilled. In instances like this, there should at least be some log message that warns you of using floating point numbers.The text was updated successfully, but these errors were encountered: