mazey 3.11.6 → 3.12.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/LICENSE +21 -21
- package/README.md +994 -991
- package/lib/index.cjs.js +1 -1
- package/lib/index.d.ts +9 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/index.esm.js +1 -1
- package/lib/mazey.min.js +1 -1
- package/lib/typing.d.ts +74 -74
- package/package.json +122 -119
- package/scripts/README.md +38 -38
- package/scripts/build-helper.js +76 -76
- package/scripts/dev.js +23 -23
- package/scripts/git-helper.js +155 -155
- package/scripts/release.js +9 -9
- package/scripts/rollup.config.mjs +152 -0
package/lib/typing.d.ts
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
interface BrowserInfo {
|
|
6
|
-
engine: string; // webkit gecko presto trident
|
|
7
|
-
engineVs: string;
|
|
8
|
-
platform: string; // desktop mobile
|
|
9
|
-
supporter: string; // chrome safari firefox opera iexplore edge
|
|
10
|
-
supporterVs: string;
|
|
11
|
-
system: string; // windows macos linux android ios
|
|
12
|
-
systemVs: string;
|
|
13
|
-
shell?: string; // wechat qq uc 360 2345 sougou liebao maxthon
|
|
14
|
-
shellVs?: string;
|
|
15
|
-
appleType?: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
interface DefineListeners {
|
|
19
|
-
[key: string]: any;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
type TestUa = (regexp: RegExp) => boolean;
|
|
23
|
-
type TestVs = (regexp: RegExp) => string;
|
|
24
|
-
|
|
25
|
-
interface UrlParams {
|
|
26
|
-
[key: string]: string | string[];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
type ThrottleFunc<T extends (...args: any[]) => any> = (...args: Parameters<T>) => ReturnType<T> | null;
|
|
30
|
-
|
|
31
|
-
type DebounceFunc<T extends (...args: any[]) => any> = (...args: Parameters<T>) => ReturnType<T>;
|
|
32
|
-
|
|
33
|
-
interface IsNumberOptions {
|
|
34
|
-
isNaNAsNumber?: boolean;
|
|
35
|
-
isInfinityAsNumber?: boolean;
|
|
36
|
-
isUnFiniteAsNumber?: boolean;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
type AnyFunction = (...args: any[]) => any;
|
|
40
|
-
|
|
41
|
-
interface ZResResponse {
|
|
42
|
-
status: number;
|
|
43
|
-
data: {
|
|
44
|
-
code: number;
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
interface ZResIsValidResOptions {
|
|
49
|
-
validStatusRange?: [number, number];
|
|
50
|
-
validCode?: number[];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface RepeatUntilOptions {
|
|
54
|
-
interval?: number;
|
|
55
|
-
times?: number;
|
|
56
|
-
context?: any;
|
|
57
|
-
args?: Array<any>;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
type LoadScriptReturns = Promise<boolean | string | Error | void>;
|
|
61
|
-
|
|
62
|
-
type SimpleType = string | number | boolean | null | undefined;
|
|
63
|
-
type SimpleObject = {
|
|
64
|
-
[key: string]: SimpleType | SimpleType[] | SimpleObject | SimpleObject[];
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
type UnknownObject = any; // Record<string, unknown>;
|
|
68
|
-
type UnknownFnParams = any[]; // | Record<string, unknown> | string | number | boolean | null | undefined;
|
|
69
|
-
type UnknownFnReturn = any;
|
|
70
|
-
type UnknownFn = (...args: UnknownFnParams) => UnknownFnReturn;
|
|
71
|
-
|
|
72
|
-
interface UnknownWindow {
|
|
73
|
-
[key: string]: any;
|
|
1
|
+
/**
|
|
2
|
+
* @author Cheng
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
interface BrowserInfo {
|
|
6
|
+
engine: string; // webkit gecko presto trident
|
|
7
|
+
engineVs: string;
|
|
8
|
+
platform: string; // desktop mobile
|
|
9
|
+
supporter: string; // chrome safari firefox opera iexplore edge
|
|
10
|
+
supporterVs: string;
|
|
11
|
+
system: string; // windows macos linux android ios
|
|
12
|
+
systemVs: string;
|
|
13
|
+
shell?: string; // wechat qq uc 360 2345 sougou liebao maxthon
|
|
14
|
+
shellVs?: string;
|
|
15
|
+
appleType?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface DefineListeners {
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type TestUa = (regexp: RegExp) => boolean;
|
|
23
|
+
type TestVs = (regexp: RegExp) => string;
|
|
24
|
+
|
|
25
|
+
interface UrlParams {
|
|
26
|
+
[key: string]: string | string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type ThrottleFunc<T extends (...args: any[]) => any> = (...args: Parameters<T>) => ReturnType<T> | null;
|
|
30
|
+
|
|
31
|
+
type DebounceFunc<T extends (...args: any[]) => any> = (...args: Parameters<T>) => ReturnType<T>;
|
|
32
|
+
|
|
33
|
+
interface IsNumberOptions {
|
|
34
|
+
isNaNAsNumber?: boolean;
|
|
35
|
+
isInfinityAsNumber?: boolean;
|
|
36
|
+
isUnFiniteAsNumber?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type AnyFunction = (...args: any[]) => any;
|
|
40
|
+
|
|
41
|
+
interface ZResResponse {
|
|
42
|
+
status: number;
|
|
43
|
+
data: {
|
|
44
|
+
code: number;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface ZResIsValidResOptions {
|
|
49
|
+
validStatusRange?: [number, number];
|
|
50
|
+
validCode?: number[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface RepeatUntilOptions {
|
|
54
|
+
interval?: number;
|
|
55
|
+
times?: number;
|
|
56
|
+
context?: any;
|
|
57
|
+
args?: Array<any>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type LoadScriptReturns = Promise<boolean | string | Error | void>;
|
|
61
|
+
|
|
62
|
+
type SimpleType = string | number | boolean | null | undefined;
|
|
63
|
+
type SimpleObject = {
|
|
64
|
+
[key: string]: SimpleType | SimpleType[] | SimpleObject | SimpleObject[];
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type UnknownObject = any; // Record<string, unknown>;
|
|
68
|
+
type UnknownFnParams = any[]; // | Record<string, unknown> | string | number | boolean | null | undefined;
|
|
69
|
+
type UnknownFnReturn = any;
|
|
70
|
+
type UnknownFn = (...args: UnknownFnParams) => UnknownFnReturn;
|
|
71
|
+
|
|
72
|
+
interface UnknownWindow {
|
|
73
|
+
[key: string]: any;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
export { AnyFunction, BrowserInfo, DebounceFunc, DefineListeners, IsNumberOptions, LoadScriptReturns, RepeatUntilOptions, SimpleObject, SimpleType, TestUa, TestVs, ThrottleFunc, UnknownFn, UnknownFnParams, UnknownFnReturn, UnknownObject, UnknownWindow, UrlParams, ZResIsValidResOptions, ZResResponse };
|
|
76
|
+
export type { AnyFunction, BrowserInfo, DebounceFunc, DefineListeners, IsNumberOptions, LoadScriptReturns, RepeatUntilOptions, SimpleObject, SimpleType, TestUa, TestVs, ThrottleFunc, UnknownFn, UnknownFnParams, UnknownFnReturn, UnknownObject, UnknownWindow, UrlParams, ZResIsValidResOptions, ZResResponse };
|
package/package.json
CHANGED
|
@@ -1,119 +1,122 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mazey",
|
|
3
|
-
"version": "3.
|
|
4
|
-
"description": "A functional library for daily front-end work.",
|
|
5
|
-
"main": "lib/index.cjs.js",
|
|
6
|
-
"module": "lib/index.esm.js",
|
|
7
|
-
"typings": "lib/index.d.ts",
|
|
8
|
-
"types": "lib/index.d.ts",
|
|
9
|
-
"mainEntryPointFilePath": "lib/index.d.ts",
|
|
10
|
-
"directories": {
|
|
11
|
-
"lib": "./lib"
|
|
12
|
-
},
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
"@babel/
|
|
69
|
-
"@babel/plugin-transform-
|
|
70
|
-
"@babel/
|
|
71
|
-
"@babel/
|
|
72
|
-
"@babel/
|
|
73
|
-
"@babel/
|
|
74
|
-
"@
|
|
75
|
-
"@
|
|
76
|
-
"@
|
|
77
|
-
"@
|
|
78
|
-
"@
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
"eslint": "^6.
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"eslint
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"rollup
|
|
104
|
-
"rollup-plugin-
|
|
105
|
-
"
|
|
106
|
-
"
|
|
107
|
-
"
|
|
108
|
-
"
|
|
109
|
-
"
|
|
110
|
-
"
|
|
111
|
-
"
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mazey",
|
|
3
|
+
"version": "3.12.0",
|
|
4
|
+
"description": "A functional library for daily front-end work.",
|
|
5
|
+
"main": "lib/index.cjs.js",
|
|
6
|
+
"module": "lib/index.esm.js",
|
|
7
|
+
"typings": "lib/index.d.ts",
|
|
8
|
+
"types": "lib/index.d.ts",
|
|
9
|
+
"mainEntryPointFilePath": "lib/index.d.ts",
|
|
10
|
+
"directories": {
|
|
11
|
+
"lib": "./lib"
|
|
12
|
+
},
|
|
13
|
+
"unpkg": "lib/mazey.min.js",
|
|
14
|
+
"jsdelivr": "lib/mazey.min.js",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "webpack serve --config ./scripts/webpack.config.dev.js --open",
|
|
17
|
+
"build": "cross-env SCRIPTS_NPM_PACKAGE_DEBUG=close SCRIPTS_NPM_PACKAGE_VERSION=$npm_package_version rollup --config ./scripts/rollup.config.mjs",
|
|
18
|
+
"build:debug": "cross-env SCRIPTS_NPM_PACKAGE_DEBUG=open SCRIPTS_NPM_PACKAGE_VERSION=$npm_package_version rollup --config ./scripts/rollup.config.mjs",
|
|
19
|
+
"test": "jest",
|
|
20
|
+
"test:debug": "npm run build:debug && npm run test",
|
|
21
|
+
"lint:fix": "eslint --ext 'js,ts,tsx' --fix ./src ./test ./scripts",
|
|
22
|
+
"docs": "typedoc --readme ./README.md --excludeProtected --excludePrivate --excludeInternal --hideGenerator",
|
|
23
|
+
"preview": "npm run build && npm run test",
|
|
24
|
+
"release": "npm run preview && cross-env SCRIPTS_NPM_PACKAGE_VERSION=$npm_package_version node ./scripts/release.js",
|
|
25
|
+
"release:win": "bash scripts/env.sh && npm i && npm run release"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/mazeyqian/mazey.git"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"front-end",
|
|
33
|
+
"load",
|
|
34
|
+
"math",
|
|
35
|
+
"url",
|
|
36
|
+
"style",
|
|
37
|
+
"css",
|
|
38
|
+
"debounce",
|
|
39
|
+
"throttle",
|
|
40
|
+
"trim",
|
|
41
|
+
"clone",
|
|
42
|
+
"json",
|
|
43
|
+
"random",
|
|
44
|
+
"dom",
|
|
45
|
+
"hash",
|
|
46
|
+
"storage",
|
|
47
|
+
"cookie",
|
|
48
|
+
"calculate",
|
|
49
|
+
"formula",
|
|
50
|
+
"browser",
|
|
51
|
+
"performance",
|
|
52
|
+
"pwa",
|
|
53
|
+
"console",
|
|
54
|
+
"function",
|
|
55
|
+
"typescript"
|
|
56
|
+
],
|
|
57
|
+
"author": {
|
|
58
|
+
"name": "Cheng",
|
|
59
|
+
"email": "mazeyqian@gmail.com",
|
|
60
|
+
"url": "https://github.com/mazeyqian"
|
|
61
|
+
},
|
|
62
|
+
"license": "MIT",
|
|
63
|
+
"bugs": {
|
|
64
|
+
"url": "https://github.com/mazeyqian/mazey/issues"
|
|
65
|
+
},
|
|
66
|
+
"homepage": "https://github.com/mazeyqian/mazey#readme",
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@babel/core": "^7.23.2",
|
|
69
|
+
"@babel/plugin-transform-class-properties": "^7.22.5",
|
|
70
|
+
"@babel/plugin-transform-object-rest-spread": "^7.22.15",
|
|
71
|
+
"@babel/plugin-transform-runtime": "^7.23.2",
|
|
72
|
+
"@babel/preset-env": "^7.23.2",
|
|
73
|
+
"@babel/preset-typescript": "^7.23.2",
|
|
74
|
+
"@babel/runtime": "^7.23.2",
|
|
75
|
+
"@babel/runtime-corejs3": "^7.23.2",
|
|
76
|
+
"@commitlint/cli": "^17.8.1",
|
|
77
|
+
"@commitlint/config-conventional": "^17.8.1",
|
|
78
|
+
"@rollup/plugin-babel": "^6.0.3",
|
|
79
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
80
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
81
|
+
"@types/jquery": "^3.5.29",
|
|
82
|
+
"@typescript-eslint/eslint-plugin": "^6.12.0",
|
|
83
|
+
"@typescript-eslint/parser": "^6.12.0",
|
|
84
|
+
"clean-webpack-plugin": "^4.0.0",
|
|
85
|
+
"core-js": "^3.33.2",
|
|
86
|
+
"cross-env": "^7.0.3",
|
|
87
|
+
"date-fns": "^2.30.0",
|
|
88
|
+
"eslint": "^8.54.0",
|
|
89
|
+
"eslint-config-standard": "^17.1.0",
|
|
90
|
+
"eslint-plugin-import": "^2.29.0",
|
|
91
|
+
"eslint-plugin-node": "^11.1.0",
|
|
92
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
93
|
+
"eslint-plugin-standard": "^4.1.0",
|
|
94
|
+
"execa": "^7.2.0",
|
|
95
|
+
"html-webpack-plugin": "^5.5.3",
|
|
96
|
+
"husky": "^8.0.3",
|
|
97
|
+
"jest": "^29.7.0",
|
|
98
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
99
|
+
"lint-staged": "^14.0.1",
|
|
100
|
+
"markdown-toc": "^1.2.0",
|
|
101
|
+
"prettier": "^2.8.8",
|
|
102
|
+
"prettier-eslint-cli": "^8.0.1",
|
|
103
|
+
"rollup": "^3.29.4",
|
|
104
|
+
"rollup-plugin-cleaner": "^1.0.0",
|
|
105
|
+
"rollup-plugin-copy": "^3.4.0",
|
|
106
|
+
"rollup-plugin-dts": "^6.1.0",
|
|
107
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
108
|
+
"ts-loader": "^9.4.4",
|
|
109
|
+
"tslib": "^2.6.2",
|
|
110
|
+
"typedoc": "^0.25.4",
|
|
111
|
+
"typescript": "5.3.2",
|
|
112
|
+
"webpack": "^5.89.0",
|
|
113
|
+
"webpack-cli": "^5.1.4",
|
|
114
|
+
"webpack-dev-server": "^4.15.1"
|
|
115
|
+
},
|
|
116
|
+
"husky": {
|
|
117
|
+
"hooks": {
|
|
118
|
+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
|
|
119
|
+
"pre-commit": "lint-staged"
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
package/scripts/README.md
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
# scripts
|
|
2
|
-
|
|
3
|
-
## release
|
|
4
|
-
|
|
5
|
-
Release this project with version by `process.env.VERSION` or `package.json`.
|
|
6
|
-
|
|
7
|
-
Usage:
|
|
8
|
-
|
|
9
|
-
```javascript
|
|
10
|
-
const pkgVersion = process.env.VERSION || require('../package.json').version;
|
|
11
|
-
release(pkgVersion);
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
It will be more straightforward if you use the development dependence CrossEnv.
|
|
15
|
-
|
|
16
|
-
```shell
|
|
17
|
-
# Install
|
|
18
|
-
npm i cross-env -D
|
|
19
|
-
|
|
20
|
-
# scripts
|
|
21
|
-
cross-env SCRIPTS_NPM_PACKAGE_VERSION=$npm_package_version node ./scripts/release.js
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
```javascript
|
|
25
|
-
// release.js
|
|
26
|
-
release();
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
<!-- @param {string} ver Version
|
|
30
|
-
@returns {void} -->
|
|
31
|
-
|
|
32
|
-
## generateToc
|
|
33
|
-
|
|
34
|
-
Generate table of contents for the markdown file.
|
|
35
|
-
|
|
36
|
-
<!-- @param {string} path Path of markdown file.
|
|
37
|
-
@param {array} options.hiddenHeadings Hidden headings.
|
|
38
|
-
@returns {void} -->
|
|
1
|
+
# scripts
|
|
2
|
+
|
|
3
|
+
## release
|
|
4
|
+
|
|
5
|
+
Release this project with version by `process.env.VERSION` or `package.json`.
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
const pkgVersion = process.env.VERSION || require('../package.json').version;
|
|
11
|
+
release(pkgVersion);
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
It will be more straightforward if you use the development dependence CrossEnv.
|
|
15
|
+
|
|
16
|
+
```shell
|
|
17
|
+
# Install
|
|
18
|
+
npm i cross-env -D
|
|
19
|
+
|
|
20
|
+
# scripts
|
|
21
|
+
cross-env SCRIPTS_NPM_PACKAGE_VERSION=$npm_package_version node ./scripts/release.js
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
// release.js
|
|
26
|
+
release();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
<!-- @param {string} ver Version
|
|
30
|
+
@returns {void} -->
|
|
31
|
+
|
|
32
|
+
## generateToc
|
|
33
|
+
|
|
34
|
+
Generate table of contents for the markdown file.
|
|
35
|
+
|
|
36
|
+
<!-- @param {string} path Path of markdown file.
|
|
37
|
+
@param {array} options.hiddenHeadings Hidden headings.
|
|
38
|
+
@returns {void} -->
|
package/scripts/build-helper.js
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
-
/* eslint-disable no-undef */
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const _resolve = (_path) => path.resolve(__dirname, _path);
|
|
5
|
-
const format = require('date-fns/format');
|
|
6
|
-
const toc = require('markdown-toc');
|
|
7
|
-
const fs = require('fs');
|
|
8
|
-
// Generate version: 20210308.072903
|
|
9
|
-
const genVersion = () => {
|
|
10
|
-
const d = new Date();
|
|
11
|
-
const version = format(d, 'yyyyMMdd.HHmmss');
|
|
12
|
-
return version;
|
|
13
|
-
};
|
|
14
|
-
const version = genVersion();
|
|
15
|
-
const defaultHiddenHeadings = [
|
|
16
|
-
'mazey',
|
|
17
|
-
'Install',
|
|
18
|
-
'Usage',
|
|
19
|
-
'Contributing',
|
|
20
|
-
'License',
|
|
21
|
-
'API Examples',
|
|
22
|
-
'Development Environment',
|
|
23
|
-
'Returns',
|
|
24
|
-
'Scripts',
|
|
25
|
-
'Node.js',
|
|
26
|
-
'TypeScript',
|
|
27
|
-
'Table of Contents',
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Generate table of contents for the markdown file.
|
|
32
|
-
*
|
|
33
|
-
* @param {string} path Path of markdown file.
|
|
34
|
-
* @param {array} options.hiddenHeadings Hidden headings.
|
|
35
|
-
* @returns {void}
|
|
36
|
-
*/
|
|
37
|
-
const generateToc = (path = '../README.md', options = {
|
|
38
|
-
hiddenHeadings: defaultHiddenHeadings,
|
|
39
|
-
}) => {
|
|
40
|
-
console.log('Generating Table of Contents...');
|
|
41
|
-
options = Object.assign({
|
|
42
|
-
hiddenHeadings: defaultHiddenHeadings,
|
|
43
|
-
}, options);
|
|
44
|
-
const RMFilePath = _resolve(path);
|
|
45
|
-
// https://melvingeorge.me/blog/get-all-the-contents-from-file-as-string-nodejs
|
|
46
|
-
console.log('running...');
|
|
47
|
-
const buffer = fs.readFileSync(RMFilePath);
|
|
48
|
-
const fileContent = buffer.toString();
|
|
49
|
-
// https://github.com/jonschlinkert/markdown-toc
|
|
50
|
-
const removeUnnecessaryHeadings = (str) => {
|
|
51
|
-
return !options.hiddenHeadings.some(v => {
|
|
52
|
-
return str.includes(v);
|
|
53
|
-
});
|
|
54
|
-
};
|
|
55
|
-
let tocContent = toc(fileContent, {
|
|
56
|
-
filter: removeUnnecessaryHeadings,
|
|
57
|
-
bullets: [ '-', '-' ],
|
|
58
|
-
}).content;
|
|
59
|
-
tocContent = tocContent.replace(/[ ]{4}/gm, '');
|
|
60
|
-
tocContent = '<!-- toc - begin -->\n' + '- Generated with ❤️\n' + tocContent;
|
|
61
|
-
tocContent = tocContent + '\n<!-- toc - end -->';
|
|
62
|
-
// Insert to file.
|
|
63
|
-
// https://stackoverflow.com/questions/14177087/replace-a-string-in-a-file-with-nodejs
|
|
64
|
-
let newFileContent = fileContent;
|
|
65
|
-
// https://stackoverflow.com/questions/1979884/how-to-use-javascript-regex-over-multiple-lines
|
|
66
|
-
newFileContent = newFileContent.replace(/<!-- toc - begin -->[\s\S]*<!-- toc - end -->/gm, tocContent);
|
|
67
|
-
console.log('running...');
|
|
68
|
-
fs.writeFileSync(RMFilePath, newFileContent);
|
|
69
|
-
console.log('Generating Table of Contents is done.');
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
module.exports = {
|
|
73
|
-
_resolve,
|
|
74
|
-
version,
|
|
75
|
-
generateToc,
|
|
76
|
-
};
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
/* eslint-disable no-undef */
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const _resolve = (_path) => path.resolve(__dirname, _path);
|
|
5
|
+
const format = require('date-fns/format');
|
|
6
|
+
const toc = require('markdown-toc');
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
// Generate version: 20210308.072903
|
|
9
|
+
const genVersion = () => {
|
|
10
|
+
const d = new Date();
|
|
11
|
+
const version = format(d, 'yyyyMMdd.HHmmss');
|
|
12
|
+
return version;
|
|
13
|
+
};
|
|
14
|
+
const version = genVersion();
|
|
15
|
+
const defaultHiddenHeadings = [
|
|
16
|
+
'mazey',
|
|
17
|
+
'Install',
|
|
18
|
+
'Usage',
|
|
19
|
+
'Contributing',
|
|
20
|
+
'License',
|
|
21
|
+
'API Examples',
|
|
22
|
+
'Development Environment',
|
|
23
|
+
'Returns',
|
|
24
|
+
'Scripts',
|
|
25
|
+
'Node.js',
|
|
26
|
+
'TypeScript',
|
|
27
|
+
'Table of Contents',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Generate table of contents for the markdown file.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} path Path of markdown file.
|
|
34
|
+
* @param {array} options.hiddenHeadings Hidden headings.
|
|
35
|
+
* @returns {void}
|
|
36
|
+
*/
|
|
37
|
+
const generateToc = (path = '../README.md', options = {
|
|
38
|
+
hiddenHeadings: defaultHiddenHeadings,
|
|
39
|
+
}) => {
|
|
40
|
+
console.log('Generating Table of Contents...');
|
|
41
|
+
options = Object.assign({
|
|
42
|
+
hiddenHeadings: defaultHiddenHeadings,
|
|
43
|
+
}, options);
|
|
44
|
+
const RMFilePath = _resolve(path);
|
|
45
|
+
// https://melvingeorge.me/blog/get-all-the-contents-from-file-as-string-nodejs
|
|
46
|
+
console.log('running...');
|
|
47
|
+
const buffer = fs.readFileSync(RMFilePath);
|
|
48
|
+
const fileContent = buffer.toString();
|
|
49
|
+
// https://github.com/jonschlinkert/markdown-toc
|
|
50
|
+
const removeUnnecessaryHeadings = (str) => {
|
|
51
|
+
return !options.hiddenHeadings.some(v => {
|
|
52
|
+
return str.includes(v);
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
let tocContent = toc(fileContent, {
|
|
56
|
+
filter: removeUnnecessaryHeadings,
|
|
57
|
+
bullets: [ '-', '-' ],
|
|
58
|
+
}).content;
|
|
59
|
+
tocContent = tocContent.replace(/[ ]{4}/gm, '');
|
|
60
|
+
tocContent = '<!-- toc - begin -->\n' + '- Generated with ❤️\n' + tocContent;
|
|
61
|
+
tocContent = tocContent + '\n<!-- toc - end -->';
|
|
62
|
+
// Insert to file.
|
|
63
|
+
// https://stackoverflow.com/questions/14177087/replace-a-string-in-a-file-with-nodejs
|
|
64
|
+
let newFileContent = fileContent;
|
|
65
|
+
// https://stackoverflow.com/questions/1979884/how-to-use-javascript-regex-over-multiple-lines
|
|
66
|
+
newFileContent = newFileContent.replace(/<!-- toc - begin -->[\s\S]*<!-- toc - end -->/gm, tocContent);
|
|
67
|
+
console.log('running...');
|
|
68
|
+
fs.writeFileSync(RMFilePath, newFileContent);
|
|
69
|
+
console.log('Generating Table of Contents is done.');
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
module.exports = {
|
|
73
|
+
_resolve,
|
|
74
|
+
version,
|
|
75
|
+
generateToc,
|
|
76
|
+
};
|