interface Todo {
id: number
order: number
title: string
}
interface Test {
whom: number
where: number
expected: number[]
}
const data: Todo[] = [
{ id: 1, order: 1, title: 'delectus aut autem' },
{ id: 2, order: 2, title: 'quis ut nam facilis et officia qui' },
{ id: 3, order: 3, title: 'fugiat veniam minus' },
{ id: 4, order: 4, title: 'et porro tempora' },
{ id: 5, order: 5, title: 'laboriosam mollitia et enim quasi adipisci quia provident illum' },
{ id: 6, order: 6, title: 'qui ullam ratione quibusdam voluptatem quia omnis' },
{ id: 7, order: 7, title: 'illo expedita consequatur quia in' },
{ id: 8, order: 8, title: 'quo adipisci enim quam ut ab' },
{ id: 9, order: 9, title: 'molestiae perspiciatis ipsa' },
{ id: 10, order: 10, title: 'illo est ratione doloremque quia maiores aut' },
]
function swapTodos(todos: Todo[], whom: number, where: number) {
if (whom === where) return todos
const result = (() => {
if (whom < where) {
const start = todos.slice(0, whom)
const neighbors = todos.slice(whom + 1, where + 1)
const tail = todos.slice(where + 1, todos.length)
return [...start, ...neighbors, todos[whom], ...tail]
} else {
const start = todos.slice(0, where)
const neighbors = todos.slice(where, whom)
const tail = todos.slice(whom + 1, todos.length)
return [...start, todos[whom], ...neighbors, ...tail]
}
})()
return result.map((it, i) => ({ ...it, order: i + 1 }))
}
const tests: Test[] = [
{ whom: 1, where: 5, expected: [1, 3, 4, 5, 6, 2, 7, 8, 9, 10] },
{ whom: 0, where: 9, expected: [2, 3, 4, 5, 6, 7, 8, 9, 10, 1] },
{ whom: 4, where: 9, expected: [1, 2, 3, 4, 6, 7, 8, 9, 10, 5] },
{ whom: 8, where: 3, expected: [1, 2, 3, 9, 4, 5, 6, 7, 8, 10] },
{ whom: 9, where: 0, expected: [10, 1, 2, 3, 4, 5, 6, 7, 8, 9] },
{ whom: 9, where: 4, expected: [1, 2, 3, 4, 10, 5, 6, 7, 8, 9] },
]
function runTests() {
console.log('-----------------')
for (const [i, { expected: expect, whom, where }] of tests.entries()) {
console.log(`[Test ${i + 1} start]`)
const result = swapTodos(data, whom, where)
const compare = result.map((it) => it.id)
if (JSON.stringify(compare) !== JSON.stringify(expect)) {
console.warn(`[Test ${i + 1} failed]: Результат не соответствует тому что ожидалось`)
console.log('Result => ', JSON.stringify(compare))
console.log('Expected => ', JSON.stringify(expect))
} else {
console.log(`[Test ${i + 1} passed]: Тест успешно пройден`)
}
console.log('-----------------')
}
}
export default runTests
public broadcast(type: string, receivers: string[], msg?: any) {
const events = this.emitter.eventNames()
if (!events.includes(type)) {
console.warn('Предупреждение: такое событие не добавлено!')
return
}
const list = receivers.map(id => this._players.indexOf(id))
const listeners = this.emitter.listeners(type)
for (const index of list) {
const fn = listeners[index]
if (typeof fn === 'function')
fn(msg)
else
console.error(`Ошибка, под индексом ${index} нет слушателя!`)
}
}
type MoveData = ((dir: "left" | "right", i: number) => void)
type TestItemProps = {
data: { name: string }, i: number, onMove: MoveData
}
const TestItem: FC<TestItemProps> = ({ data, i, onMove }) => {
return (
<div>
<div style={{ display: "flex", justifyContent: "space-between" }}>
<span
style={{ cursor: "pointer" }}
onClick={() => onMove("left", i)}
>◀</span>
<span
style={{ cursor: "pointer" }}
onClick={() => onMove("right", i)}
>▶</span>
</div>
<span>{data.name}</span>
</div>
)
}
function Test() {
const [state, setState] = useState([
{ name: "Petya", order: 0 },
{ name: "Nikita", order: 1 },
{ name: "Sasha", order: 2 },
{ name: "Valera", order: 3 },
])
const onMoveHandle: MoveData = (dir, prev) => {
const isLeftBlock = prev === 0 && dir === "left"
const isRightBlock = prev === state.length - 1 && dir === "right"
if (isLeftBlock || isRightBlock) return
const next = dir === "left" ? prev - 1 : prev + 1
setState((past) => past.reduce((acc: { name: string, order: number }[], it, i, arr) => {
if (![prev, next].includes(i))
acc.push(it)
else {
if (i === prev) acc.push(arr[next])
else acc.push(arr[prev])
}
return acc
}, []))
}
return (
<div style={{ display: "flex", columnGap: "10px" }}>
{state.map((it, i) => (
<TestItem
data={it} i={i} onMove={onMoveHandle} key={i}
/>
))}
</div>
)
}
const url = new URL(window.location.href)
url.searchParams.append("afterSending", "1")
window.history.pushState(null, null, url);
// window.location.href = url.toString()
// window.location.replace(url.toString())
const Input = () => {
const range = { min: 5, max: 50 }
const [value, setValue] = useState<number | "">(range.min)
function onBlurHandle(value: number) {
if (value >= range.min && value < range.max) return;
setValue(range.min)
}
return (
<input
type="number"
value={value}
onChange={({ target }) => setValue(target.value === "" ? "" : +target.value)}
onBlur={({ target }) => onBlurHandle(+target.value)}
min={range.min}
max={range.max}
/>
)
}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !path=
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ?path=$1 [QSA,L]
global $post, $wp_post_types;
$q_obj = get_queried_object();
$ptype = null;
if (!empty($post)) $ptype = &$wp_post_types[$post->post_type];
else if (isset($q_obj->taxonomy)) $ptype = &$wp_post_types[get_taxonomy($q_obj->taxonomy)->object_type[0]];
$post_title = $ptype->labels->name;