Last active
April 20, 2016 15:20
-
-
Save mieszko4/19707bae7e127019953635ab3aa40c0d to your computer and use it in GitHub Desktop.
JsMeetup - Streaming in javascript - Functional approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
const fs = require('fs'); | |
const intercomDrinks = require('intercom-drinks').default; | |
const radius = 100000; //in meters | |
const latitude = 53.3381985; | |
const longitude = -6.2592576; | |
const onlineSource = 'https://gist.githubusercontent.com/brianw/19896c50afa89ad4dec3/raw/6c11047887a03483c50017c1d451667fd62a53ca/gistfile1.txt'; | |
const offlineSource = 'input.jsonl'; | |
request(onlineSource, (error, allData, data) => { //1. read | |
// fs.readFile(offlineSource, 'utf8', (error, data) => { | |
var customers = data | |
.trim('') | |
.split('\n') //2. split | |
.map(row => { | |
var customer = void 0; | |
try { | |
customer = JSON.parse(row); //3. parse customer | |
} catch (e) { | |
return void 0; //ignore | |
} | |
try { | |
customer = intercomDrinks.validateCustomer(customer); //4. validate | |
} catch (e) { | |
return void 0; //ignore | |
} | |
return customer; | |
}) | |
.filter(customer => customer !== void 0)// filter out invalid customers | |
.filter(customer => intercomDrinks.isNearbyCustomer(latitude, longitude, radius, customer)) // 5. calculate distance and filter | |
; | |
/* SORT | |
// 6. sort | |
customers = customers.sort((a, b) => { | |
return a.user_id - b.user_id; //ascending | |
}); | |
*/ | |
const output = customers | |
.map(customer => `${customer.user_id}:${customer.name}`) // 7. pick name and id | |
.join('\n') //8. output | |
; | |
console.log(output); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment