Skip to content

Instantly share code, notes, and snippets.

Ian Stewart ianmstew

  • Arcesium
  • New York
Block or report user

Report or block ianmstew

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ianmstew
ianmstew / hot-tip.md
Created Nov 8, 2019
imperative vs declarative object initialization
View hot-tip.md
const toggleButtonProps = {
  key: index,
  selected
};
if (props.disabled) toggleButtonProps.disabled = props.disabled;

Hot tip!! Try to challenge yourself to not imperatively update an object or array when you can use ES6 syntax to declare your logic at initialization/object creation time. The progression of developer into declarative/functional programming looks like this:

View typescript-classes-safe.md

http://www.typescriptlang.org/play/#code/MYGwhgzhAEAqCmEAu0DeAoaXoDMD2e0AvNEgBYCWEAdBQHYVIDc0A9K3AJ4AO8AysABOFbkgC0Y+owpgQFAF7wAJtADujMtGB46yQQFdgSPIOhhBAc0zYARueKlKNKUgBMLdtAAK+wfGgAUmAAbmACwqISwGB0AOQoLjJyimoaWjp6hsam5lbY0C6uAFxmdJxM1ljaukgGRiYAFNzCoUj+LiUxnAA0BQxunWUAlGiV+Y5UtP2uDoUV+QC+6AtAA

TypeScript makes JavaScript classes actually useful ^^

By fixing a fatal ES6 class flaw

The conspicuous absence of a way to reference constructor arguments within inline class property initializers

Which IMO after a year of struggles completely hobbles ES6 classes

View native-timezone.js
(() => {
function getLocale() {
// IE 11 does not support navigator.languages
return navigator.languages ? navigator.languages[0] : navigator.language;
}
function isTimezoneSupported(timezone) {
try {
new Date().toLocaleDateString(getLocale(), { timeZone: timezone });
} catch (err) {
if (err instanceof RangeError) {
@ianmstew
ianmstew / hoc-renderprop-hook.js
Last active Oct 22, 2019
HOC vs Render Prop vs Custom Hook
View hoc-renderprop-hook.js
/* Loader that renders empty unless `props.active` is true */
function Loader(props) {
const { active } = props;
return active && <div>Loading...</div>;
}
/* Loader class component that renders empty until a timeout */
class LoaderWithDelay extends React.Component {
@ianmstew
ianmstew / plain-object-maps-typescript.md
Last active Oct 11, 2019
Plain objects as maps in TypeScript
View plain-object-maps-typescript.md

So another 🤯 about TypeScript. We use plain objects as arbitrary maps all the time, but TypeScript deems that usage (correctly) too flexible.

const map = {};
map.foo = 'foo'; // Error: Property 'foo' does not exist on type '{}'.

Okay, then we need to type our object as a map:

View higher-order-generator.js
(() => {
function MappedYieldGenerator(generator, mapYield) {
return function*() {
const gen = generator();
let genResult;
let resolvedValue;
do {
genResult = gen.next(resolvedValue);
resolvedValue = yield mapYield(genResult.value);
} while (!genResult.done);
View schema.json
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"surname": {
"type": "string"
}
},
View isPalindromePermutation.js
function isPalindromePermutation(string) {
let counter = 0;
for (let i = 0; i < string.length; i++) {
const char = string[i];
if (char === ' ') {
continue;
}
const charCodeNormalized = char.toLowerCase().charCodeAt(0) - 'a'.charCodeAt(0);
You can’t perform that action at this time.