Illegal Methods in Typescript

In javascript, we can create functions on the prototype of existing classes. Number.prototype.factorial = function() { return new Array(this.valueOf()) .fill() .reduce((acc, _, index) => acc * (index + 1), 1); }; (5).factorial(); // <- 120 However, in typescript, this is not allowed error TS2339: Property 'factorial' does not exist on type 'Number'. ► file:///C:/Users/lucas/misc/deno_test/main.ts:1:18 1 Number.prototype.factorial = function() { ~~~~~~~~~ We can add // @ts-ignore before every line and this will work, but what if we didn’t want to do that....

August 26, 2019

How to check if array is empty?

Writing code and had to check if an array was empty, however a suggestion was raised with the code I was using. arr.length === 0 The suggestion was to use the ember built in library, isEmpty, had me thinking, because I’ve written is-really-empty npm package as more of a joke againt the ember isEmpty function. It mostly just checks the length property anyway. // from ember.js/packages/@ember/-internals/metal/lib/is_empty.ts if (typeof obj.length === 'number' && objectType !...

August 23, 2019

Who uses session storage?

Session storage is available on the global window object, it’s API is very similar to localStorage but I cannot think of one case when I used it. Mdn states that sessionStorage is cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Below let’s have three outputs, one that is pulling from localStorage, another pulling from sessionStorage, and the last showing regular javascript memory....

May 15, 2019

Conditional Map in JS

Saw this in a PR at work: let result = ''; if (bar && foo) { result = 'bingo bango bongo'; } else if (!bar && foo) { result = 'bish bash bosh'; } else if (bar) { result = 'ez peazy lemon squeezy'; } return result Or something similar, made me think of how you could write this cleaner. if (fooCondition) { return barCondition ? 'bingo bango bongo' : 'bish bash bosh'; } else { return barCondition ?...

May 13, 2019

Rustfmt in Atom

Areweideyet.com is a helpful page listing IDEs and text editors with support for the language Rust. However, the one listed for Atom, the text editor I use the most, does not work. It uses an outdated API of rustfmt, there is an open PR but is untouched for 3 years. It also is not as performant as it can be. The current behavior saves the file, then passes the path to rustfmt’s command, and then overwrites the file....

May 10, 2019