Skip to content

Instantly share code, notes, and snippets.

View matthieuprat's full-sized avatar

Matthieu Prat matthieuprat

View GitHub Profile
@matthieuprat
matthieuprat / README.md
Last active September 23, 2021 16:29
Until operator for Enzyme's shallow wrapper

Usage

import until from 'path/to/until'
import { shallow } from 'enzyme'

const EnhancedFoo = compose(
  connect(...),
  withHandlers(...),
 withContext(...)
@matthieuprat
matthieuprat / capybara-eventually.rb
Created October 7, 2016 14:49
Capybara eventually helper
def eventually(timeout: Capybara.default_max_wait_time, interval: 0.2)
Timeout.timeout(timeout) do
begin
next yield
rescue Minitest::Assertion, Capybara::ExpectationNotMet
sleep interval
retry
end
end
rescue Timeout::Error
# Adapted from http://thepugautomatic.com/2014/08/union-with-active-record/.
def self.union *scopes
sql = connection.unprepared_statement do
unions = scopes.map { |scope| "(#{scope.to_sql})" }.join(' UNION ')
"(#{unions}) AS #{table_name}"
end
from sql
end
@matthieuprat
matthieuprat / page_scope_methods.rb
Created March 9, 2016 10:25
Enhance Kaminari support of negative paddings
module Kaminari
module PageScopeMethods
def padding(num)
@_padding = num
new_offset_value = offset_value + num.to_i
if new_offset_value < 0
limit([limit_value + new_offset_value, 0].max).offset(0)
else
offset(new_offset_value)
end
#!/bin/sh
git diff-index --cached --name-only -z HEAD |
xargs -0 git grep --cached -I --name-only -z -E '[[:space:]]$' -- :!'*.slim' |
xargs -0 sed -i '' -E 's/[[:space:]]+$//'
fix_eol() {
for f; do
sed -i '' -E 's/[[:space:]]*$//' "$f"
done
}
git grep -I --name-only ' $' -- :/ :!'*.slim' | while read f; do fix_eol "$f"; done
@matthieuprat
matthieuprat / fix-eof.bash
Last active January 14, 2016 18:06
Ensure that a file has one and only one final new line
fix_eof() {
for f; do
: Add a final new line if needed.
[ "$(tail -c1 $f)" = "" ] || echo >> $f
: Remove extra final new lines.
while [ "$(tail -c2 $f)" = "" ] && [ $(wc -l < $f) -gt 1 ]; do
sed -i '' '$ d' $f
done
done
@matthieuprat
matthieuprat / array-shuffle.js
Last active June 13, 2018 06:58
Shuffle a JavaScript array
const shuffle = arr =>
arr.reduce((arr, elt) => {
arr.splice(~~(Math.random() * (arr.length + 1)), 0, elt)
return arr
}, [])
// Drop-in function to parallelize a stage of a Gulp pipeline.
//
// NB: order is not preserved.
//
// Before:
//
// gulp.src('./sass/**/*.scss')
// .pipe(sass()) // Processes files one-by-one.
// .pipe(gulp.dest('./css'))
//
@matthieuprat
matthieuprat / mongo-distinct-with-count.js
Created November 12, 2015 16:39
Mongo's distinct with occurrences
/**
* Like Mongo's db.collection.distinct but with counts for each field value.
* See https://docs.mongodb.org/v3.0/reference/method/db.collection.distinct.
*
* Example:
* > distinct('inventory', 'items.sku', { dept: 'A' })
* { "111" : 2, "333" : 1 }
*/
function distinct(collection, field, query) {
var map = {}