- 本文参加了由公众号@若川视野 发起的每周源码共读活动, 点击了解详情一起参与。
- 这是源码共读的第7期,链接:www.yuque.com/ruochuan12/…。
加入阅读源码活动好久了,但是一直没有开始,终于开始给自己立flag了,要每周都阅读至少1篇源码
今天从最简单的开始 validate-npm-package-name
这个包的作用是校验是否是合格的npm包名
命名规则有如下约束,在代码中也一一体现:
- package name length should be greater than zero
- all the characters in the package name must be lowercase i.e., no uppercase or mixed case names are allowed
- package name can consist of hyphens
- package name must not contain any non-url-safe characters (since name ends up being part of a URL)
- package name should not start with . or _
- package name should not contain any leading or trailing spaces
- package name should not contain any of the following characters: ~)('!*
- package name cannot be the same as a node.js/io.js core module nor a reserved/blacklisted name. For example, the following names are invalid:
-
- http
- stream
-
- node_modules
- favicon.ico
- package name length cannot exceed 214
代码整体来说比较简单易懂,这里就总结下心得体会吧:
- 针对多条件判断功能函数,要及时返回,不要嵌套过多的if else
- 学到了delete关键字的使用场景,以后在业务中要多注意下看看是否会用到
还需要了解的内容:
- 正则,看了千八百遍 还是记不住。。
- encodeURIComponent 的用法
存在的疑惑,希望有小伙伴能帮忙解答:
- 参考文章中的 builtins模块代码,是从哪里看到的?
- npm包名规则上,对scope是没有限制的吗?比如 @d*dd/ccc 能通过校验。是否有我遗漏的地方,不然这块看上去就不合适。
PS:
有价值的参考文章:juejin.cn/post/701204…