Skip to content

Instantly share code, notes, and snippets.

View bartwttewaall's full-sized avatar

Bart Wttewaall bartwttewaall

View GitHub Profile
@bartwttewaall
bartwttewaall / generateUUID.ts
Created June 23, 2022 09:52
generate a UUID (taken from ThreeJS)
const lut: string[] = [];
function getLut(): string[] {
if (lut.length === 0) {
for (let i = 0, hex = ''; i < 256; i++) {
hex = i.toString(16);
lut.push(hex.length === 1 ? `0${hex}` : hex);
}
}
return lut;
@bartwttewaall
bartwttewaall / validate-matching-chars.ts
Created June 7, 2022 10:08
validate matching characters in string
const pairs = {
'{': '}',
'(': ')',
'[': ']',
'<': '>',
}
function isValid(input: string) {
const chars = input.split('');
let stack: string[] = [], last: string;
@bartwttewaall
bartwttewaall / createQueryUrl.twig
Created May 20, 2022 15:09
Hubspot Hubl Jinja implementation that sets the query parameter of a url depending on the arguments, with toggle and fullPath
{#
Examples in order:
createQueryUrl('tags', 'online') // {url}?tags=online
createQueryUrl('tags', 'in-store', true) // {url}?tags=online,in-store
createQueryUrl('tags', 'omnichannel', true) // {url}?tags=online,in-store,omnichannel
createQueryUrl('tags', 'in-store', true) // {url}?tags=online,omnichannel
createQueryUrl('tags', null) // {url}
#}
{% macro createQueryUrl(key, value, toggleValue = false, fullPath = false) %}
/**
* @param {HTMLVideoElement} videoElement
*/
export function waitForVideoPlaying(videoElement, duration = 500) {
return new Promise((resolve, _reject) => {
const seconds = duration / 1000;
const timeUpdateHandler = (evt) => {
if (evt.target.currentTime > seconds) {
evt.target.removeEventListener('timeupdate', timeUpdateHandler);
@bartwttewaall
bartwttewaall / orderby-multiple.twig
Created January 14, 2022 09:04
Order by two different conditions, but with a joined result in order to be able to use pagination
{% set currentDay = date(now)|date('Y-m-d') %}
{% set comingQuery = craft.entries.section('agenda')
.geplandeDatum(['and', '>= '~currentDay])
.orderBy('geplandeDatum ASC')
.ids()
%}
{% set pastQuery = craft.entries.section('agenda')
.geplandeDatum(['and', '<'~currentDay])
.orderBy('geplandeDatum DESC')
.ids()
@bartwttewaall
bartwttewaall / storedate.php
Created December 10, 2021 13:10
Convert a localized date from an html5 input[type=date] into an ISO8601 datetime string
<?php
$dateParam = $this->request->getRequiredBodyParam('date');
$timezone = new \DateTimeZone($dateParam['timezone']);
$date = new \DateTime($dateParam['date'], $timezone);
// don't know why, but the date needs the offset
// otherwise it gets stored as the previous day
$date->add(new \DateInterval('PT'.$date->getOffset().'S'));
$element->uid = StringHelper::UUID();
@bartwttewaall
bartwttewaall / array-to-object.twig
Created May 25, 2021 09:52
Creates an object with keys from an array and its translated values, then outputs it as a json string
{% set labels = ['welcome', 'ok', 'cancel'] %}
{% set obj = labels|reduce((carry, key) => carry|merge({ (key): key|t|ucfirst }), {}) %}
{{ obj|json_encode }}
{# as a macro #}
{% macro translateToJSON(labels) %}{% spaceless %}
{{ labels|reduce((carry, key) => carry|merge({ (key): key|t|ucfirst }), {})|json_encode }}
{% endspaceless %}{% endmacro %}
@bartwttewaall
bartwttewaall / twig-translations-to-vue.ts
Created May 25, 2021 08:26
Use Twig/Craft pre-translated strings in Vue components
/**
* translate(dictionary, key, params)
*
* Used to bridge twig translations into Vue files where we want to use the pre-translated values of keys in the vue component's template
@example
props: {
labels: { type: String }
},
setup() {
const state = reactive({
{% set address = { firstName: 'Stack', lastName: 'Overflow', address: 'here', zipCode: '1234' } %}
{% set addressLines = address|keys|reduce((carry, key) => carry|merge({ (key): attribute(address, key) }), []) %}
{# results in: [ 'firstName' => 'Stack', 'lastName' => 'Overflow', 'address'=> 'here', 'zipCode' => '1234' ] #}
{% set allowedKeys = ['firstName', 'lastName', 'address'] %}
{% for key, line in addressLines|filter((v, k) => k in allowedKeys) %}
{{ line }}<br/>
{% endfor %}
// mixin for setting the gutter size
@mixin row-gutters($size) {
margin-right: -$size;
margin-left: -$size;
> .col,
> [class*='col-'] {
padding-right: $size;
padding-left: $size;
}