@modern-js/utils 1.3.4 → 1.3.7
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +18 -0
- package/dist/js/modern/constants.js +1 -6
- package/dist/js/modern/is/type.js +4 -7
- package/dist/js/node/constants.js +1 -6
- package/dist/js/node/is/type.js +4 -7
- package/dist/js/treeshaking/constants.js +1 -6
- package/dist/js/treeshaking/is/type.js +4 -7
- package/dist/types/format.d.ts +2 -2
- package/dist/types/is/type.d.ts +3 -3
- package/jest.config.js +0 -1
- package/package.json +8 -8
- package/tests/is.test.ts +27 -0
- package/tests/isPlatform.test.ts +19 -0
- package/tests/isType.test.ts +70 -2
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,23 @@
|
|
1
1
|
# @modern-js/utils
|
2
2
|
|
3
|
+
## 1.3.7
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 132f7b53: feat: move config declarations to @modern-js/core
|
8
|
+
|
9
|
+
## 1.3.6
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- c2046f37: fix: plugin unbundle schema define
|
14
|
+
|
15
|
+
## 1.3.5
|
16
|
+
|
17
|
+
### Patch Changes
|
18
|
+
|
19
|
+
- 5bf5868d: fix: isObject should return false when input is null
|
20
|
+
|
3
21
|
## 1.3.4
|
4
22
|
|
5
23
|
### Patch Changes
|
@@ -233,12 +233,7 @@ export const PLUGIN_SCHEMAS = {
|
|
233
233
|
}
|
234
234
|
}],
|
235
235
|
'@modern-js/plugin-unbundle': [{
|
236
|
-
target: '
|
237
|
-
schema: {
|
238
|
-
type: 'boolean'
|
239
|
-
}
|
240
|
-
}, {
|
241
|
-
target: 'server.https',
|
236
|
+
target: 'output.disableAutoImportStyle',
|
242
237
|
schema: {
|
243
238
|
type: 'boolean'
|
244
239
|
}
|
@@ -5,23 +5,20 @@ export function isUndefined(obj) {
|
|
5
5
|
return typeof obj === 'undefined';
|
6
6
|
}
|
7
7
|
export function isArray(obj) {
|
8
|
-
return
|
8
|
+
return Array.isArray(obj);
|
9
9
|
} // eslint-disable-next-line @typescript-eslint/ban-types
|
10
10
|
|
11
11
|
export function isFunction(func) {
|
12
12
|
return typeof func === 'function';
|
13
|
-
}
|
14
|
-
|
13
|
+
}
|
15
14
|
export function isObject(obj) {
|
16
|
-
return typeof obj === 'object';
|
15
|
+
return obj !== null && typeof obj === 'object';
|
17
16
|
}
|
18
17
|
export function isPlainObject(obj) {
|
19
|
-
return obj &&
|
18
|
+
return isObject(obj) && Object.prototype.toString.call(obj) === '[object Object]';
|
20
19
|
}
|
21
20
|
export function isPromise(obj) {
|
22
|
-
/* eslint-disable promise/prefer-await-to-then */
|
23
21
|
return Boolean(obj) && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
24
|
-
/* eslint-enable promise/prefer-await-to-then */
|
25
22
|
}
|
26
23
|
export function isRegExp(obj) {
|
27
24
|
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
@@ -252,12 +252,7 @@ const PLUGIN_SCHEMAS = {
|
|
252
252
|
}
|
253
253
|
}],
|
254
254
|
'@modern-js/plugin-unbundle': [{
|
255
|
-
target: '
|
256
|
-
schema: {
|
257
|
-
type: 'boolean'
|
258
|
-
}
|
259
|
-
}, {
|
260
|
-
target: 'server.https',
|
255
|
+
target: 'output.disableAutoImportStyle',
|
261
256
|
schema: {
|
262
257
|
type: 'boolean'
|
263
258
|
}
|
package/dist/js/node/is/type.js
CHANGED
@@ -21,27 +21,24 @@ function isUndefined(obj) {
|
|
21
21
|
}
|
22
22
|
|
23
23
|
function isArray(obj) {
|
24
|
-
return
|
24
|
+
return Array.isArray(obj);
|
25
25
|
} // eslint-disable-next-line @typescript-eslint/ban-types
|
26
26
|
|
27
27
|
|
28
28
|
function isFunction(func) {
|
29
29
|
return typeof func === 'function';
|
30
|
-
}
|
31
|
-
|
30
|
+
}
|
32
31
|
|
33
32
|
function isObject(obj) {
|
34
|
-
return typeof obj === 'object';
|
33
|
+
return obj !== null && typeof obj === 'object';
|
35
34
|
}
|
36
35
|
|
37
36
|
function isPlainObject(obj) {
|
38
|
-
return obj &&
|
37
|
+
return isObject(obj) && Object.prototype.toString.call(obj) === '[object Object]';
|
39
38
|
}
|
40
39
|
|
41
40
|
function isPromise(obj) {
|
42
|
-
/* eslint-disable promise/prefer-await-to-then */
|
43
41
|
return Boolean(obj) && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
44
|
-
/* eslint-enable promise/prefer-await-to-then */
|
45
42
|
}
|
46
43
|
|
47
44
|
function isRegExp(obj) {
|
@@ -233,12 +233,7 @@ export var PLUGIN_SCHEMAS = {
|
|
233
233
|
}
|
234
234
|
}],
|
235
235
|
'@modern-js/plugin-unbundle': [{
|
236
|
-
target: '
|
237
|
-
schema: {
|
238
|
-
type: 'boolean'
|
239
|
-
}
|
240
|
-
}, {
|
241
|
-
target: 'server.https',
|
236
|
+
target: 'output.disableAutoImportStyle',
|
242
237
|
schema: {
|
243
238
|
type: 'boolean'
|
244
239
|
}
|
@@ -7,23 +7,20 @@ export function isUndefined(obj) {
|
|
7
7
|
return typeof obj === 'undefined';
|
8
8
|
}
|
9
9
|
export function isArray(obj) {
|
10
|
-
return
|
10
|
+
return Array.isArray(obj);
|
11
11
|
} // eslint-disable-next-line @typescript-eslint/ban-types
|
12
12
|
|
13
13
|
export function isFunction(func) {
|
14
14
|
return typeof func === 'function';
|
15
|
-
}
|
16
|
-
|
15
|
+
}
|
17
16
|
export function isObject(obj) {
|
18
|
-
return _typeof(obj) === 'object';
|
17
|
+
return obj !== null && _typeof(obj) === 'object';
|
19
18
|
}
|
20
19
|
export function isPlainObject(obj) {
|
21
|
-
return
|
20
|
+
return isObject(obj) && Object.prototype.toString.call(obj) === '[object Object]';
|
22
21
|
}
|
23
22
|
export function isPromise(obj) {
|
24
|
-
/* eslint-disable promise/prefer-await-to-then */
|
25
23
|
return Boolean(obj) && (_typeof(obj) === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
26
|
-
/* eslint-enable promise/prefer-await-to-then */
|
27
24
|
}
|
28
25
|
export function isRegExp(obj) {
|
29
26
|
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
package/dist/types/format.d.ts
CHANGED
@@ -6,11 +6,11 @@
|
|
6
6
|
* https://github.com/facebook/create-react-app/blob/master/LICENSE
|
7
7
|
*/
|
8
8
|
import { StatsCompilation } from 'webpack';
|
9
|
-
import { ProxyDetail,
|
9
|
+
import type { ProxyDetail, BffProxyOptions } from '@modern-js/types';
|
10
10
|
declare function formatWebpackMessages(json: StatsCompilation): {
|
11
11
|
errors: string[];
|
12
12
|
warnings: string[];
|
13
13
|
};
|
14
14
|
export { formatWebpackMessages };
|
15
|
-
declare function formatProxyOptions(proxyOptions:
|
15
|
+
declare function formatProxyOptions(proxyOptions: BffProxyOptions): ProxyDetail[];
|
16
16
|
export { formatProxyOptions };
|
package/dist/types/is/type.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
export declare function isString(str: any): str is string;
|
2
2
|
export declare function isUndefined(obj: any): obj is undefined;
|
3
|
-
export declare function isArray(obj:
|
3
|
+
export declare function isArray(obj: unknown): obj is any[];
|
4
4
|
export declare function isFunction(func: any): func is Function;
|
5
|
-
export declare function isObject(obj:
|
6
|
-
export declare function isPlainObject(obj:
|
5
|
+
export declare function isObject(obj: unknown): obj is Record<string, any>;
|
6
|
+
export declare function isPlainObject(obj: unknown): obj is Record<string, any>;
|
7
7
|
export declare function isPromise(obj: any): obj is Promise<any>;
|
8
8
|
export declare function isRegExp(obj: any): obj is RegExp;
|
package/jest.config.js
CHANGED
@@ -2,7 +2,6 @@ const sharedConfig = require('@scripts/jest-config');
|
|
2
2
|
|
3
3
|
/** @type {import('@jest/types').Config.InitialOptions} */
|
4
4
|
module.exports = {
|
5
|
-
// eslint-disable-next-line node/no-unsupported-features/es-syntax
|
6
5
|
...sharedConfig,
|
7
6
|
rootDir: __dirname,
|
8
7
|
modulePathIgnorePatterns: [
|
package/package.json
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "1.3.
|
14
|
+
"version": "1.3.7",
|
15
15
|
"jsnext:source": "./src/index.ts",
|
16
16
|
"types": "./dist/types/index.d.ts",
|
17
17
|
"main": "./dist/js/node/index.js",
|
@@ -52,6 +52,7 @@
|
|
52
52
|
"chalk": "^4.1.2",
|
53
53
|
"chokidar": "^3.5.2",
|
54
54
|
"debug": "^4.3.2",
|
55
|
+
"execa": "5",
|
55
56
|
"filesize": "^7.0.0",
|
56
57
|
"fs-extra": "^10.0.0",
|
57
58
|
"glob": "^7.1.7",
|
@@ -60,23 +61,22 @@
|
|
60
61
|
"pkg-up": "^3.1.0",
|
61
62
|
"recursive-readdir": "^2.2.2",
|
62
63
|
"strip-ansi": "6.0.0",
|
64
|
+
"typescript": "^4",
|
63
65
|
"upath": "^2.0.1",
|
64
|
-
"yaml": "^1.10.2"
|
65
|
-
"execa": "5",
|
66
|
-
"typescript": "^4"
|
66
|
+
"yaml": "^1.10.2"
|
67
67
|
},
|
68
68
|
"devDependencies": {
|
69
|
+
"@modern-js/types": "^1.3.6",
|
70
|
+
"@scripts/build": "0.0.0",
|
71
|
+
"@scripts/jest-config": "0.0.0",
|
69
72
|
"@types/debug": "^4.1.7",
|
70
73
|
"@types/fs-extra": "^9.0.12",
|
71
74
|
"@types/glob": "^7.1.4",
|
72
75
|
"@types/jest": "^26",
|
73
76
|
"@types/node": "^14",
|
74
77
|
"@types/recursive-readdir": "^2.2.0",
|
75
|
-
"webpack": "^5.54.0",
|
76
|
-
"@scripts/build": "0.0.0",
|
77
78
|
"jest": "^27",
|
78
|
-
"
|
79
|
-
"@modern-js/types": "^1.3.4"
|
79
|
+
"webpack": "^5.71.0"
|
80
80
|
},
|
81
81
|
"sideEffects": false,
|
82
82
|
"publishConfig": {
|
package/tests/is.test.ts
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import { isEmpty, isFastRefresh } from '../src/is';
|
2
|
+
|
3
|
+
describe('validate', () => {
|
4
|
+
it('should validate empty object correctly', () => {
|
5
|
+
expect(isEmpty({})).toBeTruthy();
|
6
|
+
expect(isEmpty({ foo: 'bar' })).toBeFalsy();
|
7
|
+
});
|
8
|
+
|
9
|
+
it('should validate fast refresh correctly', () => {
|
10
|
+
const { NODE_ENV, FAST_REFRESH } = process.env;
|
11
|
+
|
12
|
+
process.env.NODE_ENV = 'development';
|
13
|
+
process.env.FAST_REFRESH = 'true';
|
14
|
+
expect(isFastRefresh()).toBeTruthy();
|
15
|
+
|
16
|
+
process.env.NODE_ENV = 'production';
|
17
|
+
process.env.FAST_REFRESH = 'true';
|
18
|
+
expect(isFastRefresh()).toBeFalsy();
|
19
|
+
|
20
|
+
process.env.NODE_ENV = 'development';
|
21
|
+
process.env.FAST_REFRESH = 'false';
|
22
|
+
expect(isFastRefresh()).toBeFalsy();
|
23
|
+
|
24
|
+
process.env.NODE_ENV = NODE_ENV;
|
25
|
+
process.env.FAST_REFRESH = FAST_REFRESH;
|
26
|
+
});
|
27
|
+
});
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { isNodeJS, isBrowser } from '../src/is/platform';
|
2
|
+
|
3
|
+
describe('validate platform', () => {
|
4
|
+
it('should validate Node.js correctly', () => {
|
5
|
+
expect(isNodeJS()).toBeTruthy();
|
6
|
+
});
|
7
|
+
|
8
|
+
it('should validate browser correctly', () => {
|
9
|
+
const windowSpy = jest.spyOn(window, 'window', 'get');
|
10
|
+
|
11
|
+
windowSpy.mockImplementation((): any => undefined);
|
12
|
+
expect(isBrowser()).toBeFalsy();
|
13
|
+
|
14
|
+
windowSpy.mockImplementation((): any => global);
|
15
|
+
expect(isBrowser()).toBeTruthy();
|
16
|
+
|
17
|
+
windowSpy.mockRestore();
|
18
|
+
});
|
19
|
+
});
|
package/tests/isType.test.ts
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
isArray,
|
3
|
+
isObject,
|
4
|
+
isString,
|
5
|
+
isRegExp,
|
6
|
+
isPromise,
|
7
|
+
isUndefined,
|
8
|
+
isPlainObject,
|
9
|
+
} from '../src/is/type';
|
2
10
|
|
3
11
|
describe('validate type', () => {
|
4
12
|
it('should validate string correctly', () => {
|
@@ -8,10 +16,70 @@ describe('validate type', () => {
|
|
8
16
|
expect(isString(123)).toBeFalsy();
|
9
17
|
});
|
10
18
|
|
11
|
-
it('should validate
|
19
|
+
it('should validate undefined correctly', () => {
|
12
20
|
expect(isUndefined(undefined)).toBeTruthy();
|
13
21
|
expect(isUndefined(null)).toBeFalsy();
|
14
22
|
expect(isUndefined('')).toBeFalsy();
|
15
23
|
expect(isUndefined(123)).toBeFalsy();
|
16
24
|
});
|
25
|
+
|
26
|
+
it('should validate array correctly', () => {
|
27
|
+
expect(isArray(undefined)).toBeFalsy();
|
28
|
+
expect(isArray(null)).toBeFalsy();
|
29
|
+
expect(isArray('')).toBeFalsy();
|
30
|
+
expect(isArray(123)).toBeFalsy();
|
31
|
+
expect(isArray({})).toBeFalsy();
|
32
|
+
expect(isArray([])).toBeTruthy();
|
33
|
+
});
|
34
|
+
|
35
|
+
it('should validate object correctly', () => {
|
36
|
+
expect(isObject(1)).toBeFalsy();
|
37
|
+
expect(isObject('1')).toBeFalsy();
|
38
|
+
expect(isObject(undefined)).toBeFalsy();
|
39
|
+
expect(isObject(null)).toBeFalsy();
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
41
|
+
expect(isObject(() => {})).toBeFalsy();
|
42
|
+
expect(isObject({})).toBeTruthy();
|
43
|
+
expect(isObject([])).toBeTruthy();
|
44
|
+
expect(isObject(/foo/)).toBeTruthy();
|
45
|
+
});
|
46
|
+
|
47
|
+
it('should validate plain object correctly', () => {
|
48
|
+
expect(isPlainObject(1)).toBeFalsy();
|
49
|
+
expect(isPlainObject('1')).toBeFalsy();
|
50
|
+
expect(isPlainObject(undefined)).toBeFalsy();
|
51
|
+
expect(isPlainObject(null)).toBeFalsy();
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
53
|
+
expect(isPlainObject(() => {})).toBeFalsy();
|
54
|
+
expect(isPlainObject({})).toBeTruthy();
|
55
|
+
expect(isPlainObject([])).toBeFalsy();
|
56
|
+
expect(isPlainObject(/foo/)).toBeFalsy();
|
57
|
+
});
|
58
|
+
|
59
|
+
it('should validate RegExp correctly', () => {
|
60
|
+
expect(isRegExp(1)).toBeFalsy();
|
61
|
+
expect(isRegExp('1')).toBeFalsy();
|
62
|
+
expect(isRegExp(undefined)).toBeFalsy();
|
63
|
+
expect(isRegExp(null)).toBeFalsy();
|
64
|
+
expect(isRegExp({})).toBeFalsy();
|
65
|
+
expect(isRegExp([])).toBeFalsy();
|
66
|
+
expect(isRegExp(/foo/)).toBeTruthy();
|
67
|
+
});
|
68
|
+
|
69
|
+
it('should validate Promise correctly', () => {
|
70
|
+
expect(isPromise(1)).toBeFalsy();
|
71
|
+
expect(isPromise('1')).toBeFalsy();
|
72
|
+
expect(isPromise(undefined)).toBeFalsy();
|
73
|
+
expect(isPromise(null)).toBeFalsy();
|
74
|
+
expect(isPromise({})).toBeFalsy();
|
75
|
+
expect(isPromise([])).toBeFalsy();
|
76
|
+
expect(isPromise(/foo/)).toBeFalsy();
|
77
|
+
expect(
|
78
|
+
isPromise(
|
79
|
+
new Promise<void>(resolve => {
|
80
|
+
resolve();
|
81
|
+
}),
|
82
|
+
),
|
83
|
+
).toBeTruthy();
|
84
|
+
});
|
17
85
|
});
|