Skip to content

Instantly share code, notes, and snippets.

@ervinne13
ervinne13 / debouncing.js
Last active March 20, 2018 12:27
Debouncing / On Finished Typing in jQuery
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
@ervinne13
ervinne13 / Laravel Auto Increment Reset
Last active March 29, 2022 09:47
Laravel reset auto increment for tests with databases where you can't do a migrate:refresh
<?php
namespace <Your namespace here>;
use Illuminate\Support\Facades\DB;
/**
*
* @author Ervinne Sodusta <[email protected]>
*/
@ervinne13
ervinne13 / ExceptionToHttpResponseHandlerRouter.php
Last active September 19, 2018 13:02
Separating render from exceptions
// app/Exceptions/Handlers.php
public function render($request, Exception $exception)
{
if ($this->exceptionToHttpResponseHandlerRouter->isHandleable($exception)) {
return $this->exceptionToHttpResponseHandlerRouter->handle($exception);
}
if (!$this->isApiCall($request)) {
return parent::render($request, $exception);
} else {
@ervinne13
ervinne13 / RequireIPIsWhitelisted.php
Last active September 25, 2018 02:31
Middleware for Blocking access to routes unless IP is whitelisted.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class RequireIPIsWhitelisted
{
@ervinne13
ervinne13 / answer_to_questions.js
Last active October 23, 2018 02:25
Mapping answers to questions
let questions = [{
"form_criteria_id": "46",
"field_name": "pumpstation_general",
"field_label": "General repairs required?",
"mandatory": false,
"action": "dropdown",
"input_type": [{
"id": 7,
"text": "Yes",
"value": "yes",
@ervinne13
ervinne13 / style-modifying.js
Last active February 3, 2019 06:52
Refactoring JavaScript: Separation of Concerns - Styles on JS
let foo = document.getElementById('foo');
foo.style.color = 'red';
foo.style.border = '2px solid #000';
foo.style.paddingLeft = '3px';
foo.style.marginTop = '3px';
foo.style.fontSize = '1.2em';
foo.style.fontStyle = 'italic';
@ervinne13
ervinne13 / style-modifying-fixed-by-toggling.js
Created February 3, 2019 06:53
Refactoring JavaScript: Separation of Concerns - Styles on JS (Fixed)
function toggleFooHasError() {
let foo = document.getElementById('foo');
if (foo.classList.contains('has-error')) {
foo.classList.remove('has-error');
} else {
foo.classList.add('has-error');
}
}
toggleFooHasError();
@ervinne13
ervinne13 / html-on-js.js
Last active February 3, 2019 08:11
Refactoring JavaScript: Separation of Concerns - Markup Code on JS
// ... variable declaration and setting of widgetID, widgetTitle, etc.
let widgetText = "JavaScript is Quirky";
let widgetHTML = '<div id=' + widgetId + '>' +
'<h2 id="' + widgetTitleId + '">' + widgetTitle + '</h2>' +
'<div id="' + widgetContentId + '" style="' + widgetContentStyles + '">' +
widgetText +
'</div>' +
'</div>';
document.getElementById("widget-container").innerHTML = widgetHTML;
@ervinne13
ervinne13 / html-on-js-fix-by-blue-imp.html
Last active February 3, 2019 08:10
Refactoring JavaScript: Separation of Concerns - Markup Code on JS Fix
<script src="js/tmpl.min.js"></script>
<script type="text/x-tmpl" id="tmpl-specific-widget-x">
<div id="specific-widget-x" class="specific-widget-x">
<h2>{%=o.title%}</h2>
<div class="specific-widget-x-body-classes">
{%=o.text%}
</div>
</div>
</script>
@ervinne13
ervinne13 / main.js
Last active March 5, 2019 07:19
Composing functions by piping
const { validate, normalize, persist } = require('./user-module')
const pipe = require('./pipe')
const saveUser = (user) => {
return pipe(
validate,
normalize,
persist
)(user)
}