Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonathan Petitcolas jpetitcolas

View GitHub Profile
@jpetitcolas
jpetitcolas / gist:5967887
Created Jul 10, 2013
Encode/decode a base64 encoded string in PostGreSQL
View gist:5967887
-- Decoding
SELECT CONVERT_FROM(DECODE(field, 'BASE64'), 'UTF-8') FROM table;
-- Encoding
SELECT ENCODE(CONVERT_TO(field, 'UTF-8'), 'base64') FROM table;
@jpetitcolas
jpetitcolas / pythonizeJsonDump.js
Last active Oct 21, 2020
Pythonise JS JSON dump
View pythonizeJsonDump.js
/*
Python and Node don't stringify JSON object the same way.
Python adds some spaces after `:` and `,`, causing some trouble if
you try to compute the same hashes across languages.
Here is a Node function turning a Node generated JSON string into
its Python equivalent.
*/
export const pythonizeJsonDump = (jsonDump) =>
jsonDump.replace(/(?!\B"[^"]*)(:|,)(?![^"]*"\B)/g, '$1 ');
@jpetitcolas
jpetitcolas / parsing-binary-file.go
Last active Jul 27, 2020
How to parse a binary file in Go? Snippet based on MoPaQ SC2 replay parsing. Related blog post: http://www.jonathan-petitcolas.com/2014/09/25/parsing-binary-files-in-go.html
View parsing-binary-file.go
package main
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"os"
)
@jpetitcolas
jpetitcolas / even-odd-row.twig
Created Jan 8, 2013
How to differenciate odd and even rows with Twig?
View even-odd-row.twig
<table>
{% for record in records %}
<tr class="record{% if loop.index is divisibleby(2) %}even{% else %}odd{% endif %}">
<td>{{ record.id }}</td>
<td>{{ record.title }}</td>
</tr>
{% endfor %}
</table>
@jpetitcolas
jpetitcolas / camelize.js
Created Jan 8, 2013
Camelize a string in Javascript. Example: camelize("hello_world") --> "HelloWorld"
View camelize.js
/**
* Camelize a string, cutting the string by separator character.
* @param string Text to camelize
* @param string Word separator (underscore by default)
* @return string Camelized text
*/
function camelize(text, separator) {
// Assume separator is _ if no one has been provided.
if(typeof(separator) == "undefined") {
@jpetitcolas
jpetitcolas / snippet.sh
Created Feb 24, 2020
Getting AWS CloudWatch Insight results from CLI
View snippet.sh
aws logs start-query \
--profile [PROFILE] \
--log-group-name [GROUP_NAME] \
--start-time `date --date="-30 minutes" "+%s"` \
--end-time `date "+%s"` \
--query-string 'fields @message | limit 50' \
| jq '.queryId' \
| xargs -I{} aws --profile [PROFILE] logs get-query-results --query-id {} \
| jq '.results | .[][0].value'
@jpetitcolas
jpetitcolas / snippet.js
Created Jan 11, 2020
Marking all tickets as completed on an Asana board column
View snippet.js
const COLUMN_NAME = "Deployed to Prod"
var column = [...document.querySelectorAll(".BoardBody-column")].filter(
column =>
column.querySelector('.BoardColumnHeader-name') &&
column.querySelector('.BoardColumnHeader-name').textContent == COLUMN_NAME
)[0]
Array.from(column.querySelectorAll(".BoardCard")).map(card => {
card.querySelector(".BoardCard-dropdownButton").click()
@jpetitcolas
jpetitcolas / pre-commit
Created Aug 12, 2014
My standard Git pre-commit hook
View pre-commit
#!/bin/sh
#
# Based on http://nrocco.github.io/2012/04/19/git-pre-commit-hook-for-PHP.html post
#
# Do not forget to: chmod +x .git/hooks/pre-commit
BAD_PHP_WORDS='var_dump|die|todo'
BAD_TWIG_WORDS='{{ dump(.*) }}'
EXITCODE=0
@jpetitcolas
jpetitcolas / uncamelize.js
Created Jan 8, 2013
How to uncamelize a string in Javascript? Example: "HelloWorld" --> "hello_world"
View uncamelize.js
/**
* Uncamelize a string, joining the words by separator character.
* @param string Text to uncamelize
* @param string Word separator
* @return string Uncamelized text
*/
function uncamelize(text, separator) {
// Assume separator is _ if no one has been provided.
if(typeof(separator) == "undefined") {
@jpetitcolas
jpetitcolas / javascript.json
Created Mar 5, 2018
React Functional Component Visual Code Snippet
View javascript.json
{
"Create new functional component": {
"prefix": "_compo",
"body": [
"import React from 'react';",
"import PropTypes from 'prop-types';",
"",
"export const $1 = ({$2}) => (",
" $3",
");",
You can’t perform that action at this time.