nv-random-path 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db +0 -0
- package/index.js +51 -0
- package/package.json +16 -0
package/db
ADDED
|
Binary file
|
package/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const _path = require("path");
|
|
2
|
+
const _fs = require("fs");
|
|
3
|
+
const _pako = require("pako");
|
|
4
|
+
const DB = JSON.parse(_pako.ungzip(_fs.readFileSync(_path.join(__dirname,"db")),{to:"string"}))
|
|
5
|
+
/**
|
|
6
|
+
* 生成随机路径
|
|
7
|
+
* @param {number} max_depth - 最大深度
|
|
8
|
+
* @param {number} min_depth - 最小深度
|
|
9
|
+
* @returns {string} 随机路径
|
|
10
|
+
*/
|
|
11
|
+
const _rand = (max_depth = 16, min_depth = 1) => {
|
|
12
|
+
const dir_names = DB.dir; // 字符串数组
|
|
13
|
+
const file_names = DB.leaf; // 字符串数组
|
|
14
|
+
const exts = DB.ext; // 字符串数组 不带前面的 "."
|
|
15
|
+
|
|
16
|
+
// 1. 确定深度
|
|
17
|
+
const depth = Math.floor(Math.random() * (max_depth - min_depth + 1)) + min_depth;
|
|
18
|
+
|
|
19
|
+
// 2. 随机选择目录
|
|
20
|
+
const randomDirs = [];
|
|
21
|
+
for (let i = 0; i < depth; i++) {
|
|
22
|
+
const randomIndex = Math.floor(Math.random() * dir_names.length);
|
|
23
|
+
randomDirs.push(dir_names[randomIndex]);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const is_dir = Math.random() >= 0.5;
|
|
27
|
+
|
|
28
|
+
if (is_dir) {
|
|
29
|
+
// 生成目录路径: /<ele>/<ele>.../<ele>/
|
|
30
|
+
return "/" + randomDirs.join("/") + "/";
|
|
31
|
+
} else {
|
|
32
|
+
// 生成文件路径: /<ele>/<ele>.../<ele>/<file>.<ext>
|
|
33
|
+
const randomFileIndex = Math.floor(Math.random() * file_names.length);
|
|
34
|
+
const randomExtIndex = Math.floor(Math.random() * exts.length);
|
|
35
|
+
|
|
36
|
+
const fileName = file_names[randomFileIndex];
|
|
37
|
+
const fileExt = exts[randomExtIndex];
|
|
38
|
+
|
|
39
|
+
return "/" + randomDirs.join("/") + "/" + fileName + "." + fileExt;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
module.exports = (mx=8,mn=1)=>{
|
|
45
|
+
let p = _rand(mx,mn);
|
|
46
|
+
while(Buffer.from(p).length>4096) {
|
|
47
|
+
p = _rand(mx,mn);
|
|
48
|
+
}
|
|
49
|
+
return p;
|
|
50
|
+
}
|
|
51
|
+
module.exports.rand = _rand;
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nv-random-path",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"author": "",
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"description": "",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"nv-random-util": "^0.0.2",
|
|
13
|
+
"pako": "^2.1.0"
|
|
14
|
+
},
|
|
15
|
+
"files":["db"]
|
|
16
|
+
}
|