Skip to content

Instantly share code, notes, and snippets.

View mmontes11's full-sized avatar
🦭
Incubating mariadb-operator

Martin Montes mmontes11

🦭
Incubating mariadb-operator
View GitHub Profile
@mmontes11
mmontes11 / cheatsheet.js
Last active February 28, 2020 11:25
JS Cheatsheet
// var
(function() {
console.log(foo); //undefined, foo is hoisted
var foo = "foo";
console.log(bar); // ReferenceError: bar is not defined
})();
(function() {
function foo() {
var a = (b = 0);
@mmontes11
mmontes11 / index-by.js
Last active February 3, 2020 10:43
Index an array by multiple fields
const indexBy = (value, ...fields) => {
if (fields.length === 0) {
return value;
}
const [currentField, ...rest] = fields;
const newValue = mapObjectValues(value, Array.isArray, indexByField, currentField);
return indexBy(newValue, ...rest);
};
Array.prototype.indexBy = function(...fields) {
return indexBy(this, ...fields);
@mmontes11
mmontes11 / map-object-values.js
Last active February 3, 2020 10:37
Maps object values recursivelly
const mapObjectValues = (value, shouldMap, mapFn, ...args) => {
if (shouldMap(value)) {
return mapFn(value, ...args);
}
return {
...Object.keys(value).reduce(
(acc, k) => ({
...acc,
[k]: mapObjectValues(value[k], shouldMap, mapFn, ...args),
}),
@mmontes11
mmontes11 / index-by-field.js
Last active February 3, 2020 14:52
Index an array by a single fields
const indexByField = (array, field) =>
array.reduce((acc, it) => {
const key = it[field];
const value = acc[key] ? [...acc[key], it] : [it];
return {
...acc,
[key]: value,
};
}, {});
@mmontes11
mmontes11 / algorithms.js
Last active August 22, 2020 12:41
JS algorithms
// Remove duplicates from an array
(function () {
Array.prototype.unique = function () {
return Array.from(new Set(this));
};
console.log([1, 2, 3, 4, 4, 5].unique()); // [1, 2, 3, 4, 5]
})();
// Memoized function
(function () {
@mmontes11
mmontes11 / dht.js
Last active March 20, 2020 22:51
DHT sensor
import Promise from "bluebird";
import dhtSensorLib from "node-dht-sensor";
export class DHTSensor {
constructor(dhtType, gpio, measurementLocation) {
this.dhtType = dhtType;
this.gpio = gpio;
this.measurementLocation = measurementLocation;
}
toString() {
@mmontes11
mmontes11 / thing-socket-handler.js
Last active March 20, 2020 18:37
Thing socket handler
import os from "os";
import { Log } from "../util/log";
import { SensorHandler } from "./sensorHandler";
import { LEDHandler } from "./ledHandler";
import config from "../config";
export class SocketHandler {
constructor(io) {
this.io = io;
this.numConnections = 0;
@mmontes11
mmontes11 / backend-socketio-middleware.js
Last active March 20, 2020 19:07
Backend Socket.io middleware
import SocketIO from "socket.io";
import jwt from "jsonwebtoken";
import _ from "underscore";
import { logError } from "../utils/log";
import { ThingModel } from "../models/thing";
export const setupSocketIO = server => {
const io = new SocketIO(server);
io.use(async (socket, next) => {
const {
@mmontes11
mmontes11 / frontend-realtime-component.jsx
Created March 20, 2020 18:42
Frontend realtime component
import React, { useEffect } from "react";
import PropTypes from "prop-types";
import { handleDataParams } from "hocs/dataParams";
import store from "config/store";
import RealTimeParamsPanel from "containers/RealTimeParamsPanel";
import { startRealTimeData, finishRealTimeData, resetData } from "actions/data";
import { reset } from "actions/common";
import Charts from "containers/Charts";
import { REALTIME } from "constants/chartTypes";
import { THING, TYPE } from "constants/params";
@mmontes11
mmontes11 / frontend-realtime-thunks.js
Last active March 21, 2020 15:28
Frontend realtime thunks
import {
DATA_REQUEST,
DATA_REQUEST_SUCCESS,
DATA_REQUEST_ERROR,
ADD_DATA_ITEM,
RESET_DATA,
} from "constants/actionTypes/data";
import { SocketController } from "helpers/socketController";
import { RESET } from "constants/actionTypes/common";
import * as fromState from "reducers";