Skip to content

Is there any type guards? #92

@hache9669

Description

@hache9669

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>
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions