Skip to content

Instantly share code, notes, and snippets.

Mattias Petter Johansson mpj

View GitHub Profile
View hello.html
<html>
<head>
<link href="style.css" rel="stylesheet" />
</head>
<body>
<h3>Graph: Dragons</h3>
<div class="legend legendX"><div>Scariness ️→</div></div>
<div class="legend legendY"><div>Actual Power →</div></div>
<!-- http://bit.ly/neuralvanilla -->
<canvas id="wowacanvas" width="1000" height="1000"></canvas>
View curryingetc.js
const Pusher = require('pusher')
const secrets = require('./secrets')
const Encryptor = require('simple-encryptor')
const R = require('ramda')
const encrypt = R.curry((secret, data) => Encryptor(secret).encrypt(data))
const encryptWithSecret = encrypt(secrets.e2eSecret)
const trigger = R.curry((secrets, channel, event, message) => {
const client = new Pusher({
appId: secrets.appId,
View obs.js
const delay = require('delay')
async function* makeCountingObservable() {
let count = 0
while(true) {
if (count > 4) return
await delay(1000)
yield count++
}
}
const counter = makeCountingObservable()
View sniffix.js
//@ts-check
const util = require('util')
const fs = require('fs')
const path = require('path')
const writeFile = util.promisify(require('fs').writeFile)
const readFile = util.promisify(require('fs').readFile)
function createSniffer({
fixtureDirectory,
dummy = false
@mpj
mpj / stream-speech.js
Created Oct 8, 2018
Most simple example I could find for ongoing listening of microphone using the google cloud speech api.
View stream-speech.js
// Imports the Google Cloud client library
const speech = require('@google-cloud/speech')
const record = require('node-record-lpcm16')
const client = new speech.SpeechClient({
// generated service key downloaded from cloud console
credentials: require('./admin-key.json')
})
const sampleRateHertz = 16000
View hello.js
const fs = require('fs')
fs.readFile('hello.txt', 'utf8', (error, content) =>
content
.split('\n')
.filter(line => line.includes('dog'))
.forEach(line => console.log(line))
)
View order-total.js
function orderTotal(fetch, order) {
fetch('https://vatapi.com/v1/country-code-check?code=' + order.country)
return Promise.resolve(order.items.reduce((prev, cur) =>
cur.price * (cur.quantity || 1) + prev, 0))
}
module.exports = orderTotal
@mpj
mpj / order-total.js
Created Nov 26, 2017
Code from Unit testing in JavaScript Part 3 - Test runners
View order-total.js
function orderTotal(order) {
return order.items.reduce((prev, cur) => cur.price * (cur.quantity || 1) + prev, 0)
}
module.exports = orderTotal
View unit-testing-first-tests.js
function orderTotal(order) {
return order.items.reduce((prev, cur) => cur.price * (cur.quantity || 1) + prev, 0)
}
if (orderTotal({
items: [
{ 'name': 'Dragon candy', price: 2, quantity: 3 }
]
}) !== 6) {
throw new Error('Check fail: Quantity')
View functional.js
const fetch = require('node-fetch')
const fs = require('fs')
require('dotenv').config();
const accessToken = process.env.PATREON_ACCESS_TOKEN
const pageCount = 100
const url = `https://www.patreon.com/api/oauth2/api/campaigns/1137737/pledges` +
`?type=pledge&sort=created&page%5Bcount%5D=${pageCount}&access_token=${accessToken}`
const getPatreonData = function (url, pledges = []) {
You can’t perform that action at this time.