Skip to content

Instantly share code, notes, and snippets.

/* スクロールアニメーションを設定 */
// スクロール率を取得
let scrollPercent = 0;
const scrollHeight = document.documentElement.scrollHeight; // ページの総スクロール量を取得
const clientHeight = document.documentElement.clientHeight; // デバイス表示領域の高さを取得
document.body.onscroll = function () {
const scrollTop = document.documentElement.scrollTop; //ブラウザ際上部からのスクロール量を取得
scrollPercent = (scrollTop / (scrollHeight - clientHeight)) * 100; // 何%スクロールしているかを算出
};
// アニメーションさせる要素を取得
@usualoma
usualoma / jsx.ts
Last active October 18, 2023 20:57
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jsx.JSX {
interface IntrinsicElements {
[tagName: string]: Record<string, string>;
}
}
}
function escape(str: string): string {
import { jsx, render } from './jsx'
const Layout = (props: { children?: string; title: string }) => {
return (
<html>
<head>
<title>{props.title}</title>
</head>
<body>{props.children}</body>
</html>
@jasonbyrne
jasonbyrne / merge-objects.ts
Created May 2, 2022 13:04
Deep Merge and Shallow Merge
export const shallowMerge = <T extends object = Record<string, any>>(
...objects: T[]
): T => {
return objects.reduce((prev, cur) => ({ ...prev, ...cur }), {} as T);
};
export const deepMerge = <T extends object = Record<string, any>>(
target: T,
...sources: object[]
): T => {
@lmammino
lmammino / cp.js
Last active January 20, 2023 17:28
Session about Node.js streams with Kelvin
import { createReadStream, createWriteStream } from 'fs'
const source = createReadStream('./assets/moby-dick.txt')
const dest = createWriteStream('./assets/moby-dick-decompressed.txt')
source
.pipe(dest)
@anastaciocintra
anastaciocintra / Compare two data objects .md
Last active November 7, 2024 19:20
Compare two objects JS

Compare two data objects / arrays in javascript and/or typescript regardless of the order of the data;

When to use:

  • When you need compare two "json" objects to know if it have same data.
  • When the order of the array doesn't matters ie. [1,2] should be equal [2,1], but it is configurable.
  • lightweight - just one small .js file

when not to use:

  • when you need to compare more complex objects, with functions, for example.
@w3collective
w3collective / autocomplete-search-javascript.html
Created March 23, 2022 23:26
Autocomplete search using vanilla JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Create an autocomplete search using vanilla JavaScript</title>
</head>
<body>
<div>
<input type="text" id="autocomplete" placeholder="Select a color..."></input>
@asaumet230
asaumet230 / useForm.js
Last active June 27, 2023 20:41
Custom Hook UseForm( )
import { useState } from "react";
const useForm = ( initialState = {} ) => {
const [ values, setValues ] = useState(initialState);
// Agregar Valores al Formulario:
const handleInputChanges = ( { target } ) => {
setValues({
@CarlosRA97
CarlosRA97 / useAsyncHook.ts
Last active May 4, 2022 12:11
Async hook for Axios in ReactJS
import { AxiosResponse } from 'axios';
import { useEffect } from 'react';
export const useAsync = (
asyncFn: () => Promise<AxiosResponse<any, any>>,
successFunction: Function,
returnFunction: Function,
dependencies: any[] = []
) => {
useEffect(() => {
@Alonso-Pablo
Alonso-Pablo / useAsync.ts
Created March 12, 2022 21:49
Avoids errors if the request cannot be resolved because the React component was disassembled.
import { useEffect } from 'react'
import { AxiosResponse } from 'axios'
export const useAsync = (
asyncFn: () => Promise<AxiosResponse<any, any>>,
successFunction: Function,
returnFunction: Function,
dependencies: any[] = []
) => {
useEffect(() => {