Skip to content

Instantly share code, notes, and snippets.

View marcus-at-localhost's full-sized avatar
💭
☕

Marcus marcus-at-localhost

💭
☕
View GitHub Profile
// @see https://stackoverflow.com/a/31536517/814031
const items = json3.items
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(items[0])
const csv = [
header.join(','), // header row first
...items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
].join('\r\n')
console.log(csv)
@marcus-at-localhost
marcus-at-localhost / 1info.md
Last active October 30, 2022 10:40
[JSON Path with Alpine CSP build in Postman Visualizer] Query JSON Response with JSON Path

JSON Path with Alpine.js CSP build in Postman Visualizer

Query JSON Response with JSON Path

2022-04-30_13-58-39_2

To work more easily with Postman JSON results, I implemented JSONPath in a Postman Visualizer, to query the results.

JSONPath is to JSON what XPath is to XML, and it helps to dig into large nested objects and get what you are looking for faster.

First I was using just a jQuery version that can be found in the examples and that worked just fine. (JSONpath Visualizer | Postman Team Collections | Postman API Network)

@marcus-at-localhost
marcus-at-localhost / write-csv.php
Created February 22, 2022 13:00
[Create CSV With Headers if not Exist] https://csv.thephpleague.com/
<?php
use League\Csv\Writer;
use League\Csv\Reader;
$records = 0;
if (file_exists($filename)) {
$csv = Reader::createFromPath($filename, 'r');
$csv->setHeaderOffset(0);
@marcus-at-localhost
marcus-at-localhost / bootstrap-validation.js
Last active October 30, 2023 14:53
[Bootstrap Form Validation HTMX Extension] See example: https://jsfiddle.net/hxjvw62L/ #htmx #bootstrap
htmx.defineExtension('bs-validation', {
onEvent: function (name, evt, data) {
let form = evt.detail.elt;
// check if trigger attribute and submit event exists
// for the form
if(!form.hasAttribute('hx-trigger')){
// set trigger for custom event bs-send
form.setAttribute('hx-trigger','bs-send');
@marcus-at-localhost
marcus-at-localhost / htmx-compilers.js
Created February 21, 2022 13:32
[HTMX Miniframework] Compiler for htmx - Work in Progress
/**
* -- HTMX Mini Framework
* @see https://eager.io/blog/how-to-decide-when-your-code-should-run/
* @see https://stackoverflow.com/a/44057187/814031
*/
;
(function() {
var results = []
<form hx-post="/example" hx-ext="disable-submit">
  <button type="submit" class="hx-disable button-default">Save</button>
</form>
htmx.defineExtension('disable-submit', {
  onEvent: function (name, evt, data) {
 let elt = evt.detail.elt;
@marcus-at-localhost
marcus-at-localhost / instantsearch.js
Created February 8, 2022 22:40
[Instantsearch.js - Persist Existing GET Parameters in Router]
const algoliaAppId = 'APP_ID';
const algoliaApiKey = 'API_KEY';
const algoliaIndex = 'instant_search';
const search = instantsearch({
indexName: algoliaIndex,
algoliasearch(algoliaAppId, algoliaApiKey),
routing: {
router: instantsearch.routers.history({
@marcus-at-localhost
marcus-at-localhost / geo_trigger.sql
Created January 27, 2022 18:10
[Trigger to write to another table]
CREATE DEFINER=`xyz`@`%` TRIGGER `places_after_insert` AFTER INSERT ON `places` FOR EACH ROW BEGIN
#Update coords table
INSERT INTO places_coords (id,coords) VALUES (NEW.id,POINT(new.lng,new.lat)) ON DUPLICATE KEY UPDATE coords=POINT(new.lng,new.lat);
END
CREATE DEFINER=`xyz`@`%` TRIGGER `places_after_update` AFTER UPDATE ON `places` FOR EACH ROW BEGIN
#Update coords table
INSERT INTO places_coords (id,coords) VALUES (NEW.id,POINT(new.lng,new.lat)) ON DUPLICATE KEY UPDATE coords=POINT(new.lng,new.lat);
END
@marcus-at-localhost
marcus-at-localhost / foreach.blade.php
Last active November 8, 2021 17:31
[Count Up in `foreach` Loop in Blade Template]
<?php
// https://stackoverflow.com/a/45562847/814031
@foreach($categories as $category)
<li><a href="#tab_c{{ $loop->iteration }}" role="tab" data-toggle="tab">{{$category->name}}</a></li>
@endforeach
@marcus-at-localhost
marcus-at-localhost / bootstrap-5-sass-mixins-cheat-sheet.scss
Created September 6, 2021 06:50 — forked from anschaef/bootstrap-5-sass-mixins-cheat-sheet.scss
All New Bootstrap 5 Sass Mixins [Cheat sheet with examples]
/* -------------------------------------------------------------------------- */
// All Bootstrap 5 Sass Mixins [ Cheat sheet ]
// Updated to Bootstrap v5.1.x
// @author https://anschaef.de
// @see https://github.com/twbs/bootstrap/tree/main/scss/mixins
// @see https://github.com/twbs/bootstrap/blob/main/scss/_variables.scss
/* -------------------------------------------------------------------------- */
// Options
// @see https://getbootstrap.com/docs/5.1/customize/options/