Skip to content

Instantly share code, notes, and snippets.

function wrap (fn, args, thisArg) {
return function () {
return Promise(function (resolve, reject) {
fn.apply(thisArg, (args || []).concat([function (err, res) {
if (err) reject(err)
else resolve(res)
}]))
})
}
}
var _ = require('lodash')
var utils = require('./mongo-utils')
var docs = require('./random-fixtures')(50000)
var db = utils.connect('mongodb://localhost/test')
db
.then(utils.collection('articles'))
.then(utils.clear)
@naholyr
naholyr / dafuq.md
Last active August 29, 2015 13:56
That one was nice

The bug

/*
  The model is something like:
  Person: { name: String }
  Car: { name: String, driver: {type: Schema.Types.ObjectId, ref: 'Person'} }
*/

// Somewhere we do this…

server

GET /echo HTTP/1.1
lower-case: 1
UPPER-CASE: 2
Mixed-Case: 3
Host: 127.0.0.1:1337
Custom-Header: 4
Connection: keep-alive
@naholyr
naholyr / 1-line.sh
Last active August 17, 2017 18:12
Add "use strict" everywhere
find -name '*.js'|grep -v '^./node_modules'|while read f;do if (head -n3 "$f"|grep -Fv 'use strict'>/dev/null);then echo "$f";sed -i -e '1i "use strict"\n' "$f";fi;done
@naholyr
naholyr / fixtures.js
Last active December 28, 2015 21:08
Mongoose fixtures proposal
{
// 1 document in this collection
"Collection": [
{"fieldName": "value", "dbRefField": {"$ref": "OtherCollection", "$idx": 0}}
// dbRefField == OtherCollection[0] once made available
],
// 2 documents in this collection
"OtherCollection": [
// I care about context
foo = obj.bar.bind(obj).partial(1, 2)
foo = obj.bar.bind(obj, 1, 2)
// I don't care about context
foo = bar.partial(1, 2)
foo = bar.bind(null, 1, 2)
// In one case, bind() wins obviously
// In the other case, partial() wins hardly
var DEBUG = false;
var DEFAULT_NB_WORKERS = require('os').cpus().length;
var fork = require('child_process').fork;
var fs = require('fs');
var path = require('path');
if (process.argv.length == 2) console.error('Arguments: <file> [<nb-workers>]'), process.exit(1);
var file = path.resolve(process.argv[2]);
@naholyr
naholyr / 1-useless-stack.js
Last active June 8, 2021 08:29
Make stack traces useful again
var fs = require('fs');
// Here is a simple function that reads a file and makes something with its content
function readLog1 (cb) {
// Notice how I even gave a name to my callback, this is useful for stack traces, everyone says…
fs.readFile('file-not-found.log', function onRead1 (err, content) {
// Usual error handling
if (err) return cb(err);
@naholyr
naholyr / A_Sample.md
Last active December 14, 2015 11:48
A dumb event emitter service for Angular, primary usage is to allow transparent communication between controllers → http://jsfiddle.net/NuCjC/1/

Sample usage on JSFiddle

Disclaimer

This may be a dumb solution, you'd better use $rootScope.$on(event, handler) and $rootScope.$emit(event, args…) and not use a third-party service.

My solution will bring you:

  • support for off() if you really want to clean up your event handlers
  • maybe a conceptually better solution than using scopes, as with a service you make yourself independant from your view