Skip to content

Instantly share code, notes, and snippets.

View kieraneglin's full-sized avatar
😎

Kieran kieraneglin

😎
  • British Columbia, Canada
View GitHub Profile
@kieraneglin
kieraneglin / regex.js
Last active April 3, 2017 14:04
[JS OSX Duplicate File Regex] #tags: js, regex
new RegExp(/\(([\d^)]+)\)(?!\S)/igm)
@kieraneglin
kieraneglin / safe-access-object.js
Last active February 28, 2019 13:01
Safely Access Deeply Nested Objects In JavaScript
// Problem: how do you access a deeply nested object that you're not certain exists?
// if(object.user && object.user.post && object.user.post[0] && object.user.post[0].comment) {
// return object.user.post[0].comment
// }
// and that's horrifying. Here's something better
const idx = (props, object) => props.reduce((prefix, val) => (prefix && prefix[val]) ? prefix[val] : null, object)
// Usage:
nestedObj = {...}