Skip to content

Instantly share code, notes, and snippets.

View jpitchardu's full-sized avatar
🕶️

J. Pichardo jpitchardu

🕶️
View GitHub Profile
@jpitchardu
jpitchardu / keybase.md
Created July 21, 2017 18:57
Keybase GitHub integration

Keybase proof

I hereby claim:

  • I am pichardoj on github.
  • I am jpichardo (https://keybase.io/jpichardo) on keybase.
  • I have a public key ASCQ75Bt6nqP1GB6T-CVvSsWco0g48HHICv3uA9j0B5m-go

To claim this, I am signing this object:

@jpitchardu
jpitchardu / optional.js
Last active June 5, 2020 02:31
Optional API javascript
function optionalAccess(obj, path, def) {
const propNames = path.replace(/\]|\)/, "").split(/\.|\[|\(/);
return propNames.reduce((acc, prop) => acc[prop] || def, obj);
}
function proxyOptional(obj, evalFunc, def) {
const handler = {
get: function(target, prop, receiver) {
const res = Reflect.get(...arguments);
@jpitchardu
jpitchardu / deep-default.js
Created May 23, 2018 16:17
Code blocks for *Optional (null-safe) in Javascript* Medium article
const evaluation = obj && obj.prop1 && obj.prop1.prop2 && obj.prop1.prop2.prop3;
console.log( evaluation != null ? evaluation : "SomeDefaultValue" );
@jpitchardu
jpitchardu / optional.java
Created May 23, 2018 16:23
Java's Optional API Example
SomeClass object;
Optional.ofNullable(object)
.map(obj -> obj.prop1)
.map(obj -> obj.prop2)
.map(obj -> obj.prop3)
.orElse("SomeDefaultValue");
@jpitchardu
jpitchardu / null-safety.kt
Created May 23, 2018 16:24
Null-safety example in Kotlin
val object: SomeClass?
object?.prop1?.prop2?.prop3 ?: "SomeDefaultValue";
@jpitchardu
jpitchardu / deep-default.js
Last active May 23, 2018 16:28
Default value with null-checking in javascript
const evaluation = obj && obj.prop1 && obj.prop1.prop2 && obj.prop1.prop2.prop3;
console.log( evaluation != null ? evaluation : "SomeDefaultValue" ); // => "SomeDefaultValue"
@jpitchardu
jpitchardu / null-coalescence.cs
Created May 23, 2018 16:29
Null Coalescence in C#
SomeClass obj;
obj?.prop1?.prop2?.prop3 ?? "SomeDefaultValue";
@jpitchardu
jpitchardu / type-error.js
Created May 23, 2018 16:31
Type Error in JS
let obj;
console.log(obj.someProp); // => TypeError: Cannot read property 'someProp' of undefined
obj = null;
console.log(obj.someProp); // => TypeError: Cannot read property 'someProp' of null
@jpitchardu
jpitchardu / null-condition.js
Created May 23, 2018 16:31
Null-Conditional in JS
let obj;
console.log(obj && obj.someProp); // Prints undefined
@jpitchardu
jpitchardu / deep-null.js
Created May 23, 2018 16:32
Deep null-check in javascript
console.log( obj && obj.prop1 && obj.prop1.prop2 && obj.prop1.prop2.prop3 ); // => undefined