reaxes-utils 1.0.0 → 1.0.1

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.
Files changed (63) hide show
  1. package/index.js +2 -0
  2. package/index.js.map +1 -0
  3. package/package.json +12 -51
  4. package/types/ConditionRender.utility.d.ts +19 -0
  5. package/types/assert-group.utility.d.ts +6 -0
  6. package/types/checkGenericNull.utility.d.ts +1 -0
  7. package/types/checkType.utility.d.ts +2 -0
  8. package/types/cookie.utility.d.ts +7 -0
  9. package/types/crayon.utility.d.ts +9 -0
  10. package/types/dataflow.utility.d.ts +5 -0
  11. package/types/debounce.utility.d.ts +1 -0
  12. package/types/getNestedValue.utility.d.ts +2 -0
  13. package/{src/hooks/index.ts → types/hooks/index.d.ts} +1 -0
  14. package/types/hooks/useAsync.utility.d.ts +24 -0
  15. package/types/hooks/useForceUpdate.d.ts +1 -0
  16. package/types/hooks/useMountedRef.utility.d.ts +2 -0
  17. package/types/hooks/useRoute.utility.d.ts +5 -0
  18. package/types/hooks/useUndo.utility.d.ts +14 -0
  19. package/types/index.d.ts +23 -0
  20. package/types/isPromise.utility.d.ts +1 -0
  21. package/types/logProxy.utility.d.ts +1 -0
  22. package/types/makePair.utility.d.ts +1 -0
  23. package/types/orzPromise.utility.d.ts +4 -0
  24. package/types/overload.d.ts +1 -0
  25. package/types/queryString.utility.d.ts +6 -0
  26. package/types/random.utility.d.ts +5 -0
  27. package/types/replaceStr.utility.d.ts +1 -0
  28. package/types/runOnlyOnce.utility.d.ts +1 -0
  29. package/types/stringify.utility.d.ts +6 -0
  30. package/types/throttle.utility.d.ts +6 -0
  31. package/types/timer.utility.d.ts +14 -0
  32. package/types/tools.utility.d.ts +3 -0
  33. package/babel.config.js +0 -50
  34. package/src/ConditionRender.utility.tsx +0 -128
  35. package/src/assert-group.utility.ts +0 -70
  36. package/src/checkGenericNull.utility.ts +0 -18
  37. package/src/checkType.utility.ts +0 -44
  38. package/src/cookie.utility.ts +0 -124
  39. package/src/crayon.utility.ts +0 -149
  40. package/src/dataflow.utility.ts +0 -30
  41. package/src/debounce.utility.ts +0 -31
  42. package/src/getNestedValue.utility.ts +0 -47
  43. package/src/hooks/useAsync.utility.ts +0 -129
  44. package/src/hooks/useForceUpdate.tsx +0 -4
  45. package/src/hooks/useMountedRef.utility.ts +0 -20
  46. package/src/hooks/useUndo.utility.ts +0 -103
  47. package/src/index.tsx +0 -55
  48. package/src/isPromise.utility.ts +0 -10
  49. package/src/logProxy.utility.tsx +0 -7
  50. package/src/makePair.utility.ts +0 -18
  51. package/src/orzPromise.utility.ts +0 -24
  52. package/src/overload.ts +0 -17
  53. package/src/queryString.utility.ts +0 -77
  54. package/src/random.utility.ts +0 -73
  55. package/src/replaceStr.utility.ts +0 -15
  56. package/src/runOnlyOnce.utility.ts +0 -13
  57. package/src/stringify.utility.ts +0 -228
  58. package/src/throttle.utility.ts +0 -43
  59. package/src/timer.utility.ts +0 -103
  60. package/src/tools.utility.ts +0 -59
  61. package/src/workspace.d.ts +0 -43
  62. package/tsconfig.json +0 -89
  63. package/webpack.config.mjs +0 -205
@@ -0,0 +1 @@
1
+ export declare const isPromise: <T = any>(target: any) => target is Promise<T>;
@@ -0,0 +1 @@
1
+ export declare const logProxy: (source?: object) => any;
@@ -0,0 +1 @@
1
+ export declare const makePair: <I, F extends (value: I) => any>(value: I, ...callbacks: F[]) => [I, ...ReturnType<F>[]];
@@ -0,0 +1,4 @@
1
+ export declare const orzPromise: <T = any>(callback?: (resolve: Function, reject: Function) => any) => Promise<T> & {
2
+ resolve: (value?: any) => void;
3
+ reject: (reason?: any) => void;
4
+ };
@@ -0,0 +1 @@
1
+ export declare const overload: (params: any, processer: any) => any;
@@ -0,0 +1,6 @@
1
+ type ret = {
2
+ [p: string]: string;
3
+ };
4
+ export declare const decodeQueryString: <result extends ret>(str?: string) => ret;
5
+ export declare const encodeQueryString: (source: object) => string;
6
+ export {};
@@ -0,0 +1,5 @@
1
+ declare const Random: {
2
+ color(): string;
3
+ number(min: number, max: number, float?: number | boolean): number;
4
+ };
5
+ export default Random;
@@ -0,0 +1 @@
1
+ export declare const replaceStrUtility: (sourceText: string, [coordinate0, coordinate1]: [number, number], paragraph: string) => string;
@@ -0,0 +1 @@
1
+ export declare const runOnlyOnce: (callback: any) => (...args: any[]) => void;
@@ -0,0 +1,6 @@
1
+ export { stringify };
2
+ declare function stringify(obj: any, replacer?: any, spacer?: any, options?: any): any;
3
+ declare namespace stringify {
4
+ var stable: typeof deterministicStringify;
5
+ }
6
+ declare function deterministicStringify(obj: any, replacer: any, spacer: any, options: any): any;
@@ -0,0 +1,6 @@
1
+ interface IOptions {
2
+ leading?: boolean;
3
+ trailing?: boolean;
4
+ }
5
+ export default function throttle(func: any, wait: any, options: IOptions): () => void;
6
+ export {};
@@ -0,0 +1,14 @@
1
+ export declare const Timer: {
2
+ new (): {
3
+ "__#1@#subscribeList": any[];
4
+ "__#1@#currentTime": number;
5
+ "__#1@#interval": any;
6
+ "__#1@#currentTimeout": any;
7
+ "__#1@#status": boolean;
8
+ start: (time: any) => void | any;
9
+ subscribe(callback: any): any;
10
+ unsubscribe(callback: any): any;
11
+ destroy(): void;
12
+ stop(): any;
13
+ };
14
+ };
@@ -0,0 +1,3 @@
1
+ export declare const toDecimal2: (x: any) => string | false;
2
+ export declare const fen2yuan: (fen: any) => any;
3
+ export declare const yuan2fen: (yuan: any, digit?: number) => number;
package/babel.config.js DELETED
@@ -1,50 +0,0 @@
1
- module.exports = {
2
- "presets": [
3
- "@babel/preset-env",
4
- "@babel/preset-react",
5
- "@babel/preset-typescript",
6
- ],
7
- "plugins": [
8
- "@babel/plugin-proposal-do-expressions",
9
- "@babel/plugin-proposal-class-static-block",
10
- "@babel/plugin-proposal-export-default-from",
11
- "@babel/plugin-proposal-function-bind",
12
- "@babel/plugin-proposal-function-sent",
13
- "@babel/plugin-proposal-partial-application",
14
- ["@babel/plugin-proposal-pipeline-operator" , {
15
- "proposal" : "hack",
16
- "topicToken": "^^"
17
- }],
18
- "@babel/plugin-proposal-throw-expressions",
19
- "@babel/plugin-proposal-private-property-in-object",
20
- "@babel/plugin-syntax-bigint",
21
- ["@babel/plugin-proposal-decorators" , {
22
- "legacy": true,
23
- }],
24
-
25
- ],
26
- };
27
-
28
-
29
- /*
30
- `
31
-
32
- npm install --save-dev
33
-
34
- @babel/plugin-proposal-do-expressions
35
- @babel/plugin-proposal-class-static-block
36
- @babel/plugin-proposal-decorators
37
- @babel/plugin-proposal-export-default-from
38
- @babel/plugin-proposal-function-bind
39
- @babel/plugin-proposal-function-sent
40
- @babel/plugin-proposal-partial-application
41
- @babel/plugin-proposal-pipeline-operator
42
- @babel/plugin-proposal-throw-expressions
43
- @babel/plugin-proposal-private-property-in-object
44
- @babel/plugin-proposal-record-and-tuple
45
- @babel/plugin-syntax-bigint
46
-
47
-
48
-
49
-
50
- `.replaceAll(/\n/g, " "); */
@@ -1,128 +0,0 @@
1
-
2
- /**
3
- * 优雅的条件渲染器
4
- * @example
5
- * <If condition = {true}> 将会渲染出来 </If>
6
- * <If condition = {() => false}> 将不会渲染出来 </If> ;
7
- * <If condition = {() => false}>
8
- * <>condition为<b>true</b>时会渲染</>
9
- * {Else}
10
- * <>condition为<b>false</b>时会渲染</>
11
- * </If> ;
12
- * @param children {ReactNode} 要条件渲染的 ReactNode ;
13
- * @param condition {boolean | function : boolean} ;
14
- */
15
- export const If = ({children, condition}) => {
16
- /*cond : 拿到条件值: boolean*/
17
- const cond = (function () {
18
- if (typeof condition === 'function') {
19
- return condition();
20
- } else {
21
- return condition;
22
- }
23
- })();
24
- const result = (val = null) => <>{val}</>;
25
-
26
- if (Array.isArray(children) === true) {
27
- const [ELSE_JSX] = children.filter(value => value?.type === Else);
28
- const mapCallback = element => {
29
- if (typeof element === 'function') return element();
30
- else return element;
31
- };
32
- if (ELSE_JSX === undefined) {
33
- if (cond === true) {
34
- return result(children.map(mapCallback));
35
- } else {
36
- return result();
37
- }
38
- }
39
- const [the_first, the_last] = [
40
- children.slice(0, children.indexOf(ELSE_JSX)),
41
- children.slice(children.indexOf(ELSE_JSX) + 1),
42
- ];
43
- if (cond === true) {
44
- return result(the_first.map(mapCallback));
45
- } else {
46
- return result(the_last.map(mapCallback));
47
- }
48
- } else if (typeof children === 'function') {
49
- /*如果是函数 , 则返回函数的返回值 , 这里直接调用函数而非以组件形式调用*/
50
- if (cond === true) return result(children());
51
- else return result();
52
- } else {
53
- /*如果是数组 , 则条件渲染*/
54
- /*如果children不是数组类型 是个text或单一元素 , 则根据condition来决定是否渲染children*/
55
- if (cond === true) {
56
- return result(children);
57
- } else {
58
- return result();
59
- }
60
- }
61
- };
62
- export const Else = () => null;
63
- If.Else = Else;
64
-
65
- type Switch = React.FunctionComponent<{
66
- children;
67
- target;
68
- }> & {
69
- Case: Function;
70
- Default: Function;
71
- };
72
- export const Switch = (({children, target}) => {
73
- const {Case, Default} = Switch;
74
- let flag = Symbol('缺省--空值');
75
- let $Default: any = flag;
76
- let result: any = flag;
77
- if (Array.isArray(children)) {
78
- children.forEach(node => {
79
- const {props, type} = node;
80
- const {children} = props;
81
- if (type !== Case && type !== Default) {
82
- throw 'Switch组件的Children每一项必须是Case(可以包含一个Default)';
83
- }
84
- if (type === Default) {
85
- $Default = node;
86
- }
87
- if (Set.prototype === Object.getPrototypeOf(props.value)) {
88
- props.value.forEach(value$forEach => {
89
- if (value$forEach === target) {
90
- result = children;
91
- }
92
- });
93
- } else {
94
- if (props.value === target) {
95
- result = children;
96
- }
97
- }
98
- });
99
- } else {
100
- throw 'xxx2x4xx';
101
- }
102
- if (result === flag /*如果遍历结束后result还是flag , 就说明case中没有任何一项匹配*/) {
103
- if ($Default === flag /*看看有没有Default组件 */) {
104
- result = null;
105
- } else {
106
- result = $Default.props.children;
107
- }
108
- }
109
-
110
- return result;
111
- }) as Switch;
112
- /*
113
- 如果<Case/>.props.value是个Set , 就认为是多值匹配同一个返回结果
114
- 类似于switch(value){case 1:case 2:case3:return "xxx";}
115
- */
116
- Object.assign(Switch, {
117
- Case: ({value, children}) => {},
118
- Default: ({children}) => {},
119
- });
120
-
121
- function $example() {
122
- const {Case, Default} = Switch;
123
- <Switch target={Math.random()}>
124
- <Case value={123}>22323</Case>
125
- <Case value={new Set([3232, 2312])}>cccccccccc</Case>
126
- <Default value={''}></Default>
127
- </Switch>;
128
- }
@@ -1,70 +0,0 @@
1
- /**
2
- * 断言一组表达式是否全部为<expected>
3
- * @example
4
- assert(true,[true,true,false]) -> false
5
- * @param expected 预期的结果值
6
- * @param expressionList
7
- *
8
- */
9
- const print = ( value ) => typeof value === 'string' ? `"${ value }"` : value;
10
- export const assert = <Expect extends any , List extends any[]>( expected : Expect , expressionList:List , consoleWarn = false ):boolean => {
11
- for(let i = 0 ; i < expressionList.length ; i++){
12
- const value = expressionList[ i ];
13
- if ( value !== expected ) {
14
- if(consoleWarn) crayon.coral( `断言组第 `,i,` 个表达式不符合预期为 ` , print(expected) ,` 的结果, expressionList[`,i,`]: ` , print(value) );
15
- return false;
16
- }
17
- }
18
-
19
- return true;
20
- };
21
-
22
-
23
-
24
-
25
- /**
26
- * 组断言函数
27
- *
28
- * @example exp1 === true && exp2 === true && exp3 ==> assertTrue([exp1,exp2,exp3]) === true
29
- * @param array 一组表达式
30
- */
31
-
32
-
33
- export const assertTrue = ( array , consoleWarn = false) => assert( true , array , consoleWarn );
34
-
35
- /**每一组断言均为false时返回true*/
36
- export const assertFalse = ( array ,consoleWarn = false) => assert( false , array , consoleWarn );
37
-
38
-
39
- /**
40
- * 断言-一组表达式中只要有一个符合@param1则返回true
41
- * @example
42
- assert1of(false , [true,true,false,true]) -> true
43
- */
44
- export const assert1of = <Expected extends any,ExpressionList extends any[]>( expected:Expected , expressionList:ExpressionList , consoleWarn:boolean = false ) :boolean => {
45
- for ( let i = 0 ; i < expressionList.length ; i++ ) {
46
- const value = expressionList[ i ];
47
- if ( value === expected ) {
48
- if(consoleWarn) crayon['#81cc00']( `断言组第 `,i,` 个表达式符合预期为 ` , print(expected) ,` 的结果, expressionList[`,i,`]: ` , print(value) );
49
- return true;
50
- };
51
- }
52
- if(consoleWarn) crayon.coral( `断言组每个值都不符合预期为 ` , print(expected) ,` 的结果, expressionList: ` , print(expressionList) );
53
- return false;
54
- };
55
-
56
- export const assert1ofTrue = (expressionList , consoleWarn = false) => assert1of( true , expressionList ,consoleWarn);
57
- export const assert1ofFalse = (expressionList , consoleWarn = false) => assert1of( false , expressionList ,consoleWarn );
58
-
59
-
60
-
61
- import {crayon} from './crayon.utility';
62
- /**
63
- * @test
64
- *
65
- */
66
- if(0){
67
- const a = assert({a:1},["abc",{a:1},"abc"] , true);
68
- console.log( a );
69
- assert1of( 111 , [22,11221,2] , true );
70
- }
@@ -1,18 +0,0 @@
1
- /**
2
- * @format
3
- * @description 判断一切(后端认为的)空值,包括 undefined,null,{},[],""
4
- */
5
-
6
- export const checkGenericNull = (val: any): boolean => {
7
- switch (val) {
8
- case null:
9
- case '':
10
- case undefined:
11
- return true;
12
- }
13
- if (typeof val === 'object') {
14
- if (Array.isArray(val)) return val.length === 0;
15
- else return Object.keys(val).length === 0;
16
- }
17
- return false;
18
- };
@@ -1,44 +0,0 @@
1
- /** @format */
2
- /**
3
- * checkType
4
- *
5
- * @description Js类型检测
6
- *
7
- * @param {*} target 需要被检测的数据
8
- * @param {string} type 传入的类型
9
- * @returns {boolean | string}
10
- * @example
11
- *
12
- * checkType() // => undefined
13
- * checkType('test') // => string
14
- * checkType(123) // => number
15
- * checkType(null) // => null
16
- * checkType(undefined) // => undefined
17
- * checkType(true) // => boolean
18
- * checkType(symbol) // => function
19
- *
20
- * checkType('test', 'string') // => true
21
- * checkType(123, 'string') // => false
22
- * checkType([1,2,3], 'array') // => true
23
- * checkType({a:"1",b:"2"}, 'object') // => true
24
- *
25
- */
26
-
27
- function checkType(target: any, type?: string): boolean | string | void {
28
- if (arguments.length === 0) {
29
- console.error('Js类型检测方法参数不建议为空。');
30
- return undefined;
31
- }
32
- let TempStr = Object.prototype.toString.call(target).split(' ')[1];
33
- let CompleteStr = TempStr.slice(0, TempStr.length - 1).toLowerCase();
34
- if (arguments.length === 1) {
35
- return CompleteStr;
36
- }
37
- if (arguments.length >= 2) {
38
- let lowercaseType = type.toLowerCase();
39
- let result = CompleteStr === lowercaseType;
40
- return result;
41
- }
42
- }
43
-
44
- export default checkType;
@@ -1,124 +0,0 @@
1
- /**
2
- * Cookie工具方法
3
- *
4
- * @format
5
- */
6
-
7
- const Cookie = {
8
- /**
9
- * get
10
- *
11
- * @description 获取cookie
12
- *
13
- * @param {string} key - 目标cookie的key
14
- * @param {Function} [decode] - value解码方法 默认使用 decodeURIComponent 进行解码
15
- * @returns {string}
16
- *
17
- * @example
18
- *
19
- * Cookie.get('test')
20
- */
21
- get(key, decode = decodeURIComponent) {
22
- let value;
23
- if (key && document.cookie && document.cookie !== '') {
24
- let cookies = document.cookie.split('; ');
25
- for (let i = 0; i < cookies.length; i++) {
26
- let parts = cookies[i].split('=');
27
- let cookie = parts.slice(1).join('=');
28
- let name = decode(parts[0]);
29
-
30
- //去掉多余的 "
31
- if (cookie.indexOf('"') === 0) {
32
- cookie = cookie.slice(1, -1);
33
- }
34
-
35
- if (key === name) {
36
- value = decode(cookie);
37
- break;
38
- }
39
- }
40
- }
41
- return value;
42
- },
43
-
44
- /**
45
- * set
46
- *
47
- * @description 设置cookie
48
- *
49
- * @param {string} key - cookie 的 key
50
- * @param {string|number|object} value - cookie 的 value
51
- * @param {object} options - 例:{ expires: 0, path: null, secure: true, domain: 'd.2dfire.com' }<br>
52
- * expires: 过期时间默认单位是天,如果需要设置小于一天的过期时间,可以直接转入具体的Date对象;<br>
53
- * path: 例如 '/', '/mydir'; 如果没有定义,默认为当前文档位置的路径;<br>
54
- * secure: cookie只会被https传输 (boolean | null);<br>
55
- * domain: 例:'d.2dfire.com';如果没有定义,默认为当前文档位置的路径的域名部分 (string | null)。
56
- *
57
- * @returns {string}
58
- *
59
- * @example
60
- *
61
- * Cookie.set('test', 'xxx', { expires: 0, path: null, secure: true, domain: 'd.2dfire.com' })
62
- *
63
- */
64
- set(key, value, options) {
65
- options = Object.assign(
66
- {},
67
- {
68
- encode: encodeURIComponent,
69
- expires: 365,
70
- path: '/',
71
- secure: window.location.protocol === 'https:',
72
- },
73
- options,
74
- );
75
-
76
- let expires = options.expires;
77
- //过期时间默认单位是天,如果需要设置小于一天的过期时间,可以直接转入具体的Date对象
78
- if (typeof expires === 'number' && expires !== 0 && isFinite(expires)) {
79
- expires = new Date(new Date().getTime() + expires * 864e5);
80
- }
81
- //格式化value
82
- if (typeof value === 'object') {
83
- try {
84
- value = JSON.stringify(value);
85
- } catch (e) {}
86
- }
87
- //编码key、value,默认使用encodeURIComponent方法,如需其他编码方式可以传入encode覆盖默认方法
88
- key = options.encode(key);
89
- value = options.encode(value);
90
-
91
- return (document.cookie = [
92
- key + '=' + value,
93
- expires ? '; expires=' + expires.toUTCString() : '',
94
- options.path ? '; path=' + options.path : '',
95
- options.domain ? '; domain=' + options.domain : '',
96
- options.secure ? '; secure' : '',
97
- ].join(''));
98
- },
99
-
100
- /**
101
- * remove
102
- *
103
- * @description 移除cookie
104
- *
105
- * @param {string} key - cookie 的 key
106
- * @returns {boolean}
107
- *
108
- * @example
109
- *
110
- * Cookie.remove('test')
111
- */
112
- remove(key) {
113
- this.set(key, '', {
114
- expires: -1,
115
- });
116
- return !this.get(key);
117
- },
118
-
119
- hasItem: function (sKey) {
120
- return new RegExp('(?:|;s*)' + encodeURIComponent(sKey).replace(/[-.+]/g, '$&') + 's=').test(document.cookie);
121
- },
122
- };
123
-
124
- export default Cookie;
@@ -1,149 +0,0 @@
1
- type css = {
2
- [key in keyof CSSStyleDeclaration] : CSSStyleDeclaration[key] ;
3
-
4
- };
5
- type crayon = ( ( css: Partial<CSSStyleDeclaration> ) => ( ...logs:any[] ) => void ) & {
6
- [key in "warn" | "info" | "log" | "error"| "debug"| "trace"]: (( ...any ) => any) & {
7
- [key in string]: (...any) => any ;
8
- } } & {
9
- [key in string]: (...any) => any ;
10
- };
11
-
12
- /*把驼峰css key 改为短横连*/
13
- const transformCSSHumpToLowercase = (key:keyof CSSStyleDeclaration) => {
14
- return (key.toString().split('').reduce((accumulator,k) => {
15
- if(/[A-Z]/.test(k)){
16
- return accumulator.concat( '-'.concat( k.toLowerCase() ) );
17
- }
18
- return accumulator.concat(k);
19
- },'')) as keyof CSSStyleDeclaration;
20
- };
21
- /*将驼峰css key对象改为短横连*/
22
- const batchTransformCSSHumpToLowercase = (object:{[key in keyof CSSStyleDeclaration] : any}) => {
23
- const obj = {};
24
- for(const key in object){ /*@ts-ignore*/
25
- obj[transformCSSHumpToLowercase(key)] = object[key];
26
- }
27
- return obj;
28
- };
29
- /**
30
- * chalk的山寨版:crayon 用于打印更优美的日志
31
- *
32
- * @example
33
- crayon({
34
- 'background-color' : "red" ,
35
- color : "green" ,
36
- })('这里放字符串',{a:1})
37
-
38
- crayon.warn.red('以警告的方式打印红色文字',{a:1});
39
- crayon.warn('以警告的方式打印',{a:1});
40
-
41
-
42
- */
43
-
44
- /* secondary是console.log第二项的cssPropertiesString*/
45
- const argumentsAgent = (secondary , ...args) => args.reduce((accu, arg , index ) => (accu[0] += typeof arg === "string" ? arg : (accu.push(arg),"%o"),accu) ,['%c',secondary]);
46
-
47
-
48
- export const crayon : crayon = new Proxy((cssProperties:Partial<CSSStyleDeclaration> = {}) => {
49
- /*using as crayon({CSSProperties})('wanna log msg')*/
50
- return (...logs) => {
51
- let cssString = Object.keys(cssProperties).reduce((accumulator,key:string) => {
52
- /*@ts-ignore*/
53
- const res = `${accumulator}${String(transformCSSHumpToLowercase(key))}:${cssProperties[key]};`
54
- return res;
55
- },'');
56
- if(!cssString.includes('font-weight')){
57
- cssString += `font-weight:normal;`;
58
- }
59
- console.groupCollapsed(...argumentsAgent( cssString,...logs ));
60
- console.trace(`%c`,'font-weight:normal;');
61
- console.groupEnd();
62
- return;
63
- };
64
- },{
65
- get : (target, propKey:string, receiver) => {
66
-
67
- if([
68
- "prototype",
69
- "hasOwnProperty",
70
- "toString",
71
- "prototype",
72
- "length",
73
- "name",
74
- "arguments",
75
- "constructor",
76
- "isPrototypeOf",
77
- "apply",
78
- "call",
79
- "bind",
80
- ].includes(propKey)){
81
- return target[ propKey ];
82
- }
83
-
84
- const createColorProxy = ( type = "trace" ) => new Proxy( (...logs) => {
85
- if(type === "trace"){
86
- console.groupCollapsed(...argumentsAgent( `color:${ propKey };font-weight:normal;`,...logs ));
87
- console.trace(`%c`,'font-weight:normal;');
88
- console.groupEnd();
89
- return;
90
- }
91
- return console[type]( ...argumentsAgent( `color:${ propKey }`,...logs ) );
92
- } , {
93
- get : ( target , _propKey , receiver ) => {
94
-
95
- return ( ...logs ) => {
96
- if(type === "trace"){
97
- console.groupCollapsed(...argumentsAgent( `color:${ _propKey as string };font-weight:normal;`,...logs ));
98
- console.trace(`%c`,'font-weight:normal;');
99
- console.groupEnd();
100
- return;
101
- }
102
- /*当crayon.blue('',...args)时,babel使用了apply来隐式转换扩展运算符 , 导致了proxy拿到的是'apply'而非'blue' */
103
- if ( _propKey === "apply" ) {
104
- const [,_logs] = logs;
105
- if ( type === "trace" ) {
106
- console.groupCollapsed( ...argumentsAgent( `color:${ propKey }`,..._logs ) );
107
- console.trace( `%c` , 'font-weight:normal;' );
108
- console.groupEnd();
109
- return;
110
- }
111
- return console[type]( ...argumentsAgent( `color:${ propKey }`,..._logs ) );
112
- }
113
- return console[type]( ...argumentsAgent( `color:${ _propKey.toString() }`,...logs ) );
114
- };
115
- } ,
116
- } );
117
- switch ( propKey ){
118
- case "trace" :
119
- case "warn" :
120
- case "log" :
121
- case "info" :
122
- case "error" :
123
- case "debug" :
124
- return createColorProxy(propKey);
125
- }
126
- return createColorProxy("trace");
127
- },
128
- }) as crayon;
129
-
130
- /*示例 & 测试*/
131
- if(0){
132
- crayon({fontSize : "33px"})('erer',2342343,{a:23},'erer');
133
- crayon.warn['#eee']('dsdsdsdsds',{b:222});
134
- crayon.warn('dsdsdsdsds',{b:222});
135
- crayon.log('dsdsdsdsds',{b:222});
136
- crayon.purple('1111111111111',{b:222});
137
-
138
- crayon( {
139
- backgroundColor : "blue" ,
140
- color : "green" ,
141
- fontSize : "24px",
142
-
143
-
144
- } )( 'bbbbbbbbbbbbbbb' , { a : 1 } );
145
- crayon({color:"purple"})('asdasd',{a:1},'fafsdsd',{b:2},undefined);
146
-
147
- crayon[ "#aabbcc" ]( "asdsadsd");
148
- }
149
-