Skip to content

Instantly share code, notes, and snippets.

@sindresorhus
sindresorhus / esm-package.md
Last active April 25, 2024 08:14
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@fractaledmind
fractaledmind / sorter.rb
Last active April 16, 2024 15:08
A generalized sorting implementation that allows for multi-column, multi-directional, nil-handling, case_insensitive, normalized, and/or natural sorting (with tests)
require 'date'
class Sorter
def initialize(*instructions)
@defaults = {
direction: :ascending,
nils: :small,
accessor: :itself,
case_sensitive: true,
normalized: false,
# This hack is designed to prevent a thread from implicitly checking out an AR connection by
# just accessing ActiveRecord::Base.connection. The point of doing this is to ensure that threads
# don't tie up connections that they are not using, by making sure we're explicit about where we
# need to use an AR connection.
#
# See also http://tenderlovemaking.com/2011/10/20/connection-management-in-activerecord.html
ActiveSupport.on_load(:active_record) do
ActiveRecord::ConnectionAdapters::ConnectionPool # ensure loaded
@mperham
mperham / gist:7d763bdc42465caf17c7
Last active August 29, 2015 14:02
Rule execution

I want to build a system which uses rules. These rules will change hourly (i.e. "this rule is effective from 2pm to 6pm"). The rules must support arbitrarily complex logic on just 2-3 pre-defined variables and result in a boolean:

item.a > 10 && item.b == 0 || item.category == FOOTWEAR

These rules will be executed hundreds of times per second so I can't afford the overhead of plain old eval. I want to reload the current effective ruleset from the database hourly, precompile each rule's logic string and execute it via the quickest method possible. It might look something like this:

class Rule
@seanknox
seanknox / spec--capybara_helpers.rb
Created May 10, 2014 03:01
RSpec + Capybara + Sauce Labs + Selenium (local or remote)
module CapybaraHelpers
class << self
def local_ip
ipv4_addr_info.ip_address
end
def setup_selenium_remote
app_host ||= ENV.fetch('SELENIUM_APP_HOST', local_ip)
Capybara.server_host = app_host
Capybara.app_host = "http://#{app_host}:#{Capybara.current_session.server.port}"
@hadees
hadees / gist:7308571
Last active December 27, 2015 10:09 — forked from ericboehs/gist:7125105
Poltergeist hack to silence CoreText performance notes from phantomjs that works with billy.
module Capybara::Poltergeist
class Client
private
def redirect_stdout
prev = STDOUT.dup
prev.autoclose = false
$stdout = @write_io
STDOUT.reopen(@write_io)
prev = STDERR.dup

Rails 4 Differences

Chapter 1

No differences.

Chapter 2

  • root :to => "dashboard#index" is now root "dashboard#index"`
  • Need to permit parameters within Subscribem::AccountsController
@dypsilon
dypsilon / frontendDevlopmentBookmarks.md
Last active March 27, 2024 06:36
A badass list of frontend development resources I collected over time.
@tovodeverett
tovodeverett / database_cleaner.rb
Created June 19, 2013 19:40
Modified version of Avdi Grimm's solution for configuring RSpec and Capybara to use DatabaseCleaner
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = Capybara.current_driver == :rack_test ? :transaction : :truncation
DatabaseCleaner.start
end
@bgrins
bgrins / Log-.md
Last active August 1, 2023 16:32
Prevent errors on console methods when no console present and expose a global 'log' function.

Javascript log Function

Every time I start a new project, I want to pull in a log function that allows the same functionality as the console.log, including the full functionality of the Console API.

There are a lot of ways to do this, but many are lacking. A common problem with wrapper functions is that the line number that shows up next to the log is the line number of the log function itself, not where log was invoked. There are also times where the arguments get logged in a way that isn't quite the same as the native function.

This is an attempt to once and for all document the function that I pull in to new projects. There are two different options:

  • The full version: Inspired by the plugin in HTML5 Boilerplate. Use this if you are writing an application and want to create a window.log function. Additionally,