util
- URL
[Doc]
Query Strings[Doc]
Utilities[Basic]
Regex
Want to know more? Try this:
Array(range).fill(0)
.map((_, i) => String.fromCharCode(i))
.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:
const qs = require('qs');
let arr = [1,2,3,4];
let str = qs.stringify({arr});
console.log(str); // arr%5B0%5D=1&arr%5B1%5D=2&arr%5B2%5D=3&arr%5B3%5D=4
console.log(decodeURI(str)); // 'arr[0]=1&arr[1]=2&arr[2]=3&arr[3]=4'
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…
const fs = require('fs');
const path = require('path');
for (let item of fs.readdirSync(dir)) {
let filepath = path.join(dir, item);
try {
let fd = fs.openSync(filepath, 'r');
let flag = fs.fstatSync(fd).isDirectory();
fs.close(fd); // TODO
if (flag) {
res.push(...traversal(filepath));
} else {
res.push(filepath);
}
} catch(err) {
if (err.code === 'ENOENT' && // can not open link file
!!fs.readlinkSync(filepath)) { // if it is a link file
res.push(filepath);
} else {
console.error('err', err);
}
}
}
return res.map((file) => path.basename(file));
}
Of course you can also use Oh my glob:
```javascript
const glob = require(“glob”);