-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add option to use dot attribute access #44
Comments
Hi @beasteers, |
Hey @beasteers and @bartbutenaers! Regarding this issue specifically: since names containing a dot, like |
Hi @m93a, |
That's so funny lol! I'm glad other people are thinking about this and that it seems like a solution is already quite feasible. Good luck on your exam as well (today!!) I hope it goes/went well. So just to document, it looks like this is the most basic solution here: compileExpression(expression, {
customProp: (key, get, data) => key.split('.').reduce((o, k) => o && o[k], data)
}) Here's a quick and dirty codepen to demo that it does indeed work https://codepen.io/beasteers/pen/WNOZodR?editors=1010 Thanks for the tip that this was possible! I agree that adding a util/flag is probably best (esp. considering you're doing some prototype-based attribute sanitizing and whatnot), but this is good enough for me for now esp. since I'm the only one controlling the queries atm |
Hey Bea, (PS: The finals were a success! Thank y'all for wishing me luck!) |
Published as The entire changelog is listed in #49 for the time being. I will add it to a more visible place before I publish To use the dot accessor, you'd write something like this: import {
compileExpression,
useDotAccessOperator
} from 'filtrex'
const expr = "foo.bar"
const fn = compileExpression(expr, {
customProp: useDotAccessOperator
});
fn({ foo: { bar: 42 } }) // → 42 And a more complete feature description can be seen here. |
This is great thanks so much! One more request - could we have an option where it won't throw an error, it will just return undefined (even for Currently I'm using this to query metadata across sensors and the data structure isn't always consistent. export function useDotAccessOperator(name, get, obj, type) {
// ignore dots inside escaped symbol
if (type === 'single-quoted')
return get(name)
const parts = name.split('.')
for (const propertyName of parts) {
if (hasOwnProperty(obj ?? {}, propertyName)) {
obj = obj[propertyName]
} else {
obj = undefined;
break;
}
}
return obj
} Edit: I guess this would be another way to define it import UnknownPropertyError from 'filtrex.js/errors';
export function useDotAccessOperatorFallback(name, get, obj, type) {
try {
return useDotAccessOperator(name, get, obj, type);
} catch (e) {
if(e instanceof UnknownPropertyError) { return undefined; }
throw e;
}
} I can always copy this into code that I need it in, but I just figured that it seems like a slightly more useful form. |
You're right that optional chaining is something that could be useful for everybody, I've added it as Since there is no straightforward way to combine If I should add more predefined prop functions, I'll probably think of some plugin system where things can be combined (similar to the way Rollup and Babel handle plugins), but for now this seems good enough. |
@m93a, |
Hi! I just want to say that I love this package and it's super useful for things like offloading dashboard search and filtering logic to config files so thank you!
My only hangup is that you have to do
y of x of config
instead ofconfig.x.y
which to me feels a bit confusing and counter-intuitive. And it makes it harder to teach others about how to use the expressions because it goes against how you access data in most other languages.I wonder if it'd be possible to add a filter compile option that allows you to enable dot attribute access? That way we could keep the default functionality, but allow for a more familiar
config.x.y
to access attributes (instead of key literals) if a user prefers.A headstart on a solution?
I'm not very familiar with language grammar definitions so I'm sure this isn't all right, but I'm hoping it's in the right direction.
Add
['\\.', 'return ".";'],
after this line?https://github.com/m93a/filtrex/blob/20a016d5c13527047520de4573eaeda98a49e69f/src/generateParser.js#L39
Replace this regex with:
'[a-zA-Z$_][a-zA-Z0-9$_]*'
https://github.com/m93a/filtrex/blob/20a016d5c13527047520de4573eaeda98a49e69f/src/generateParser.js#L44
Add
['left', '.'],
afterhttps://github.com/m93a/filtrex/blob/20a016d5c13527047520de4573eaeda98a49e69f/src/generateParser.js#L80
Add
['e . SYMBOL', code(['prop(', 1, ',', 3, ')'])],
(I think?)https://github.com/m93a/filtrex/blob/20a016d5c13527047520de4573eaeda98a49e69f/src/generateParser.js#L111
The text was updated successfully, but these errors were encountered: