Skip to content

Instantly share code, notes, and snippets.

@xemasiv
Last active May 11, 2018 16:30
Show Gist options
  • Save xemasiv/bf30c837fea8b01599f7f8fa0709b690 to your computer and use it in GitHub Desktop.
Save xemasiv/bf30c837fea8b01599f7f8fa0709b690 to your computer and use it in GitHub Desktop.
Helpers
var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};
function success(pos) {
  var crd = pos.coords;

  console.log('Your current position is:');
  console.log(`Latitude : ${crd.latitude}`);
  console.log(`Longitude: ${crd.longitude}`);
  console.log(`More or less ${crd.accuracy} meters.`);
}
function error(err) {
  console.warn(`ERROR(${err.code}): ${err.message}`);
}
navigator.geolocation.getCurrentPosition(success, error, options);
var x = cheapRuler(lat1, 'kilometers');
x.distance([lat1, long1], [lat2, long2]);
OpenLocationCode.encode(lat, long)
OpenLocationCode.decode(lat, long)
  • Transports
    • HTTP
    • WebSockets
    • WebRTC
  • Namespaces
  • Contents *
  • Methods
  • LibraryDependencies *
  • PluginDependencies

Notes

// using https://packd.now.sh/[email protected]
var x = new pbf();
x.writeStringField(1, data.name);
x.writeStringField(2, data.surname);
var xbuff = x.finish();
function readData(tag, data, pbf) {
    if (tag === 1) data.name = pbf.readString();
    else if (tag === 2) data.surname = pbf.readString();
}
var xdata = new pbf(xbuff).readFields(readData, {});
var data = {name: 'Joshua', surname: 'Samonte'};
@xemasiv
Copy link
Author

xemasiv commented May 9, 2018

var data = {values: [1,2,3]};
var x = new pbf();
x.writePackedVarint(1, data.values);
var xbuff = x.finish();
function readData(tag, data, pbf) {
if (tag === 1) data.values= pbf.readPackedVarint();
}
var xdata = new pbf(xbuff).readFields(readData, {});
console.log(xdata);

@xemasiv
Copy link
Author

xemasiv commented May 9, 2018

var data = {values: ['-5555555555.454545',2,3]};
var x = new pbf();
x.writePackedDouble(1, data.values);
var xbuff = x.finish();
function readData(tag, data, pbf) {
if (tag === 1) data.values= pbf.readPackedDouble();
}
var xdata = new pbf(xbuff).readFields(readData, {});
console.log(xdata);

@xemasiv
Copy link
Author

xemasiv commented May 9, 2018

var data = {values: [true, false, true, true, false]};
var x = new pbf();
x.writePackedBoolean(1, data.values);
var xbuff = x.finish();
function readData(tag, data, pbf) {
if (tag === 1) data.values= pbf.readPackedBoolean();
}
var xdata = new pbf(xbuff).readFields(readData, {});
console.log(xdata);

@xemasiv
Copy link
Author

xemasiv commented May 9, 2018

var data = {values: [-6553565535, 6553565535, -6553565535]};
var x = new pbf();
x.writePackedSVarint(1, data.values);
var xbuff = x.finish();
function readData(tag, data, pbf) {
if (tag === 1) data.values= pbf.readPackedSVarint();
}
var xdata = new pbf(xbuff).readFields(readData, {});
console.log(xbuff.length, xdata.values);

@xemasiv
Copy link
Author

xemasiv commented May 11, 2018

group array values by n

const groupBy = (arr, n) => {
	var arr2 = [];
  for (var i = 0, j = 0; i < arr.length; i++) {
    if (i >= n && i % n === 0) j++;
    arr2[j] = arr2[j] || [];
    arr2[j].push(arr[i])
  }
  return arr2;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment