Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💼
Working

Charles Stover CharlesStover

👨‍💼
Working
View GitHub Profile
View readonly-history.ts
import type { History, Location } from 'history';
type ReadonlyHistory<HistoryLocationState> = Omit<
Readonly<History<HistoryLocationState>>,
'location'
> & {
readonly location: Readonly<Location<HistoryLocationState>>;
};
export default ReadonlyHistory;
View my-component.jsx
// 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');
View my-capsule.js
// Create a capsule by giving it an initial value.
import Capsule from 'react-capsule';
export default new Capsule('my initial value');
@CharlesStover
CharlesStover / reactn.addCallback.js
Created Apr 21, 2019
ReactN: addCallback Example
View reactn.addCallback.js
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 };
}
@CharlesStover
CharlesStover / unintuitive.js
Created Dec 12, 2018
Variable length currying in JavaScript
View unintuitive.js
const addTo1 = addSub(1); // nums = [1]
+addTo1(2); // 3 nums = [ 1, 2 ]
+addTo1(2); // 1 nums = [ 1, 2, 2 ]
@CharlesStover
CharlesStover / solution.js
Created Dec 12, 2018
Variable length currying in JavaScript
View solution.js
// 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 = [ ];
@CharlesStover
CharlesStover / object-type-casting.js
Last active Apr 8, 2019
Variable length currying in JavaScript
View object-type-casting.js
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() {
@CharlesStover
CharlesStover / type-cast.js
Created Dec 12, 2018
Variable length currying in JavaScript
View type-cast.js
const x = addSubtract(1)(2)(3); // function
+x; // type cast to 0
+x(4); // type cast to 4
@CharlesStover
CharlesStover / possibilities.js
Created Dec 12, 2018
Variable length currying in JavaScript
View possibilities.js
// 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:
@CharlesStover
CharlesStover / example.js
Created Dec 12, 2018
Variable length currying in JavaScript
View example.js
addSubtract(1)(2)(3); // 1 + 2 - 3 = 0
addSubtract(1)(2)(3)(4)(5)(6); // 1 + 2 - 3 + 4 - 5 + 6 = 5