-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Undefined or missing elements parse as empty strings #110
Comments
this is expected behavior because xpath is returning empty string for that path. another reason is that i want the output to shape the same as the template. eg: i don't want to check for null object from output before accessing it. however, I can see lemme take a look into that. in the mean time, there's a workaround for this with the example here https://github.com/tuananh/camaro/blob/develop/examples/default-value.md |
Thanks for the reply @tuananh. I’ve seen that example before and wasn’t sure how it would be possible to have it return an actual |
my bad, it isn't really a fix for that. |
I need to think on this some more. What do you think to handle case like calling xpath function? like |
Hmm I'm not sure, I've thought about this a little also as I was browsing the code, trying to understand how it'd be possible. What if there was a special XPath function? Something like I'm not sure how possible it is to do that, and does kind of pollute the XPath API somewhat, but I've not yet thought of something better. At least with an approach like that, it would be possible to opt-in to receiving the |
I may lean into introducing a parse option , sth like below
but nothing concrete yet |
@tuananh any hints on how you would approach this? I’d be happy to try and contribute a PR if you could give some insights on your ideas to integrate the One thing I wonder about is how to tell if an object should be In any case, any pointers would be appreciated and I’d see if I can use those to make the PR. |
@kierangraham that would require you to look into my pugixml source code to check if an xpath node exists or not. and parsing the parse options of course. |
@kierangraham there may be also import { isArray, isObject, isUndefined, isNull, isNaN } from 'lodash';
export type Matcher<T = any> = (value: T) => boolean;
// Deeply remove matching values from an object or array
export const stripMatching = <T = any>(match: Matcher<T>) => {
const noMatch = (value: T) => !match(value);
const strip = <I = any, O = I>(data: I): O =>
isArray(data)
? (data.filter(noMatch).map(strip) as O)
: isObject(data)
? (Object.entries(data).reduce(
(res, [k, v]) => (match(v) ? res : { ...res, [k]: strip(v) }),
{},
) as O)
: (data as unknown as O);
return strip;
};
export const isFalsy = (value: any): value is false | null | undefined | '' | typeof NaN =>
value == null || value === false || value === '' || isNaN(value);
export const isNullish = (value: any): value is null | undefined | '' | typeof NaN =>
value == null || value === '' || isNaN(value);
export const stripFalsy = stripMatching(isFalsy);
export const stripNaN = stripMatching(isNaN);
export const stripNullish = stripMatching(isNullish);
export const stripNulls = stripMatching(isNull);
export const stripUndefined = stripMatching(isUndefined); |
When parsing some XML which has optional elements, the values get returned as an empty string.
Included are the example output that is received and the desired output, plus the code used to generate.
It would be greatly appreciated for some pointers on how to produce the desired output and in any case thanks for the work on the library.
Example Output
Desired Output
Code
The text was updated successfully, but these errors were encountered: