Payton.Codes

Musings on life, games, and code

Blog

  • Test spies in PHP

    I think that I wrote my first unit tests in Ruby with RSpec, back in the day. But I learned most of my testing knowledge from working with mocha and chai in JavaScript. One of the things that I learned from these tools is that being able to express your test logic in clear, nearly-natural…

  • An iframe without a url

    Sometimes you need to display html inside an iframe, but it’s not at a URL. Perhaps you got the markup from an API endpoint or generated it yourself. Maybe the markup is from a different domain and you need to be able to manipulate its DOM without cross-origin errors. For all these reasons, I created…

  • Declarative vs. Imperative Soup

    I remember not too long ago (I think this was about the time that React first came out) I was trying to understand what people meant when they said that it was “declarative” vs. “imperative”. Looking up the terms at the time didn’t enlighten me much. Mostly articles seemed to focus on the difference between…

  • JavaScript: Mocking Window

    When I code in JavaScript I try to avoid using the window or document objects as much as possible, but sometimes there’s just no getting around them. Bootstrapping data from a server (eg: WordPress’s wp_localize_script), manipulating the DOM directly (eg: jQuery), or listening to viewport resize events all require touching the global window object. Why…

  • Copying files… sometimes

    File this one under “tools that probably only I will find useful”. In the course of my normal job I need to copy files to a synchronized directory on my computer (something like a DropBox folder). The files are JavaScript code that’s been transpiled and copying them to the synchronized directory is what deploys them…

  • Partial application and making tea

    Partial application is like making tea. The person making the tea needs two pieces of information: what kind of tea, and how many people to serve. We can save time by knowing one of those pieces of information before we begin. Let’s say we have a tea shop. Whenever a new person comes in with…

  • Avoiding Tightly-Coupled REST APIs

    As you might have read, WordPress is getting a powerful REST API. For a few years now I’ve been writing endpoints for the WordPress.com version of this API, as well as few REST APIs in other languages (Ruby, JavaScript). As I’m also a big fan of unit testing, I’d like to share a pattern that…

  • get_deep_value in PHP

    Ever have an array in PHP that looks like this? It can be annoying to pull deep values from such an array because if you access an array key that doesn’t exist, PHP will throw an exception. You could check each level of the data using array_key_exists() like this: But that would take forever. PHP’s…

  • The “why” of writing tests

    Unit testing is part of my normal development flow now. For the most part, I try to write my code in a “TDD” or “BDD” manner, meaning: I write my tests first, explaining how I want my public methods to behave, then follow it up with the actual code to make the tests pass. Years…

  • Using React stateless components for quick prototyping

    When you’re building a new app using React it’s nice to start laying out all the components you’ll want to use inside the components that you’re building. Unfortunately this can be a little slow because for each new component you have to: Create a new file Import that file Include React in the file Export…

  • Re-ordering Objects in a List

    In an old version of an app I wrote, I had a list of objects which were in a specific order. I wrote the app to operate using a REST API, so when you changed one of the objects, the client sent a PUT request to the server with the new data for that object…

  • ES2015 module re-exports

    Today’s episode of TIL (“Today I Learned”) is: Did you know you can re-export modules in ES2015 (aka: ES6, aka: new JavaScript)? That is, you can import a module and export it again all in one line of code. Why would you want to do this? Well, first you need to know about the way…