Skip to content

Instantly share code, notes, and snippets.

Avatar

Tracy Lum talum

View GitHub Profile
@talum
talum / combinations.js
Created Nov 28, 2019
Combinations in JS
View combinations.js
// return all combinations of array elements for size n
// ex) combinations([1,2,3], 2) => [[1,2], [2,3], [1,3]]
// use backtracking & recursion
// iteration: for each element
// add element
// recurse with element
// remove element
// continue
@talum
talum / topologicalSort.js
Created Nov 26, 2019
topologicalSort.js
View topologicalSort.js
/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {number[]}
*/
var findOrder = function(numCourses, prerequisites) {
let adjList = {};
let indegree = {};
let topologicalOrder = [];
View ellipsis.jsx
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import Button from 'lib/shared-react-components/buttons/Button'
export default class Ellipsis extends PureComponent {
render () {
if (!this.props.visible) return null
return (
@talum
talum / invoice.ex
Created May 28, 2019
Invoice changeset with constraint
View invoice.ex
defmodule Registrar.Billing.Invoice do
import Ecto.Changeset
def changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :amount_due,
   :due_date,
   :scheduled_send_date,
   :email
@talum
talum / invoice.ex
Created May 28, 2019
invoice changeset
View invoice.ex
defmodule Registrar.Billing.Invoice do
import Ecto.Changeset
def changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :amount_due,
   :due_date,
   :scheduled_send_date
  ])
@talum
talum / invoice.ex
Created May 28, 2019
Closed invoice
View invoice.ex
defmodule Registrar.Billing.Invoice do
import Ecto.Changeset
def mark_closed_changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :closed_at
  ])
  |> validate_required([
   :closed_at
@talum
talum / invoice.ex
Created May 28, 2019
Paid Changeset
View invoice.ex
defmodule Registrar.Billing.Invoice do
import Ecto.Changeset
def mark_paid_changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :paid_at
  ])
  |> validate_required([
   :paid_at
View changeset_demo.ex
iex> attributes = %{amount_due: 3000, due_date: "2019-04-30", scheduled_send_date: "2019-04-25",
iex> closed_at: "2019-05-01"}
iex> changeset = Registrar.Billing.Invoice.changeset(%Invoice{}, attributes)
iex> #Ecto.Changeset<
  action: nil,
  changes: %{amount_due: 3000, due_date: ~D[2019-04-30], scheduled_send_date: ~D[2019-04-25]},
  errors: [],
  data: #Registrar.Billing.Invoice<>,
  valid?: true
>
View invoice.ex
defmodule Registrar.Billing.Invoice do
# assume that we have an Ecto.Schema defined with all the fields this module needs to know about
import Ecto.Changeset
def changeset(%Invoice{} = invoice, attrs \\ %{}) do
  invoice
  |> cast(attrs, [
   :amount_due,
   :due_date,
   :scheduled_send_date
@talum
talum / invoices_controller.rb
Created May 28, 2019
Invoices Controller 2
View invoices_controller.rb
class InvoicesController < ApplicationController
  def update
  # The developer reasons that adding a timestamp to the params to
  # leverage the existing code will do the trick
  if params[:cancel]
  invoice_params[:closed_at] = Time.current
  end