Skip to content

Instantly share code, notes, and snippets.

View choonkending's full-sized avatar

Ken Ding choonkending

View GitHub Profile
@choonkending
choonkending / printTiming.js
Last active January 15, 2017 23:15
Print navigation timing in your browser
const events = [ 'domLoading', 'domInteractive', 'domContentLoadedEventEnd', 'domComplete', 'loadEventEnd' ];
const startEvent = 'navigationStart';
const timing = performance.timing;
const compose = (f, g) => x => f(g(x));
const timeFromNavigationStart = event => differenceInSeconds(timing[event], timing[startEvent]);
const differenceInSeconds = (t1, t2) => (t1 - t2) / 1000;
const sanitizeTime = t => (t > 0) ? t : 'N/A - Has not fired yet - please run console log again';
{
"listing": {
"features": {
"bedrooms": {
"value": 3
}
}
}
}
const Features = ({ bedrooms, bathrooms }) => (
<div>
<span>Beds: </span><span>{ bedrooms }</span>
<span>Baths: </span><span>{ bathrooms }</span>
</div>
);
const ComponentThatWrapsFeatures = ({ data }) => {
const features = getFeatures(data);
return features && <Features {...features} />
};
const Option = x => (x === undefined || x === null) ? None : Some(x);
const Some = x => ({
map: f => Some(f(x)),
flatMap: f => f(x),
fold: (ifEmpty, f) => f(x)
});
const None = {
map: f => None,
const getBedrooms = data =>
Option(data.listing)
.flatMap(listing => Option(listing.features))
.flatMap(features => Option(features.bedrooms))
.map(bedrooms => bedrooms.value)
.fold(() => 0, v => v);
const Left = x => ({
map: f => Left(x),
fold: (ifLeft, ifRight) => ifLeft(x)
});
const Right = x => ({
map: f => Right(f(x)),
fold: (ifLeft, ifRight) => ifRight(x)
});
const getUserFromLocalStorage = keyName => {
const str = localStorage.getItem(keyName);
return JSON.parse(str);
};
const doSomethingWithUserData = () => {
const cage = getUserFromLocalStorage('cage');
// do wonderful things
};
const getUserFromLocalStorage = keyName => {
const str = localStorage.getItem(keyName);
try {
return JSON.parse(str);
} catch (e) {
return "Invalid or Corrupted User Data";
}
};