Skip to content

Instantly share code, notes, and snippets.

View aercolino's full-sized avatar

Andrea Ercolino aercolino

View GitHub Profile
@aercolino
aercolino / Vending Machine Simulation
Last active April 16, 2020 06:01
Vending Machine Simulation in JavaScript for the browser.
Title of the Gist. (Fictitious file with a leading space.)
----
TEMPORARY DIRECTORY: This is where plugin / theme authors can store temporary files and directories. When a temporary directory expires, the garbage collector will delete it.
* '''tmp_dir_create'''( $expiration, $primary_file )
* ==> string $absolute_path | WP_Error $error
* Create a temporary directory specifically for the plugin/theme defined by $primary_file.
* '''tmp_dir_info'''( $absolute_path, $with_listing = false )
* ==> array $tmp_info | WP_Error $error
* Get info about the temporary directory at $absolute_path.
function convertDottedKeyToObject(name, value) {
return name.split('.').reverse().reduce((acc, key) => ({ [key]: acc }), value);
}
JSON.stringify(convertDottedKeyToObject('it.is.a', 'one liner'), null, 2)
"{
"it": {
"is": {
"a": "one liner"
import log from 'loglevel';
function logger(name, prefix) {
const result = log.getLogger(name);
if (!prefix) return result;
if (prefix === result.prefix) return result;
result.prefix = prefix;
const original = {
trace: result.trace.bind(result),
debug: result.debug.bind(result),
ACCENTED_CHARS_ACUTE = %r([\u00E1\u00C1\u00E9\u00C9\u00ED\u00CD\u00F3\u00D3\u00FA\u00DA\u00FD\u00DD])
ACCENTED_CHARS_GRAVE = %r([\u00E0\u00C0\u00E8\u00C8\u00EC\u00CC\u00F2\u00D2\u00F9\u00D9])
ACCENTED_CHARS_CIRCUMFLEX = %r([\u00E2\u00C2\u00EA\u00CA\u00EE\u00CE\u00F4\u00D4\u00FB\u00DB])
ACCENTED_CHARS_DIERESIS = %r([\u00E4\u00C4\u00EB\u00CB\u00EF\u00CF\u00F6\u00D6\u00FC\u00DC])
ACCENTED_CHARS_TILDE = %r([\u00E3\u00C3\u00F1\u00D1\u00F5\u00D5])
ACCENTED_CHARS_CEDILLA = %r([\u00E7\u00C7])
ACCENTED_CHARS = '\u00C0-\u00C4\u00C7-\u00CF\u00D1-\u00D6\u00D9-\u00DD\u00E0-\u00E4\u00E7-\u00EF\u00F1-\u00F6\u00F9-\u00FD'
@aercolino
aercolino / elasticsearch-jquery.js
Last active December 4, 2018 19:33
Searching into ElasticSearch with jQuery
// the important bit it JSON.stringify, because elasticsearch doesn't work without it
jQuery.post('http://elasticsearch.example.com:9200/activity/_search/', JSON.stringify({
"query": {
"term": {
"activity.id": "1234"
}
}
}), function (data) {
function processEvent(event, context, callback) {
const storableEvent = makeEventStorable(event);
storableEvent.created_at = new Date();
console.log('Calling MongoDB Atlas from AWS Lambda: ', JSON.stringify(storableEvent));
if (cachedDb) {
console.log('- Reusing cached connection');
createDoc(cachedDb, storableEvent, callback);
return;
}
@aercolino
aercolino / #nzymes plugin-suspension.md
Last active May 2, 2017 16:47
Plugin Suspension: suspend and resume any WP plugin (draft)

Plugin Suspension: suspend and resume any WP plugin

This is a set of two dynamic enzymes useful for suspending and resuming another plugin.

Work in progress...

Example

This would be the content of a post of yours. The thingy plugin would be filtering content as usual.

@aercolino
aercolino / Backend services in Angular2.md
Last active March 8, 2017 16:47
Simulation of a login functionality in Angular2, token based.

In this gist I show a simple setup for helping me build the frontend of a backend functionality before programming the latter. In this case it is the login functionality.

On the login page there are three buttons to allow me to login with different credentials, so that I can see what the server would tell me in each case. That will guide me to understand how I want to program the frontend.

A couple of seconds after clicking each button I'll get a response from the server.

  1. the first button will cause an error response, which I show in a feedback field on the login page.
  2. the second and third buttons will cause an OK response, which I store in the auth key of the local storage system.