Skip to content

Instantly share code, notes, and snippets.

@nexcra
nexcra / .block
Created April 13, 2020 05:25
Click to zoom with table and map in D3
license: mit
@nexcra
nexcra / mouse.js
Created September 13, 2019 13:22 — forked from electricg/mouse.js
Mouse position relative to document and element
// Which HTML element is the target of the event
function mouseTarget(e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
return targ;
}
@nexcra
nexcra / mouse_position.js
Created September 13, 2019 13:22 — forked from hendriklammers/mouse_position.js
Javascript: Canvas mouse position
// Add mousePosition on the Canvas element
(function () {
function mousePosition(event) {
var totalOffsetX = 0,
totalOffsetY = 0,
coordX = 0,
coordY = 0,
currentElement = this,
mouseX = 0,
mouseY = 0;
@nexcra
nexcra / js-beautify-from-java.java
Created July 19, 2019 14:31 — forked from fedochet/js-beautify-from-java.java
Calling javascript beautifier from java
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.InputStreamReader;
public class JavascriptBeautifierForJava {
// my javascript beautifier of choice
private static final String BEAUTIFY_JS_RESOURCE = "beautify.js";
@nexcra
nexcra / MyServiceImpl.java
Created September 8, 2018 13:30 — forked from oillio/MyServiceImpl.java
bi-directional stream gRPC with RxJava
public class MyServiceImpl extends DeviceServiceGrpc.DeviceServiceImplBase {
@Override
public StreamObserver<GetLocationRequest> getLocations(StreamObserver<GetLocationResponse> responseObserver) {
RequestBridge<GetLocationRequest> request = new RequestBridge<>();
ResponseBridge response = new ResponseBridge((ServerCallStreamObserver) responseObserver);
request.map(this::doGetLocation)
.subscribe(response);
return request;
}
@nexcra
nexcra / StoreInViewModel.js
Created October 20, 2015 05:15 — forked from alperg/StoreInViewModel.js
ExtJS 5 store in viewmodel
In file MyApp/store/Menu.js
Ext.define('MyApp.store.Menu', {
extend: 'Ext.data.Store',
requires: [
'MyApp.model.menu.Root'
],
model: 'MyApp.model.menu.Root',
@nexcra
nexcra / find-in-json.js
Created October 20, 2015 04:11 — forked from iwek/find-in-json.js
Searching through JSON
//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
@nexcra
nexcra / AssociativeReader.js
Created October 20, 2015 02:21 — forked from twasink/AssociativeReader.js
ExtJS has some really nice support out of the box for converting JSON data to model objects. However, it only supports array-based associations, and doesn't support maps/associative arrays. The AssociativeReader - included here, along with two demo models and sample data - provides a way to do that.
/**
* A variant of the JSON reader. Instead of reading arrays, where each record in the array field
* has an 'id' property, it reads objects - aka associative arrays. The key of the entry will be the
* array.
*
* So where the JSON reader would like data like this:
* [ { id: '1', property: 'foo' }, { id: '2', property: 'bar' } ]
*
* the associative reader likes data like this:
* { '1': { property: 'foo' }, '2': { property: 'bar' } }
@nexcra
nexcra / deep-reduce.js
Created October 20, 2015 02:11 — forked from JamieMason/deep-reduce.js
Perform a deeply recursive reduce on a JSON-encodable JavaScript Object hierarchy.
/**
* Perform a deeply recursive reduce on a set of JSON, or a JSON-encodable Object hierarchy.
*
* @param {Array|Object} collection
* @param {Function} fn
* @param {*} memo
* @return {*}
*/
function deepReduce(collection, fn, memo) {