util

    • URL
    • [Doc] Query Strings
    • [Doc] Utilities
    • [Basic] Regex

    Want to know more? Try this:

    1. Array(range).fill(0)
    2. .map((_, i) => String.fromCharCode(i))
    3. .map(encodeURI)

    Try to set the range to 255 first (doge.

    A query string is the part of a URL referring to the table above. Node.js provides a module called querystring.

    Method Description
    .parse(str[, sep[, eq[, options]]]) Parse a query string into a json object
    .unescape(str) Inner method used by .parse(). It is exported primarily to allow application code to provide a replacement decoding implementation if necessary
    .stringify(obj[, sep[, eq[, options]]])

    Converts a json object to a query string|
    |.escape(str)|Inner method used by .stringify(). It is exported primarily to allow application code to provide a replacement percent-encoding implementation if necessary.|

    So far, the Node.js built-in querystring does not support for the deep structure:

    1. const qs = require('qs');
    2. let arr = [1,2,3,4];
    3. let str = qs.stringify({arr});
    4. console.log(str); // arr%5B0%5D=1&arr%5B1%5D=2&arr%5B2%5D=3&arr%5B3%5D=4
    5. console.log(decodeURI(str)); // 'arr[0]=1&arr[1]=2&arr[2]=3&arr[3]=4'
    6. console.log(qs.parse(str)); // { arr: [ '1', '2', '3', '4' ] }

    You can pass arr Array to the server vir https://your.host/api/?arr[0]=1&arr[1]=2&arr[2]=3&arr[3]=4.

    In v4.0.0 or later, util.is*() is not recommended and deprecated. Maybe it is because that maintaining the library is thankless and there are so many popular libraries. The following is the list:

    • util.debug(string)
    • util.isArray(object)
    • util.isBoolean(object)
    • util.isBuffer(object)
    • util.isDate(object)
    • util.isError(object)
    • util.isFunction(object)
    • util.isNull(object)
    • util.isNullOrUndefined(object)
    • util.isNumber(object)
    • util.isObject(object)
    • util.isPrimitive(object)
    • util.isRegExp(object)
    • util.isString(object)
    • util.isSymbol(object)
    • util.isUndefined(object)
    • util.log(string)
    • util.print([…strings])
    • util.puts([…strings])
    • util._extend(target, source)

    Most of them can be used as an interview to ask how to implement.

    util.inherits

    Collecting…

    Awesome Node.js

    1. const fs = require('fs');
    2. const path = require('path');
    3. for (let item of fs.readdirSync(dir)) {
    4. let filepath = path.join(dir, item);
    5. try {
    6. let fd = fs.openSync(filepath, 'r');
    7. let flag = fs.fstatSync(fd).isDirectory();
    8. fs.close(fd); // TODO
    9. if (flag) {
    10. res.push(...traversal(filepath));
    11. } else {
    12. res.push(filepath);
    13. }
    14. } catch(err) {
    15. if (err.code === 'ENOENT' && // can not open link file
    16. !!fs.readlinkSync(filepath)) { // if it is a link file
    17. res.push(filepath);
    18. } else {
    19. console.error('err', err);
    20. }
    21. }
    22. }
    23. return res.map((file) => path.basename(file));
    24. }

    Of course you can also use Oh my glob:

    ```javascript
    const glob = require(“glob”);