Skip to content

Instantly share code, notes, and snippets.

View vnglst's full-sized avatar
💭
Building 🪲

Koen van Gilst vnglst

💭
Building 🪲
View GitHub Profile
/* eslint-env jest */
const github = require('../github')
// A simple example test
describe('#getUser() using Promises', () => {
it('should load user data', () => {
return github.getUser('vnglst')
.then(data => {
expect(data).toBeDefined()
@vnglst
vnglst / request.js
Created February 6, 2017 09:43
Mocked version of request.js
const fs = require('fs')
const request = (url) => new Promise((resolve, reject) => {
// Get userID from supplied url string
const lastSlash = url.lastIndexOf('/')
const userID = url.substring(lastSlash + 1)
// Load user json data from a file in de subfolder for mock data
fs.readFile(`./src/api/__mockData__/${userID}.json`, 'utf8', (err, data) => {
if (err) reject(err)
// Parse the data as JSON and put in the key entity (just like the request library does)
{
"login": "vnglst",
"id": 3457693,
"avatar_url": "https://avatars.githubusercontent.com/u/3457693?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/vnglst",
"html_url": "https://github.com/vnglst",
"followers_url": "https://api.github.com/users/vnglst/followers",
"following_url": "https://api.github.com/users/vnglst/following{/other_user}",
"gists_url": "https://api.github.com/users/vnglst/gists{/gist_id}",
// The exact same test using async/await
describe('#getUser() using async/await', () => {
it('should load user data', async () => {
const data = await github.getUser('vnglst')
expect(data).toBeDefined()
expect(data.entity.name).toEqual('Koen van Gilst')
})
})
@vnglst
vnglst / 03-compound-component.js
Last active February 1, 2018 08:49
Egghead React Kent C. Dodds
function ToggleOn({on, children}) {
return on ? children : null
}
function ToggleOff({on, children}) {
return on ? null : children
}
function ToggleButton({on, toggle, ...props}) {
return (
<Switch on={on} onClick={toggle} {...props} />
)
@vnglst
vnglst / delay.js
Last active January 15, 2018 18:43 — forked from daliborgogic/delay.js
Handy snippet: Node.js Async/Await delay
'use strict'
const timeout = ms => new Promise(res => setTimeout(res, ms))
function convinceMe (convince) {
let unixTime = Math.round(+new Date() / 1000)
console.log(`Delay ${convince} at ${unixTime}`)
}
async function delay () {
@vnglst
vnglst / example.js
Last active January 30, 2018 13:59
JSON Schema function validation
const api = {};
const ajv = new Ajv({
coerceTypes: true,
useDefaults: true,
allErrors: true,
});
api.api = (schema, handler) => {
const validator = ajv.compile(schema);
@vnglst
vnglst / shrinkpdf.sh
Created April 2, 2018 14:56
Reduce size of pdf files from command line (requires gs, ghostscript)
#!/bin/sh
# http://www.alfredklomp.com/programming/shrinkpdf
# Licensed under the 3-clause BSD license:
#
# Copyright (c) 2014, Alfred Klomp
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
// conditionally render children based on loading state
const AsyncPopup = ({ hasLoaded = false, children }) => (
<View>{hasLoaded ? children : null}</View>
);
// example usage
const App = () => (
<View>
<AsyncPopup hasLoaded>
<DataComponent data={someData} />
// AsyncPopup using renderProps
const AsyncPopupV2 ({ hasLoaded = false, renderContent }) => (
<View>{hasLoaded ? renderContent() : null}</View>
);
// example usage:
const App = () => (
<View>
<AsyncPopupV2
hasLoaded