Skip to content

Instantly share code, notes, and snippets.

@erikologic
erikologic / extract.js
Created March 2, 2023 22:42
Calculate border crosses with Google Maps Timeline data
/*
I need to monitor my UK border crosses for residential purposes.
This script will calculate whenever I moved more than 5 degrees, either on lat or long, from one datapoint to the other, since the start of 2017.
*/
// First of all, go to Google Takeout and fetch your Location data.
// Then, for each day, extract the last position for the day with the following command (using gojq)
// cat Records.json | gojq -r '[.locations[] | select((.timestamp | sub("\\.[0-9]+";"") | strptime("%Y-%m-%dT%H:%M:%SZ")) | mktime >= 1483228800) | {latitudeE7, longitudeE7, accuracy, source, timestamp}] | group_by(.timestamp[:10]) | map(max_by(.timestamp)) | map({latitude: (.latitudeE7 / 10000000), longitude: (.longitudeE7 / 10000000), accuracy: .accuracy, source: .source, timestamp: .timestamp})' > grouped.json
// *** You might want to change the Unix timestamp after mktime to filter on your needs
@erikologic
erikologic / inject_behaviour.js
Last active July 25, 2023 21:02
Jest: mocking externally dependencies
// TestSubject.js
const ExternalDependency = require('./ExternalDependency')
module.exports = class TestSubject {
constructor () {
this.externalDependency = new ExternalDependency()
}
isExternalDependencyMocked () {
return this.externalDependency.isMocked
@erikologic
erikologic / Error.js
Created August 12, 2017 02:41
CustomError in NodeJS
module.exports = function CustomError(message) {
this.name = this.constructor.name
this.message = message
Error.captureStackTrace(this, this.constructor)
}
module.exports.prototype.inspect = function () {
return this.stack
}
//file substack.js
module.exports = function() {
console.log('main entry point');
};
module.exports.details = function() {
console.log('more detailed functionality');
};
function getDatasetfromJsonData(jsonData){
return jsonData[0].scores;
};
function getLabels(dataset) {
return Object.keys(dataset)
};
function getData(dataset) {
return Object.keys(dataset).map(function(key) {
// ...IN THE CALLER
import JSONDataConverter from './JSONDataConverter';
<RadarChart data={JSONDataConverter(data)} width={width} height={height} redraw />
var JSONDataConverter = (function () {
var getDatasetfromJsonData = function(jsonData){
return jsonData[0].scores;
};
var getLabels = function(dataset) {
return Object.keys(dataset)
};
var getData = function(dataset) {
class DataConverterContainer extends Component {
getDatasetfromJsonData(jsonData){
return jsonData[0].scores;
};
getLabels(dataset){
return Object.keys(dataset)
};
getData(dataset){
@erikologic
erikologic / avgNull.js
Created September 9, 2015 12:48
Javascript: average which accept null values
16 lines (14 sloc) 0.36 kB
function average(data) {
/*Can't find an average function in JS, made one
This function is able to handle null values!!!*/
var count = null;
var sum = null;
for (i=0; i<data.length; i++) {
if (data[i]) {
count++;