Skip to content

Instantly share code, notes, and snippets.

View danielrose7's full-sized avatar

Daniel Rose danielrose7

View GitHub Profile
@danielrose7
danielrose7 / noteStoreShape.js
Created October 24, 2017 14:56
Starting shape of new react/redux note app
notes: {
byId: { // object keyed by Id
noteId: {
id: noteId,
content: noteContent,
...
}
},
creationForm: {
@danielrose7
danielrose7 / react_redux_rails_configs_with_babel_env_jest_and_prettier.md
Last active January 17, 2018 00:05
Babelrc env preset for rails app with React + Redux without webpacker using Prettier, Jest and Browserify for create-react-app like setup

package.json

Here I've placed what I see as essentials. Feel free to add or substract if your see overkill for your sitruation! sinon seems like the only potential overkill to me at the time of this writing.

{
  "name": "app-name",
  "jest": {
    "verbose": true,
@danielrose7
danielrose7 / export_records_in_model_to_csv.rb
Created March 9, 2017 22:49
Exports records from a sample users model to csv by means of a controller action and `.csv` format. Ruby on Rails app
# Synchronously creates and sends records for download
# For scale, this would be better done async via a rake task, chron job, or something similar
# borrowed from https://gorails.com/episodes/export-to-csv
# sample controller
class UsersController < ApplicationController
def index
@users = User.all
@danielrose7
danielrose7 / query_to_csv.rb
Created March 9, 2017 22:16
This ruby script runs a SQL query and writes it as a CSV in the tmp folder using ruby's CSV library in a Ruby on Rails app
# run from console with `rails runner /path/to/this/file.rb`
# a timestamped file is placed in a Rails' app's tmp folder
require 'csv'
def file_path(name_of_file)
Rails.root.join('tmp', "#{name_of_file}_#{sanitize_time(Time.zone.now)}.csv") # don't edit name set on line 22
end
def sanitize_time(time)
@danielrose7
danielrose7 / paper_clip_extensions.rb
Created March 2, 2017 19:32
Verbose paperclip file extensions Basic: csv, pdf, txt | Microsoft: doc, docx, ppt, pptx, xls, xlsx | Images: gif, jpg, jpeg, png | Apple: pages, numbers
# If you want to add to your list, you can sniff using a binding.pry
# Look at the ActionDispatch::Http::UploadedFile object's Content-Type in the @header variable
validates_attachment :attachment,
allow_blank: false,
content_type: { content_type: ['image/jpg', #image
'image/jpeg', #image
'image/gif', #image
'image/png', #image
'application/pdf', #pdf
@danielrose7
danielrose7 / pre-commit-rubocop
Created February 2, 2017 19:16
Rubocop precommit git hook for ruby apps that need cleanup as new features are coming along
# Place in your project here: .git/hooks/pre-commit
# Runs all cops on newly added files
# Runs standard cops on files that have been modified
#!/usr/bin/env ruby
require 'rubocop'
def get_files(selector)
@danielrose7
danielrose7 / linearRegression.js
Last active November 24, 2016 05:25
Linear Regression in simple structure. Accepts a data object with x + y attributes. Returns a y = mx + b style line with an r squared value. Great for D3
const linearRegression = (data, y_attr, x_attr) => {
//separate data into x and y sets
let y_data = [];
let x_data = [];
for (let i = 0; i < data.length; i++) {
y_data.push(data[i][y_attr]);
x_data.push(data[i][x_attr]);
@danielrose7
danielrose7 / mergeSort.js
Last active October 21, 2016 06:48
Merge sort algorithm in javascript with mini-test suite. Breaks into pieces and then merges back together. Uncomment inner console logs to see it in action.
/* mergeSort
*
* basic idea is as follows:
* break an array of integrers into pieces recursively
* and merges it back together using its sorted pieces
*
* this implementation uses "take from the front put in
* the back" logic while merging
*
* find diagram at http://quiz.geeksforgeeks.org/merge-sort/
@danielrose7
danielrose7 / alphabetShift.js
Created October 21, 2016 06:04
Shifts letters to next letter in alphabet (c -> d, z -> a) and ensures vowels in new string are CAPS in Javascript ES6. Includes tests!
/* -- Problem --
* Have the function alphabetShift(str) take the str parameter
* being passed and modify it using the following algorithm.
* Replace every letter in the string with the letter following it
* in the alphabet (ie. c becomes d, z becomes a). Then capitalize
* every vowel in this new string (a, e, i, o, u) and finally return
* this modified string.
*/
/* -- Examples --
@danielrose7
danielrose7 / longestPalindrome.js
Last active October 5, 2016 17:33
Finds length of the longest substring of a string that is a palindrome in Javascript.
// Palindromes are the same forwards as in reverse
// As an example, if the input was “I like racecars that go fast”,
// the largest palindrom substring (racecar)
// would have the length of 7.
var longestPalindrome = function(s){
let biggest = 0;
const stringLength = s.length;