Skip to content

Instantly share code, notes, and snippets.

View quisido's full-sized avatar
🌸
trying my best

quisi.do quisido

🌸
trying my best
View GitHub Profile
@quisido
quisido / readonly-history.ts
Created September 5, 2021 18:06
ReadonlyHistory
import type { History, Location } from 'history';
type ReadonlyHistory<HistoryLocationState> = Omit<
Readonly<History<HistoryLocationState>>,
'location'
> & {
readonly location: Readonly<Location<HistoryLocationState>>;
};
export default ReadonlyHistory;
@quisido
quisido / my-component.jsx
Last active August 16, 2020 02:16
capsule.useState
// Manage a capsule with the useCapsule hook.
import { useCapsule } from 'react-capsule';
import myCapsule from '...';
export default function MyComponent() {
const [value, setValue] = useCapsule(myCapsule); // 💊
// Change my capsule's value on click.
const handleClick = React.useCallback(() => {
setValue('your value');
@quisido
quisido / my-capsule.js
Created July 6, 2020 18:27
new Capsule
// Create a capsule by giving it an initial value.
import Capsule from 'react-capsule';
export default new Capsule('my initial value');
@quisido
quisido / reactn.addCallback.js
Created April 21, 2019 20:31
ReactN: addCallback Example
import { addCallback, setGlobal } from 'reactn';
// Every time the global state changes, this function will execute.
function prevent1(newGlobalState) {
alert(`The new value is ${newGlobalState.value}!`);
// If the global state was changed to 1, change it to 2.
if (newGlobalState.value === 1) {
return { value: 2 };
}
@quisido
quisido / unintuitive.js
Created December 12, 2018 13:31
Variable length currying in JavaScript
const addTo1 = addSub(1); // nums = [1]
+addTo1(2); // 3 nums = [ 1, 2 ]
+addTo1(2); // 1 nums = [ 1, 2, 2 ]
@quisido
quisido / solution.js
Created December 12, 2018 13:30
Variable length currying in JavaScript
// Given an array of numbers, if the index is even, add.
// If the index is odd, subtract.
const addSubtractReducer = (total, current, index) =>
(index % 2) === 0 ?
total + current :
total - current;
const addSubtract = x => {
const nums = [ ];
@quisido
quisido / object-type-casting.js
Last active April 8, 2019 14:42
Variable length currying in JavaScript
const myStrObject = {
toString: function() {
return 'Str';
}
};
console.log('My object is ' + myStrObject); // 'My object is Str'
console.log(myStrObject + 297); // 'Str297'
const myNumObject = {
valueOf: function() {
@quisido
quisido / type-cast.js
Created December 12, 2018 13:28
Variable length currying in JavaScript
const x = addSubtract(1)(2)(3); // function
+x; // type cast to 0
+x(4); // type cast to 4
@quisido
quisido / possibilities.js
Created December 12, 2018 13:27
Variable length currying in JavaScript
// This cannot be true with the following statement.
addSubtract(1)(2)(3) === 0;
// This cannot be true with the preceding statement.
addSubtract(1)(2)(3)(4)(5)(6) === 5;
// This can be true:
addSubtract(1)(2)(3) + addSubtract(1)(2)(3)(4)(5)(6) === 5;
// These can be true too:
@quisido
quisido / example.js
Created December 12, 2018 13:27
Variable length currying in JavaScript
addSubtract(1)(2)(3); // 1 + 2 - 3 = 0
addSubtract(1)(2)(3)(4)(5)(6); // 1 + 2 - 3 + 4 - 5 + 6 = 5