DEV Community

Cover image for Introducing TypeLib JS - simplified type detection and debugging

Introducing TypeLib JS - simplified type detection and debugging

Schemetastic (Rodrigo) on February 07, 2024

If you want to see it in action, check out the project landing page at: typelib.schemetastic.com I do also have a promo short video about the proj...
Collapse
 
efpage profile image
Eckehard

I much appreciate your effort! Type checking is super important in Javascript and a nice lib could help to make things easier.

I like your coding style, it is very clear. But as type checks are used often and the lib is included in any project, the code should be thoroughly optimized for size and speed. Maybe arrow functions and a shorter names could save some bytes?

The following routine could be possibly faster and shorter:

var kindsList = [];
// Verify all the properties that are `true`, replace the word `is`, and push it to `kindsList`.
kindsProps.forEach(function (prop) {
     if (inst[prop]) kindsList.push(prop.toLowerCase().replace(/^is/, ''));
});
Enter fullscreen mode Exit fullscreen mode
  • RegEx-Replace can be very slow. slice(2) would do the same job
  • instead of forEach->push, you could use "filter":
console.log([1,2,3,4,5,6,7].filter(i => i>3))  // -> [ 4, 5, 6, 7 ]
Enter fullscreen mode Exit fullscreen mode

So, I suppose, your code could be like ths:

let kindsList = kindsProps.filter(prop => inst[prop]).map(p => p.slice(2).toLowerCase())
Enter fullscreen mode Exit fullscreen mode

I did not run this code, but maybe you can take this as an example.

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

Arrow functions? In a previous version they were detected, but I removed them from the spec... 3 reasons

  1. Couldn't find a satisfying regex (arrow functions can have comments, and be written in different ways, for example, you can even omit the parenthesis)
  2. The only method I know to detect them imply that you verify if it is a function, and it doesn't have a prototype (thanks AI)... but I thought that JS may in the future would add another type of function without a prototype and could break code.
  3. TypeLib is a lot about developer experience... commonly, when you need to verify a function is because you would use it as a callback, some devs prefer arrow functions, other normal... I've got to realize that having to differentiate between one or another could be confusing and time-consuming... #I'm-not-a-rapper ... but maybe? Haha

By the way, I'm glad that you reviewed my code and that you liked it, I'm surprised that someone took the time to read it and review it, It took a lot of efforts to write that code in that way. Thanks for your suggestion, I don't know how much using a regex like that could affect performance, but your suggestion is valid, I will consider that for a future version. Really, thanks a lot.

Collapse
 
efpage profile image
Eckehard

I was just curious to see, how you did all the testing...

Collapse
 
vaddijaswant profile image
Jaswant Vaddi

nice Idea

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

nice Comment 😉

Collapse
 
piko profile image
Piko

Great article!

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

Thanks Piko, I've noticed you have been producing great videos on YouTube!

Collapse
 
piko profile image
Piko

You are too kind! 🤗

Collapse
 
artydev profile image
artydev

Very useful, thank you

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

Thanks to you! If you are interested, next article will be posted on Monday!

Collapse
 
poetryofcode profile image
Poetry Of Code

Cool

Collapse
 
emmanuelayinde profile image
Emmanuel Ayinde

Thank you for sharing. I think I just found myself a new tool to add to my toolbox 😊

Collapse
 
schemetastic profile image
Schemetastic (Rodrigo)

Hey, your welcome! I'm glad you like it, I will be producing more tools and resources for developers soon!