Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
🐝 Bzz!

Christopher Robert Van Wiemeersch cvan

💭
🐝 Bzz!
View GitHub Profile
View cypress-debug.js
const enableCypressDebug = win => {
if (process.env.DEBUG === '1') {
win.localStorage.setItem('debug', 'cypress:*');
}
};
beforeEach(enableCypressDebug);
View nodemon-safe-kill.js
if (process.env.NODE_ENV === 'development') {
// This helps for nodemon to not be prematurely killed.
// See nodemon's docs: https://github.com/remy/nodemon/blob/master/README.md#controlling-shutdown-of-your-script
// Adapted from source: https://www.benjiegillam.com/2011/08/node-js-clean-restart-and-faster-development-with-nodemon/
const gracefulShutdown = cb => {
setTimeout(() => {
process.exit(0);
cb();
}, 5000);
};
@cvan
cvan / strip-trailing-slashes-in-url.js
Created Jul 28, 2020
strip trailing slashes in a URL (JavaScript)
View strip-trailing-slashes-in-url.js
const stripTrailingSlashesInUrl = str => str.replace(/\/+$/g, '');
@cvan
cvan / strip-html-comments.js
Created Jul 21, 2020
strip html comments javascript
View strip-html-comments.js
function stripHtmlComments(html) {
// Remove HTML comments (useful for removing developer-facing comments from production HTML markup).
return html.replace(/<!--[\s\S]*?(?:-->)/g, '');
}
@cvan
cvan / parse-a-single-qs-value.js
Created Jul 7, 2020
JS regex one-liner for query-string value parsing
View parse-a-single-qs-value.js
// Just replace `placeholder_name` with the actual key for which you want its value.
var placeholderName = decodeURIComponent((window.location.search.match(/[?&]placeholder_name=([^&]+)/)||['', ''])[1]);
@cvan
cvan / useMedia-hook.js
Created Jul 1, 2020
useMedia - React hook for `window.matchMedia` for matching media-query
View useMedia-hook.js
function useMedia(mediaQuery) {
const match = () => {
if (!window.matchMedia) {
return false;
}
return window.matchMedia(mediaQuery).matches;
};
const [value, set] = useState(match);
View theinhardt-fontfix.scss
@mixin text-crop($line-height: 1.3, $top-adjustment: 0px, $bottom-adjustment: 0px) {
// Configured in Step 1
$top-crop: 4;
$bottom-crop: 14;
$crop-font-size: 36;
$crop-line-height: 1.2;
// Apply values to calculate em-based margins that work with any font size
$dynamic-top-crop: max(($top-crop + ($line-height - $crop-line-height) * ($crop-font-size / 2)), 0) / $crop-font-size;
$dynamic-bottom-crop: max(($bottom-crop + ($line-height - $crop-line-height) * ($crop-font-size / 2)), 0) / $crop-font-size;
@cvan
cvan / prevent-body-scroll.js
Created May 26, 2020
using `body-scroll-lock` npm package to prevent body scroll on iOS
View prevent-body-scroll.js
import React, { useEffect } from 'react';
import * as bodyScrollLock from 'body-scroll-lock';
const disableBodyScroll = bodyScrollLock.disableBodyScroll;
const enableBodyScroll = bodyScrollLock.enableBodyScroll;
export default function Page() {
const appRef = React.createRef();
useEffect(() => {
@cvan
cvan / export-301-redirects.js
Created May 12, 2020
copy URL redirects from 301 Redirects WordPress plugin
View export-301-redirects.js
// Paste into your DevTools Console.
list = $$('input[name*="301_redirects[request]"]').map(src => src.value ? `${src.value} ${src.closest('div').querySelector('input[name*="301_redirects[destination]"]').value}`
: '').filter(Boolean).sort().join('\n');
copy(list);
@cvan
cvan / object-flatten.js
Created Apr 16, 2020
flatten a nested JavaScript object to a flatten one-level object
View object-flatten.js
// Adapted from source: https://stackoverflow.com/a/19204980
const flatten = (function(isArray, wrapped) {
return function(table) {
return reduce('', {}, table);
};
function reduce(path, accumulator, table) {
if (isArray(table)) {
const length = table.length;
You can’t perform that action at this time.