-
Notifications
You must be signed in to change notification settings - Fork 24
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
Returning null for unrecognized urls #52
Conversation
4c17581
to
a279243
Compare
Please merge this, the last change kills me. |
@4lejandrito Thanks for this! I have been thinking a lot more about this lately. So I appreciate the help. A couple of things to clarify though:
Your PR only covers this case when the url is not recognized as a youtube, vine, videopress or vimeo url, for example: const video = getVideoId('https://not.supported/video/url');
// => null but in cases where it can still detect that the url belongs to one of these services youtube, vine, videopress or vimeo, then you would still be returning test('returns null for unrecognized vimeo urls', t => {
t.is(fn('https://player.vimeo.com/unknown').service, 'vimeo'); // passes
t.is(fn('https://player.vimeo.com/unknown').id, null); // fails with AssertionError: undefined === null
}); So from your proposed changes to the types: export default function getVideoId(
url: string
): {
id?: string;
service?: "youtube" | "vimeo" | "vine" | "videopress";
}; It could make sense to allow either/or the And then there is this case to consider when neither the service or id can be found, do we return: {
id: null,
service: null
} Or simply return These different cases can confuse things on the receiving end of the function, because we would need to account for either null, in place of an object, or null/undefined values if the object exists. Trying to find the best way to handle all cases is why I opened this discussion: #47 Happy to keep discussing this. |
@4lejandrito my thoughts on the type defs are found in the discussion here: #47 (comment) |
a279243
to
9070669
Compare
Hi @radiovisual, I push forced with the changes discussed in the thread. Regards! |
This is specially usefull for typescript use. With the current typings I get a type error when trying to do things like: const {id = null, service = null} = getVideoId(url) const videoId = getVideoId(url)?.id const videoService = getVideoId(url)?.service
9070669
to
2161f23
Compare
Awesome, thanks! ❤️ I will merge this and publish a new version soon |
Version |
Is this a feature, improvement or a bug fix?
Improvement.
If you changed any JavaScript files, did you write unit tests?
Yes.
Please describe (in detail) the solution(s) provided by your Pull Request
This is specially useful for Typescript use. With the current typings I get a type error when trying to do things like:
Also I believe
null
is a better return for unsupported URLs than an empty object.An alternative to this would be to modify just the typings to:
But that wouldn't be entirely accurate because it would imply that either the
id
or theservice
can benull
orundefined
independently. My proposal ensures that when the url is recognized both are defined.