Skip to content

Instantly share code, notes, and snippets.

Mattias Petter Johansson mpj

Block or report user

Report or block mpj

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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 = []) {
View snippet.js
someOrder = {
items: [
{ name: 'Dragon food' , price: 8, quantity: 8 },
{ name: 'Dragon cage (small)', price: 800, quantity: 2 },
{ name: 'Shipping', price: 40, shipping: true }
]
}
orderTotal = (order) => {
totalNormalItems =
View 1headers.txt
X-Patreon-Event: pledges:create
Cf-Ray: 3aed45fb2c8851b2-SJC
User-Agent: python-requests/2.5.2 CPython/3.6.2 Linux/3.13.0-132-generic
Content-Type: application/json
Cf-Visitor: {"scheme":"https"}
Accept-Encoding: gzip
Accept: */*
Total-Route-Time: 0
Content-Length: 25417
Via: 1.1 vegur
You can’t perform that action at this time.