feat: 代码生成
This commit is contained in:
@@ -4,7 +4,9 @@ export * from './generate-routes-backend';
|
||||
export * from './generate-routes-frontend';
|
||||
export * from './get-popup-container';
|
||||
export * from './merge-route-modules';
|
||||
export * from './mitt';
|
||||
export * from './reset-routes';
|
||||
export * from './safe';
|
||||
export * from './tree';
|
||||
export * from './unmount-global-loading';
|
||||
export * from './uuid';
|
||||
|
135
packages/utils/src/helpers/mitt.ts
Normal file
135
packages/utils/src/helpers/mitt.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* copy to https://github.com/developit/mitt
|
||||
* Expand clear method
|
||||
*/
|
||||
export type EventType = string | symbol;
|
||||
|
||||
// An event handler can take an optional event argument
|
||||
// and should not return a value
|
||||
export type Handler<T = unknown> = (event: T) => void;
|
||||
export type WildcardHandler<T = Record<string, unknown>> = (
|
||||
type: keyof T,
|
||||
event: T[keyof T],
|
||||
) => void;
|
||||
|
||||
// An array of all currently registered event handlers for a type
|
||||
export type EventHandlerList<T = unknown> = Array<Handler<T>>;
|
||||
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<
|
||||
WildcardHandler<T>
|
||||
>;
|
||||
|
||||
// A map of event types and their corresponding event handlers.
|
||||
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<
|
||||
'*' | keyof Events,
|
||||
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
|
||||
>;
|
||||
|
||||
export interface Emitter<Events extends Record<EventType, unknown>> {
|
||||
all: EventHandlerMap<Events>;
|
||||
|
||||
clear(): void;
|
||||
emit<Key extends keyof Events>(
|
||||
type: undefined extends Events[Key] ? Key : never,
|
||||
): void;
|
||||
|
||||
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void;
|
||||
off(type: '*', handler: WildcardHandler<Events>): void;
|
||||
|
||||
off<Key extends keyof Events>(
|
||||
type: Key,
|
||||
handler?: Handler<Events[Key]>,
|
||||
): void;
|
||||
on(type: '*', handler: WildcardHandler<Events>): void;
|
||||
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mitt: Tiny (~200b) functional event emitter / pubsub.
|
||||
* @name mitt
|
||||
* @returns {Mitt} any
|
||||
*/
|
||||
export function mitt<Events extends Record<EventType, unknown>>(
|
||||
all?: EventHandlerMap<Events>,
|
||||
): Emitter<Events> {
|
||||
type GenericEventHandler =
|
||||
| Handler<Events[keyof Events]>
|
||||
| WildcardHandler<Events>;
|
||||
all = all || new Map();
|
||||
|
||||
return {
|
||||
/**
|
||||
* A Map of event names to registered handler functions.
|
||||
*/
|
||||
all,
|
||||
|
||||
/**
|
||||
* Clear all
|
||||
*/
|
||||
clear() {
|
||||
this.all.clear();
|
||||
},
|
||||
|
||||
/**
|
||||
* Invoke all handlers for the given type.
|
||||
* If present, `'*'` handlers are invoked after type-matched handlers.
|
||||
*
|
||||
* Note: Manually firing '*' handlers is not supported.
|
||||
*
|
||||
* @param {string|symbol} type The event type to invoke
|
||||
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
|
||||
* @memberOf mitt
|
||||
*/
|
||||
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) {
|
||||
let handlers = all?.get(type);
|
||||
if (handlers) {
|
||||
[...(handlers as EventHandlerList<Events[keyof Events]>)].forEach(
|
||||
(handler) => {
|
||||
handler(evt as Events[Key]);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
handlers = all?.get('*');
|
||||
if (handlers) {
|
||||
[...(handlers as WildCardEventHandlerList<Events>)].forEach(
|
||||
(handler) => {
|
||||
handler(type, evt as Events[Key]);
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove an event handler for the given type.
|
||||
* If `handler` is omitted, all handlers of the given type are removed.
|
||||
* @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler)
|
||||
* @param {Function} [handler] Handler function to remove
|
||||
* @memberOf mitt
|
||||
*/
|
||||
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) {
|
||||
const handlers: Array<GenericEventHandler> | undefined = all?.get(type);
|
||||
if (handlers) {
|
||||
if (handler) {
|
||||
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
|
||||
} else {
|
||||
all?.set(type, []);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Register an event handler for the given type.
|
||||
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events
|
||||
* @param {Function} handler Function to call in response to given event
|
||||
* @memberOf mitt
|
||||
*/
|
||||
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) {
|
||||
const handlers: Array<GenericEventHandler> | undefined = all?.get(type);
|
||||
if (handlers) {
|
||||
handlers.push(handler);
|
||||
} else {
|
||||
all?.set(type, [handler] as EventHandlerList<Events[keyof Events]>);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
10
packages/utils/src/helpers/safe.ts
Normal file
10
packages/utils/src/helpers/safe.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* 跟后台逻辑一致
|
||||
* Number.isSafeInteger形参只能为Number类型 其他的直接返回false
|
||||
* @param str 数字
|
||||
* @returns 安全数内返回number类型 否则返回原字符串
|
||||
*/
|
||||
export function safeParseNumber(str: string): number | string {
|
||||
const num = Number(str);
|
||||
return Number.isSafeInteger(num) ? num : str;
|
||||
}
|
Reference in New Issue
Block a user