Skip to content

Instantly share code, notes, and snippets.

View vnglst's full-sized avatar
💭
Building 🪲

Koen van Gilst vnglst

💭
Building 🪲
View GitHub Profile
@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 / 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 () {
// 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')
})
})
{
"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}",
/* 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()
import request from './request'
const getUser = user => request(`https://api.github.com/users/${user}`)
export { getUser }
const rest = require('rest')
const mime = require('rest/interceptor/mime')
export default rest.wrap(mime)
@vnglst
vnglst / App.js
Last active February 6, 2017 09:06
import React, { Component } from 'react'
import './App.css'
import { getUser } from './api/github'
const renderLine = (user, key) => <li key={key}><b>{key}</b>: {user[key]}</li>
class App extends Component {
constructor (props) {
super(props)
this.state = { user: {} }
@vnglst
vnglst / LoaderHOC.js
Last active November 22, 2016 15:11
ReactCasts #1
import React, { Component } from 'react';
// Returns an empty "loader" div if empty,
// Return unchanged WrappedComponent if not empty
const LoaderHOC = (WrappedComponent) => {
return class LoaderOC extends Component {
render() {
return (!this.props.question)
? <div className='loading'></div>
: <WrappedComponent {...this.props} />
@vnglst
vnglst / ContactsApp.js
Created November 22, 2016 12:51
ReactCasts #1
// ....
import LoaderHOC from './LoaderHOC'
// ...
class ContactsApp extends Component {
// ...
}
export default LoaderHOC(ContactsApp);