Best way to structure helpers functions in NodeJS

后端 未结 2 1965
清歌不尽
清歌不尽 2020-12-13 00:43

I am trying to build a set of utils for my NodeJS project. These helpers will include: text utils (like substringing, console logging etc.), and more specific helpers like p

2条回答
  •  伪装坚强ぢ
    2020-12-13 01:02

    You can see a utils library example with lodash.

    Lodash is an utility lib like underscorejs. This library have file sustem structure like your.

    It divides the functions in categories. Each category is a folder with an index.js file that includes into a namespace (literal object) each functions for that category!

    Lodash/
       Objects/
           Function1.js
           Functions2.js
           ....
           Index.js
       Array/
           Function1.js
           ...
           Index.js
    

    Then in your code you can do this:

    var objectsUtils = require("lodash/objects");
    var foreach = require("lodash/array/each");
    

    You can create a similar file system structure in order to have more flexibility. You can require the entire lib, only one namespace or a single function.

    This is better for performance, because you use only what you need and have a memory usage gain.

提交回复
热议问题