Skip to content

Instantly share code, notes, and snippets.

View davidicus's full-sized avatar

David Conner davidicus

View GitHub Profile
@davidicus
davidicus / randomNumber.js
Created September 2, 2018 03:17
create a random number between two values
// Returns a random integer between min (include) and max (include)
Math.floor(Math.random() * (max - min + 1)) + min;
/////////////////////////////
// Useful examples:
/////////////////////////////
// 0 -> 10
Math.floor(Math.random() * 11);
@davidicus
davidicus / newRepo.sh
Created September 2, 2018 02:47
curl command to create a remote repo via github api
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
# Remember replace USER with your username and REPO with your repository/application name!
git remote add origin [email protected]:USER/REPO.git
git push origin master
@davidicus
davidicus / whatProps.js
Created April 13, 2018 20:37
Log what props are being passed to component
import _ from 'lodash';
componentWillReceiveProps (nextProps) {
const changedProps = _.reduce(this.props, function (result, value, key) {
return _.isEqual(value, nextProps[key])
? result
: result.concat(key)
}, [])
console.log('changedProps: ', changedProps)
}
@davidicus
davidicus / truncateString.js
Created April 12, 2018 11:20
Shorten string to 12 word length
const str = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in risus elementum quam gravida faucibus. Vestibulum blandit sollicitudin enim nec tempus. Nunc eu mattis massa. Nulla facilisi. Proin vel lobortis mauris, ut aliquet orci. Phasellus vel fermentum risus, at interdum sapien. Nullam ante velit, venenatis quis consectetur dapibus, dictum sed urna. Vestibulum quis mi molestie est iaculis ullamcorper. Phasellus felis lorem, vehicula eu venenatis id, auctor nec eros. Nullam purus justo, efficitur ac finibus vitae, sodales vel tellus. Nunc accumsan massa ut diam ullamcorper ultricies. Integer odio ex, laoreet malesuada felis vel, vehicula malesuada leo. Nullam accumsan ullamcorper orci, nec vestibulum nulla eleifend ac. Duis leo odio, viverra eget laoreet vitae, molestie ut eros. Morbi vitae dui interdum ipsum hendrerit facilisis a ut dolor.`
str.split(' ').slice(0, 12).join(' ');
//-> returns "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in risus elementum"
@davidicus
davidicus / bash-colors.md
Created March 30, 2018 11:35 — forked from iamnewton/bash-colors.md
The entire table of ANSI color codes.

Regular Colors

Value Color
\e[0;30m Black
\e[0;31m Red
\e[0;32m Green
\e[0;33m Yellow
\e[0;34m Blue
\e[0;35m Purple
@davidicus
davidicus / eager-prefetching-async-data-example.js
Created March 29, 2018 18:58 — forked from bvaughn/eager-prefetching-async-data-example.js
Advanced example for eagerly prefetching async data in a React component.
// This is an advanced example! It is not intended for use in application code.
// Libraries like Relay may make use of this technique to save some time on low-end mobile devices.
// Most components should just initiate async requests in componentDidMount.
class ExampleComponent extends React.Component {
_hasUnmounted = false;
state = {
externalData: null,
};
@davidicus
davidicus / getStylesheetStyles.js
Created March 1, 2018 17:23
A snippit to grab specific css property using JS
const tableBody = document.querySelector('.dataitems-table tbody');
// can grab pseudo element styles by replacing null with selector
window.getComputedStyle(tableBody, null).getPropertyValue("padding-right");
@davidicus
davidicus / hourMinuteSecondFormat.js
Created February 2, 2018 21:09
Format hour, minutes, and seconds
const leadingZero = (num) => `0${num}`.slice(-2);
const formatTime = (date) =>
[date.getHours(), date.getMinutes(), date.getSeconds()]
.map(leadingZero)
.join(':');
formatTime(new Date(1514727120000))
// -> "07:32:00"
@davidicus
davidicus / formatDate.js
Created January 19, 2018 03:42
Some Time formatting methods off the date object
var now = Date.now();
var then = new Date(now);
var date1 = then.getFullYear();
var date2 = then.getDate();
var date3 = then.getMonth();
var date4 = then.getHours();
var date5 = then.getMinutes();
var date6 = then.getSeconds();
var date7 = then.getMilliseconds();
@davidicus
davidicus / fetchWithErrorHandling.js
Created January 10, 2018 21:31
Fetch api and make error fail loudly
fetch(path.join(__dirname, `/api/structures/overview`), {
method: `GET`,
credentials: 'include'
})
.then(response => {
const json = response.json();
if (response.status >= 200 && response.status < 300) {
return json;
} else {
return json.then(Promise.reject.bind(Promise));