Skip to content

Instantly share code, notes, and snippets.

@seamusleahy
seamusleahy / twig-indentless.php
Last active April 16, 2018 14:05
Twig tag to remove indentation at the start of each line: {% indentless %}...{%endindetless %}
<?php
/**
* Remove whitespace at the start of each line.
*
* Based on Twig's Twig_TokenParser_Spaceless class
*/
class TwigIndentlessTokenParser extends Twig_TokenParser
{
/**
<LocalizationProvider messages={myLocalizationMessages}>
<Foo>
<h1><LocalizationText key="welcomeMessage" /></h1>
</Foo>
</LocalizationProvider>
@seamusleahy
seamusleahy / Angular.$q.all().js
Last active September 2, 2016 03:48
How to deal with multiple promises with native code and several popular libraries. https://seamusleahy.com/how-to-resolve-multiple-promises/
module.controller('MyCtrl', function($scope, $q) {
const promise1 = somethingThatReturnsAPromise();
const promise2 = somethingElseThatReturnsAPromise();
const promise3 = yetAnotherCallThatReturnsAPromise();
// We create a wrapper promise for all the them using $q.all which follows Promise.all of taking an array of promises
const consolidatedPromise = $q.all([promise1, promise2, promise3]);
consolidatedPromise.then(function (promiseValues) {
console.log('Promise 1:', promiseValues[0]); // The value from `promise1.then(function(value){})`
console.log('Promise 2:', promiseValues[1]); // The value from `promise2.then(function(value){})`
@seamusleahy
seamusleahy / fetch-response.js
Last active May 4, 2019 19:17
Using the Fetch HTML5 API to POST a form and get back JSON data.
// 3. Use the response
// ================================
responsePromise
// 3.1 Convert the response into JSON-JS object.
.then(function(response) {
return response.json();
})
// 3.2 Do something with the JSON data
.then(function(jsonData) {
console.log(jsonData);
@seamusleahy
seamusleahy / test.js
Created June 27, 2017 03:27
How to test the content inside of a react-bootstrap <Modal>
import { mount, ReactWrapper } from 'enzyme';
import { Modal } from 'react-bootstrap';
// The Enzyme selector query will not work because the actual inside HTML of the modal lives else where in the DOM
// via portals. See: https://react-bootstrap.github.io/react-overlays/#portals
function getWrapperForModalPortal(parentWrapper) {
// The Modal instance provides a reference to HTML content:
// [1] https://github.com/react-bootstrap/react-bootstrap/blob/dddb9b966c7f3e79495be5c5730f7cce04813aaf/src/Modal.js#L228
// [2] https://github.com/react-bootstrap/react-bootstrap/blob/419051127d913bbb149e81ed221500108ba0d7f3/test/ModalSpec.js#L32
const modalInstance = parentWrapper.find(Modal).getNode()._modal.getDialogElement();
@seamusleahy
seamusleahy / Jest Enzym Error
Created June 28, 2017 15:04
How to fix the Enzym warning when mounting to component to the body
$ npm test
> [email protected] test /home/user/project
> jest
PASS src/__tests__/WithErrorExample.test.jsx
Example
✓ Test (108ms)
Test Suites: 1 passed, 1 total