Skip to content

Instantly share code, notes, and snippets.

@mieszko4
Last active April 20, 2016 15:20
Show Gist options
  • Save mieszko4/19707bae7e127019953635ab3aa40c0d to your computer and use it in GitHub Desktop.
Save mieszko4/19707bae7e127019953635ab3aa40c0d to your computer and use it in GitHub Desktop.
JsMeetup - Streaming in javascript - Functional approach
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