Skip to content

Instantly share code, notes, and snippets.

View zoetrope69's full-sized avatar
🦫
beavering away

z zoetrope69

🦫
beavering away
View GitHub Profile
@zoetrope69
zoetrope69 / js-magic.js
Created September 17, 2019 14:31
Example of missing values in destructuring an array
const arr = ['a', 'b', 'c', 'd'];
const [,,,result] = arr;
console.log(result);
// 'd'
@zoetrope69
zoetrope69 / combine-images-imgix.js
Created June 24, 2019 13:01
Combine two images in Imgix
/*
We're using the watermark feature in Imgix to combine these images together:
https://docs.imgix.com/apis/url/watermark
If we simplified this component in terms of design we could remove this...
*/
const combineImages = ({ foreground, background, width, height }) => {
const devicePixelRatio = (window && window.devicePixelRatio) || 1;
const newForegroundUrl = replaceImgixParams(foreground, {
@zoetrope69
zoetrope69 / VisuallyHidden.css
Last active October 7, 2020 10:38
VisuallyHidden React component
/* https://github.com/alphagov/govuk-frontend/blob/master/src/helpers/_visually-hidden.scss */
.visuallyHidden {
position: absolute !important;
width: 1px !important;
height: 1px !important;
margin: 0 !important;
padding: 0 !important;
overflow: hidden !important;
clip: rect(0 0 0 0) !important;
clip-path: inset(50%) !important;
@zoetrope69
zoetrope69 / dnt-tracking-example.js
Created June 15, 2018 10:35
Example of choose not to send information to Google Analytics when Do Not Track is enabled
if (!isDoNotTrackEnabled) {
window.ga('GA_APP_TRACKER.example', 'foo')
}
@zoetrope69
zoetrope69 / dnt-templating-example.js
Created June 15, 2018 10:34
Example of how you could choose not to send Google scripts if Do Not Track is enabled
const trackingScripts = `
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
</script>
`
const html = `
@zoetrope69
zoetrope69 / dnt-client-side-function.js
Last active July 5, 2018 14:42
Example of detecting Do Not Track on the client side
function isDoNotTrackEnabled () {
const doNotTrackOption = (
window.doNotTrack ||
window.navigator.doNotTrack ||
window.navigator.msDoNotTrack
)
if (!doNotTrackOption) {
return false
}
@zoetrope69
zoetrope69 / dnt-server-side-function.js
Created June 15, 2018 10:32
Example function that can be used to detect Do Not Track preferences server side
function isDoNotTrackEnabled (request) {
const doNotTrackHeader = request.header('DNT')
if (!doNotTrackHeader) {
return false
}
if (doNotTrackHeader.charAt(0) === '1') {
return true
}
@zoetrope69
zoetrope69 / dnt-express-example.js
Created June 15, 2018 10:30
Example of using express to get the Do Not Track preferences from a request
app.get('/', (request, response) => {
console.log(request.header('DNT'))
})
@zoetrope69
zoetrope69 / gm-example.js
Last active July 21, 2017 11:18
GM Example
const gm = require('gm');
gm('/path/to/inputFile.png')
.monochrome()
.write('/path/to/outputFile.png', () => {
// Image created
});
@zoetrope69
zoetrope69 / printer-example.js
Last active July 21, 2017 11:22
Printer code example
const SerialPort = require('serialport')
const serialPort = new SerialPort('/dev/ttyUSB0', { baudrate: 19200 })
const Printer = require('thermalprinter');
const printer = new Printer(serialPort);
printer.on('ready', () => {
printer.printImage(imagePath).print();
});