Skip to content
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

Is there any type guards? #92

Open
hache9669 opened this issue Jan 25, 2023 · 1 comment
Open

Is there any type guards? #92

hache9669 opened this issue Jan 25, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@hache9669
Copy link

Is your feature request related to a problem? Please describe.
I imported plantuml-parser in my Next.js app, and I need some of type guards.
Now I just written a piece of type guards(only for what I need), and they are in my app project.

eg. When I render UML in browser,

  • classify between interface and class to show some icon
  • classify between methods and member variable to separate div tag

Describe the solution you'd like

  1. As far as I have been able to find in the repository, it does not appear that type guards are provided.
    If I am mistaken and type guards already exists in the project, please let me know.

  2. May I create a pull request to add the type guard I coded?
    It might help other users. (although it may not be necessary)
    Currently I have only created type guards for a few classes I need, but
    I consider that I can create type guards for all types defined in types.d.ts.

Here is an example of what I have created;

const isMethod = (maybeMethod: Object): maybeMethod is Method => {
  return (
    hasPrimitive(maybeMethod, 'name', 'string') &&
    hasPrimitive(maybeMethod, 'isStatic', 'boolean') &&
    hasStringLiteral(maybeMethod, 'accessor', isAccessor) &&
    hasPrimitive(maybeMethod, 'returnType', 'string') &&
    hasPrimitive(maybeMethod, '_arguments', 'string')
  );
};

const isMemberVariable = (maybeMemberVariable: Object): maybeMemberVariable is MemberVariable => {
  return (
    hasPrimitive(maybeMemberVariable, 'name', 'string') &&
    hasPrimitive(maybeMemberVariable, 'isStatic', 'boolean') &&
    hasStringLiteral(maybeMemberVariable, 'accessor', isAccessor) &&
    hasPrimitive(maybeMemberVariable, 'type', 'string')
  );
};

const isMember = (maybeMember: Object): maybeMember is Member => {
  return isMethod(maybeMember) || isMemberVariable(maybeMember);
};

// how to use
const renderClass = (c: Class) => {
  const variables = c.members.filter(member => isMemberVariable(member));
  const methods = c.members.filter(member => isMethod(member));

  return <div className="container">
    <h1>{c.title}</h1>
    {variables.length > 0 &&<div className="properties">
      <h2>properties</h2>
      <ul>{variables.map(v => <li>{v.name}</li>)}</ul>
    </div>}
    {methods.length > 0 &&<div className="methods">
      <h2>methods</h2>
      <ul>{methods.map(m => <li>{m.name}</li>)}</ul>
    </div>}
  </div>
}
@hache9669 hache9669 added the enhancement New feature or request label Jan 25, 2023
@Enteee
Copy link
Owner

Enteee commented Feb 5, 2023

Hello @hache9669 , and thank you for this issue.
You are right. There are no such type guards provided for any types in this project.

If you have a set of helpful typeguards and are willing to extend those to cover all types in this project. I am more than happy to review and merge a pull request for this 👍 . I think the only point to discuss would be how we can easily integrate testing for those guards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants