grofc_utils 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/generate.exports.js +51 -0
- package/index.js +5 -0
- package/package.json +45 -0
- package/readme.md +4 -0
- package/src/array.js +22 -0
- package/src/guard.js +564 -0
- package/src/missingData.js +65 -0
- package/src/random.js +372 -0
- package/src/string.js +24 -0
- package/tsconfig.json +112 -0
- package/types/index.d.ts +5 -0
- package/types/src/array.d.ts +7 -0
- package/types/src/guard.d.ts +223 -0
- package/types/src/missingData.d.ts +37 -0
- package/types/src/random.d.ts +173 -0
- package/types/src/string.d.ts +9 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
const projectRoot = path.resolve(__dirname);
|
|
8
|
+
|
|
9
|
+
function generateExports() {
|
|
10
|
+
const srcDir = path.join(projectRoot, 'src');
|
|
11
|
+
|
|
12
|
+
// 获取src目录下所有的js文件
|
|
13
|
+
const jsFiles = fs.readdirSync(srcDir)
|
|
14
|
+
.filter(file => fs.statSync(path.join(srcDir, file)).isFile() && path.extname(file) === '.js')
|
|
15
|
+
.map(file => path.basename(file, '.js'))
|
|
16
|
+
.sort();
|
|
17
|
+
|
|
18
|
+
// 生成 index.js 内容
|
|
19
|
+
const exportLines = jsFiles.map(file => `export * from './src/${file}.js';`);
|
|
20
|
+
const indexContent = exportLines.join('\n') + '\n';
|
|
21
|
+
|
|
22
|
+
// 写入 index.js 文件
|
|
23
|
+
fs.writeFileSync(path.join(projectRoot, 'index.js'), indexContent);
|
|
24
|
+
console.log('index.js 文件已生成');
|
|
25
|
+
|
|
26
|
+
// 构建 package.json 的 exports 字段
|
|
27
|
+
const exports = {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./index.js",
|
|
30
|
+
"types": "./types/index.d.ts"
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// 为每个文件添加导出配置
|
|
35
|
+
jsFiles.forEach(file => {
|
|
36
|
+
exports[`./${file}`] = {
|
|
37
|
+
"import": `./src/${file}.js`,
|
|
38
|
+
"types": `./types/src/${file}.d.ts`
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// 更新 package.json
|
|
43
|
+
const packagePath = path.join(projectRoot, 'package.json');
|
|
44
|
+
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
|
|
45
|
+
packageJson.exports = exports;
|
|
46
|
+
fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2) + '\n');
|
|
47
|
+
|
|
48
|
+
console.log('Exports 字段已更新');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
generateExports();
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "grofc_utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build:type": "tsc -p tsconfig.json",
|
|
8
|
+
"generate:exports": "node generate.exports.js",
|
|
9
|
+
"dev": "nodemon --watch src --exec \"npm run build:type & npm run generate:exports\""
|
|
10
|
+
},
|
|
11
|
+
"types": "./types/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./index.js",
|
|
15
|
+
"types": "./types/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./array": {
|
|
18
|
+
"import": "./src/array.js",
|
|
19
|
+
"types": "./types/src/array.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./guard": {
|
|
22
|
+
"import": "./src/guard.js",
|
|
23
|
+
"types": "./types/src/guard.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./missingData": {
|
|
26
|
+
"import": "./src/missingData.js",
|
|
27
|
+
"types": "./types/src/missingData.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./random": {
|
|
30
|
+
"import": "./src/random.js",
|
|
31
|
+
"types": "./types/src/random.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./string": {
|
|
34
|
+
"import": "./src/string.js",
|
|
35
|
+
"types": "./types/src/string.d.ts"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"keywords": [],
|
|
39
|
+
"author": "",
|
|
40
|
+
"license": "ISC",
|
|
41
|
+
"type": "module",
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"vitest": "^4.0.6"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/readme.md
ADDED
package/src/array.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template T
|
|
3
|
+
* @param {T[]} input
|
|
4
|
+
* @param {number} step
|
|
5
|
+
* @returns {T[][]}
|
|
6
|
+
*/
|
|
7
|
+
export function chunk(input, step) {
|
|
8
|
+
if (!Array.isArray(input)) throw new TypeError(`${input} must be string or array.`);
|
|
9
|
+
if (!Number.isInteger(step)) throw new TypeError(`${step} must be an integer.`);
|
|
10
|
+
if (step === 0) throw new RangeError(`step must not be zero.`);
|
|
11
|
+
const result = [];
|
|
12
|
+
if (step > 0) {
|
|
13
|
+
for (let i = 0; i < input.length; i += step) {
|
|
14
|
+
result.push(input.slice(i, i + step));
|
|
15
|
+
}
|
|
16
|
+
} else if (step < 0) {
|
|
17
|
+
for (let i = input.length; i > 0; i += step) {
|
|
18
|
+
result.push(input.slice(Math.max(0, i + step), i));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|