Last active: 2 years ago
Implement the state queue yourself
export const getFinalState = (baseState, queue) =>
queue.reduce(
(prev, cur) => (Function.prototype.isPrototypeOf(cur) ? cur(prev) : cur),
baseState
);
Last active: 2 years ago
/**
* 遍历父元素类名
* 查询是否有对应类名
* @returns
*/
const parentClassChecker = (el: HTMLElement | null): boolean => {
if (!el) return false;
if (!el?.classList) return parentClassChecker(el.parentElement);
return (
!!Array.from(el.classList).filter((item) =>
['more-language', 'popbtn-list', 'view-control'].includes(item)
).length || parentClassChecker(el.parentElement)
);
};
Last active: 2 years ago
/**
* Check element's id and it's parents
* @param el
* @returns
*/
const parentIdChecker = (el: HTMLElement | null): boolean => {
if (!el) return false;
if (!el?.id) return parentIdChecker(el.parentElement);
return (
!!['menu', 'menu-icon'].filter((item) => item === el.id).length ||
parentIdChecker(el.parentElement)
);
};
Last active: 2 years ago
const wrapper = useRef<HTMLDivElement>(null);
const obCallback: IntersectionObserverCallback = useCallback(
(entries, observer) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
if (!wrapper.current) return;
if (loading) return;
setPage((d) => {
d.pageCount = d.pageCount + pageCount;
});
observer.unobserve(wrapper.current);
});
},
[loading, setPage]
);
useEffect(() => {
const observer = new IntersectionObserver(obCallback);
wrapper.current && observer.observe(wrapper.current);
Last active: 2 years ago
import useTranslation, { TranslationTarget } from './useTranslation';
type Props = {
required?: TranslationTarget;
pattern?: TranslationTarget;
};
const defaultProps: Required<Props> = {
required: 'Value required',
pattern: 'Not a valid email address',
};
const useFormErrorMap = (props?: Props) => {
const { t } = useTranslation();
const { required, pattern } = {
...defaultProps,
...props,
};
return {
Last active: 2 years ago
func fibonacci22(n int) int {
f := make([]int, n+1, n+2)
if n < 2 {
f = f[0:2]
}
f[0] = 0
f[1] = 1
for i := 2; i <= n; i++ {
f[i] = f[i-1] + f[i-2]
}
return f[n]
}
func fibonacci2(n int) int {
x, y := 0, 1
for i := 2; i <= n; i++ {
x, y = y, x+y
}
return y
}
Last active: 2 years ago
Compose function
const compose =
<T>(...fns: ((...arg: T[]) => T)[]) =>
(arg: T) =>
fns.reduce((composed, fn) => fn(composed), arg);
const add = (n: number) => n + 1;
const multiply = (n: number) => n * 2;
const both = compose(add, multiply);
const result = both(20);
console.log(result);
Last active: 2 years ago
func Sqrt(x float64) float64 {
z := 1.0
for {
oldZ := z
z -= (z*z - x) / (2 * z)
if math.Abs(oldZ-z) <= 0.00000001 {
break
}
}
return z
}
Last active: 2 years ago
function sqrt(n) {
let z = 1.0;
while (true) {
let oldZ = z;
z -= (z * z - n) / (2 * z);
if (Math.abs(oldZ - z) <= 0.00000001) break;
}
return z;
}
Last active: 2 years ago
.force_landscape {
width: 100vmax;
height: 100vmin;
transform-origin: top left;
transform: rotate(90deg) translate(0, -100vmin);
}
Last active: 2 years ago
function frameArea(
sizeToFitOnScreen: number,
boxSize: number,
boxCenter: Vector3,
camera: THREE.PerspectiveCamera
) {
const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
const halfFovY = THREE.MathUtils.degToRad(camera.fov * 0.5);
const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
// compute a unit vector that points in the direction the camera is now
// in the xz plane from the center of the box
const direction = new THREE.Vector3()
.subVectors(camera.position, boxCenter)
.multiply(new THREE.Vector3(1, 0, 1))
.normalize();
// move the camera to a position distance units way from the center
// in whatever direction the camera was from the center already
camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
Last active: 2 years ago
class Person {
#age = 0;
constructor(name, age) {
this.name = name;
this.#age = age;
Object.defineProperty(this, 'name', { enumerable: false });
Object.defineProperties(this, {
name: {
enumerable: false,
},
age: {
enumerable: true,
},
});
}
get age() {
return this.#age;