Skip to content

Instantly share code, notes, and snippets.

View mpneuried's full-sized avatar
🏠
Working from home

M. Peter mpneuried

🏠
Working from home
View GitHub Profile
@mpneuried
mpneuried / pubsub.coffee
Last active December 10, 2015 23:28
Simple namespaced Pub/Sub coffee class to use as extendable module for NodeJS and the browser.
# # PubSub
#
# Is a small helper to simply realize a pub/sub pattern to a coffee class.
#
# A namespaceing of the topics is also included.
# This means you can subscribe to `a` and also get the `a.b`. But if you subscribe to `a.b` you will not get a `a`.
#
# **required module**: `underscore`
#
class PubSub
@mpneuried
mpneuried / encode_decode_json_linebreak.coffee
Created January 28, 2013 15:35
encode json with linebreaks
o = {
"ID": "BgQfL",
"jsonSettings": "{\"weight\":49,\"height\":158,\"heartrate_max\":168,\"phonebusiness\":\"03423/667840\",\"usergroups\":\"Mitglied\",\"colors\":{\"plantargets\":[],\"usergroups\":[],\"pain\":[],\"damage\":[],\"activeplan_targets\":[]},\"comment\":\"Skoliose\r\n\r\nPatientin ist hypermobil\r\n\r\nbd Clavicula gebrochen; links 2011, recht 1997\",\"plantargets\":\"Muskelaufbau,Ausdauer\"}",
"firstname": "Heike",
"lastname": "Jüttner"
}
console.log( o.jsonSettings )
_json =
encode: ( s )->
@mpneuried
mpneuried / backbone_moveafter.coffee
Created February 11, 2013 08:51
Backbone code to move a model inside a collection after another model or to the top of the collection.
class SortableCollection extends Collection
###
## moveAfter
`collections.moveAfter( model, predecessor )`
Move a model after another model or if `predecessor` is `null` or `0` to the top of the model list
@param { Model | String | Number } model The model to move, it's `id` or the `cid`
@mpneuried
mpneuried / jsonreplace.coffee
Created February 11, 2013 09:08
Multiple replace of keys inside a object or JSON. *Useful for replacing id's with new id's.*
class JSONreplace
###
## _jsonreplacemap
`_jsonreplacemap( _json, map )`
Replace the values of a key inside of a json string.
@param { String | Object } _json A Stringfied JSON or a object witch will be stringfied.
@mpneuried
mpneuried / middleware.coffee
Created February 14, 2013 15:05
Middleware solution to run multiple tasks
class Middleware
constructor: ->
# define a shared object witch will be routed through all called methods
shared = {}
# run the middleware method to process all defined methods
# If one method fails by calling error the queue will be stopped
@middleware shared, @simpleCall, @addSomeMethods, ( err, shared )=>
if err
@mpneuried
mpneuried / clean_array.sql
Last active December 18, 2015 01:29
Workarround to store a clean array in mysql
# drop a existing test table
DROP TABLE IF EXISTS `test`;
# create the test table
CREATE TABLE `test` (
`_h` varchar(5) NOT NULL DEFAULT '',
`_set` text,
PRIMARY KEY (`_h`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@mpneuried
mpneuried / timeCountBy.coffee
Last active December 18, 2015 21:39
Get the count of timestamps based on a given timeframe.
###
**IMPORTANT** Make shure moment.js is available.
**ARGUMENTS**
* `timestamps` *( Array )*:An arry of timestamps.
* `type` *( String )*: A moment time type like `minute`, `hour`, `day`
* `pastCursor` *( Number )*: A number to define the maximum time to the past. If lower than `0` all given timestamps will be returned.
###
@mpneuried
mpneuried / crypto.coffee
Last active December 26, 2015 21:29
Simplify the data en/decryption in node.js
### USEAGE
crypto = require( "./crypto" )()
_crypted = crypto.crypt( "myPassowrd", "data123" )
crypto.decrypt( "myPassowrd", _crypted ) # data123
###
crypto = require( "crypto" )
@mpneuried
mpneuried / Backbone.getOrFetchId.coffee
Created November 21, 2013 10:16
Backbone collection extension to solve the problem of getting a model with eventually not exists within a collection. It also handles parallel fetches and distributes a single `.fetch()` to multiple requesters.
###
EXAMPLE USAGE
testColl = new Backbone.Collection.Extended()
# PROMISE USAGE
# define handlers
fnSuccess = ->
console.log( "SUCCESS", arguments )
@mpneuried
mpneuried / add-hosts.sh
Last active January 21, 2016 12:29
Scritp's to change /etc/hosts. Tahnks to Claus Witt, http://clauswitt.com/319.html.
DEFAULT_IP=127.0.0.1
IP=${2:-$DEFAULT_IP}
sed -ie "\| $1\$|d" /etc/hosts
echo "$IP $1" >> /etc/hosts
exit 0