Skip to content

Instantly share code, notes, and snippets.

View r3dm1ke's full-sized avatar
🎯
Before software can be reusable it first has to be usable.

Michael Krasnov r3dm1ke

🎯
Before software can be reusable it first has to be usable.
View GitHub Profile
@r3dm1ke
r3dm1ke / this.js
Last active March 2, 2020 15:03
Understanding this keyword in JS
const myObject = {
a: 'b',
b: 'c',
doStuff: function() {
// Here, this refers to myObject
console.log(this.a + this.b);
}
}
myObject.doStuff(); // bc
@r3dm1ke
r3dm1ke / type-coercion-example.js
Created February 6, 2020 18:28
Showing type coercion in JavaScript
/* Here, '5' will be converted to 5 */
5 == '5'; // true
5 === '5'; // false
/* Here, true will be converted to 1 */
1 == true; // true
1 > false; // true
0 === false; // false
// Here, JS will try to convert both of these to number
@r3dm1ke
r3dm1ke / scoping-example.js
Last active February 13, 2020 17:05
Comparing let and var in scoping levels
function doStuff() {
// both a and b will be available for this function, but not outside
let a = 5;
var b = 5;
console.log(a + b); // 10
}
doStuff(); // 10;
console.log(a); // ReferenceError
@r3dm1ke
r3dm1ke / Observable.ts
Created February 5, 2020 17:07
RxJS TS readability example
// HACK: Since TypeScript inherits static properties too, we have to
// fight against TypeScript here so Subject can have a different static create signature
/**
* Creates a new cold Observable by calling the Observable constructor
* @static true
* @owner Observable
* @method create
* @param {Function} subscribe? the subscriber function to be passed to the Observable constructor
* @return {Observable} a new cold observable
* @nocollapse
@r3dm1ke
r3dm1ke / createStore.ts
Created February 5, 2020 16:31
Redux TS readability example
// TODO: do this more elegantly
;((currentReducer as unknown) as Reducer<
NewState,
NewActions
>) = nextReducer
@r3dm1ke
r3dm1ke / proxy-custom-validation.js
Created January 31, 2020 16:32
Using Proxy API to define custom validation for property values
const stringValidatorHandler = {
set: (obj, key, value) => {
if (typeof value !== 'string') {
throw new Error('Expected string!')
}
obj[key] = value.toUpperCase();
}
}
@r3dm1ke
r3dm1ke / proxy-default-value.js
Last active February 20, 2023 07:26
Using the Proxy API to set a default value for undefined object properties
const defaultValueHandler = {
get: (obj, property) =>
property in obj ? obj[property] : 'general kenobi'
}
const objectWithDefaultValue = new Proxy({}, defaultValueHandler);
objectWithDefaultValue.a = 'b';
console.log(objectWithDefaultValue.a); // b
const client = ClientBuilder()
.forBaseUrl('https://some-api.com/api/')
.withHeaders({Authorization: 'Bearer ABACABA'})
.usingAxios()
.build();
client.post('UpdateData', {data: 'new data'});
@r3dm1ke
r3dm1ke / client.js
Last active January 21, 2022 12:58
Builder pattern in JavaScript
function executeWithFetch(request) {
// EXERCISE FOR THE READER
}
function executeWithAxios(request) {
// EXERCISE FOR THE READER
}
function ClientBuilder() {
return {
@r3dm1ke
r3dm1ke / Main.java
Created January 28, 2020 18:17
StringBuilder example
class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
String info =
sb.append("Version: ")
.append(VersionSignleton.getVersion())
.append(" running on: ")
.append(System.getProperty("os.arch"))
.toString();