import { useState } from "react";
export type DispatchKey<T> = keyof T;
export type DispatchValue<T, K extends DispatchKey<T>> = T[K];
export type DispatchIterator<T, K extends DispatchKey<T>> = (key: K) => DispatchValue<T, K>;
export type InitialState<T> = T | (() => T);
export default function useDispatch<T extends {}>(
initialState: InitialState<T>
) {
const [state, setState] = useState(initialState);
function dispatch<K extends DispatchKey<T>>(
key: K,
value: DispatchValue<T, K>
): void;
function dispatch<K extends DispatchKey<T>>(
keys: K[],
iterator: DispatchIterator<T, K>
): void;
function dispatch<T, K extends DispatchKey<T>>(
keyOrKeys: K | K[],
valueOrIterator: DispatchValue<T, K> | DispatchIterator<T, K>
): void {
if (Array.isArray(keyOrKeys)) {
if (typeof valueOrIterator === "function") {
const iterator = valueOrIterator as DispatchIterator<T, K>;
setState((prev) => {
const updated = keyOrKeys.map((key) => iterator(key));
console.log(updated);
return prev;
});
}
} else {
setState((prev) => ({
...prev,
[keyOrKeys]: valueOrIterator
}));
}
}
return [state, dispatch] as const;
}
const splitBy = (text, trimWhitespaces = false, delimiter = ',', escape = '"') => {
const entries = [];
const chars = [...text];
const length = chars.length;
let entry = '';
let isEscaped = false;
let isSequence = false;
let isWillDelimiter = false;
let needResetEscaped = false;
for (let index = 0; index < length; index++) {
const char = chars[index];
const isChar = char !== delimiter || (char === delimiter && isSequence);
if (isWillDelimiter && isChar) {
throw new Error(`Unexpected char. Required delimite by ${delimiter} char.`);
}
isWillDelimiter = false;
if (char === "\\") {
isEscaped = true
}
if (char === escape && !isEscaped) {
isSequence = !isSequence;
if (!isSequence) {
isWillDelimiter = true;
}
}
if (needResetEscaped) {
isEscaped = false;
needResetEscaped = false;
}
if (isChar) {
if (isEscaped) {
entry += '\\';
needResetEscaped = true;
}
entry += char;
}
if (!isChar || index === length - 1) {
const preparedEntry = trimWhitespaces ? entry.trim() : entry;
entries.push(preparedEntry);
entry = '';
}
if (isSequence && index === length - 1) {
throw new Error(`Unexpected string end. Required close sequence by ${escape} char.`);
}
}
return entries;
};
npm run build
, или подобной. После чего собранное приложение уже разместить на сервере.