Skip to content

Instantly share code, notes, and snippets.

@SergeyNarozhny
SergeyNarozhny / Twins.js
Created February 20, 2018 06:29
Twin Strings. Two strings, a and b, are said to be twins only if they can be made equivalent by performing some number of operations on one or both strings...
function Twins(a, b) {
var result = Array(a.length),
swap = function(arr, index) {
var tmp = arr[index];
arr[index] = arr[index + 2];
arr[index + 2] = tmp;
return arr;
},
swapBack = function(str, index) {
var arr = str.split('');
@bithavoc
bithavoc / postgres-notify-trigger.sql
Last active August 15, 2024 15:15
I used this trigger to notify table changes via NOTIFY (migrating off RethinkDB)
CREATE OR REPLACE FUNCTION notify_trigger() RETURNS trigger AS $$
DECLARE
channel_name varchar DEFAULT (TG_TABLE_NAME || '_changes');
BEGIN
IF TG_OP = 'INSERT' THEN
PERFORM pg_notify(channel_name, '{"id": "' || NEW.id || '"}');
RETURN NEW;
END IF;
IF TG_OP = 'DELETE' THEN
PERFORM pg_notify(channel_name, '{"id": "' || OLD.id || '"}');
@mjackson
mjackson / resolvePromise.js
Last active September 9, 2018 08:23
An easy way to do async APIs in JavaScript that support both promises *and* callbacks!
// Here is a function that I use all the time when creating public
// async APIs in JavaScript:
const resolvePromise = (promise, callback) => {
if (callback)
promise.then(value => callback(null, value), callback)
return promise
}
// Sometimes I like to use callbacks, but other times a promise is
@jeromepl
jeromepl / Enhance.js
Last active April 6, 2020 20:26 — forked from sebmarkbage/Enhance.js
Higher-order Components in React with ES7
import { Component } from "React";
export default (ComposedComponent) => class extends Component {
constructor(props) {
super(props);
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
// This function:
function foo() {
var x = 5;
var y = 6;
debugger;
return x + y;
}
@edouard-lopez
edouard-lopez / 00.restore-galaxy-s-2-i9100.md
Last active March 25, 2024 03:24
Restore Galaxy S 2 i9100 (after breaking with cynogenmod update CM-13.x)

Requirements

sudo apt-get install heimdall{,-frontend} 
sudo apt-get install android-tools-adb
sudo apt-get install build-essential cmake zlib1g-dev qt5-default libusb-1.0-0-dev libgl1-mesa-glx libgl1-mesa-dev
@gaearon
gaearon / slim-redux.js
Last active September 7, 2025 15:41
Redux without the sanity checks in a single file. Don't use this, use normal Redux. :-)
function mapValues(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
result[key] = fn(obj[key], key);
return result;
}, {});
}
function pick(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
if (fn(obj[key])) {
@mzabriskie
mzabriskie / nps.js
Created September 8, 2015 19:01
Calculate Net Promoter Score
// Expects an array of scores from 0-10
// Example: nps([10, 9, 10, 4]); -> 50
// See http://www.medallia.com/net-promoter-score/
function nps(scores) {
var promoters = 0;
var detractors = 0;
for (var i=0, l=scores.length; i<l; i++) {
if (scores[i] >= 9) promoters++;
if (scores[i] <= 6) detractors++;
@mikechau
mikechau / video.jsx
Last active August 20, 2021 12:50
videojs react component
var React = require('react');
var cx = require('classnames');
var vjs = require('video.js');
var _forEach = require('lodash/collection/forEach');
var _debounce = require('lodash/function/debounce');
var _defaults = require('lodash/object/defaults');
var DEFAULT_HEIGHT = 800;
var DEFAULT_WIDTH = 600;
var DEFAULT_ASPECT_RATIO = (9 / 16);
@NuckChorris
NuckChorris / array.js
Last active February 12, 2018 19:47 — forked from pixelhandler/transforms.js
In Ember-CLI, transforms are located in app/transforms/name.js
// app/transforms/array.js
import Ember from 'ember';
import DS from 'ember-data';
export default DS.Transform.extend({
deserialize: function(value) {
if (Ember.isArray(value)) {
return Ember.A(value);
} else {
return Ember.A();