Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
manjeettahkur / modules.md
Created October 20, 2015 05:43 — forked from mattdesl/modules.md
my favourite modules.
/* Object.create() vs new Speed Test
* @author Eric Hamilton
*
* Result: For a few thousand objects, it
* doesn't matter which way you go.
* For hundreds of thousands of objects,
* you might want to use new instead of
* Object.create().
*
* Tested on node v0.2.0
@manjeettahkur
manjeettahkur / test.js
Created October 21, 2015 05:27 — forked from juanpaco/test.js
Sparse indexes in Mongo with Mongoose
// Assumes you've already somehow configured your db access. I left that part out
var mongoose = require("mongoose")
var es = new mongoose.Schema({
'firstname': { type: String, required: true },
'lastname': { type: String, required: true },
'email': { type: String, required: true, index: { unique: true, sparse: true }, validate: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/ },
'departmentId': { type: mongoose.Schema.ObjectId, required: true },
'enddate': String,
var mongoose = require('mongoose');
mongoose.connect('localhost', 'testing_invalidate');
var schema = new mongoose.Schema({
name: { type: String, required: true },
pass: { type: String, required: true },
});
schema.methods.setPassword = function setPassword (pwd, confirm) {
if (pwd === confirm) {
@manjeettahkur
manjeettahkur / customer.js
Created October 22, 2015 12:01 — forked from geek0x23/customer.js
A mongoose model that takes advantage of the handy invalidate method to run multiple validations
var mongoose = require('./db-connect'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId,
uuid = require('node-uuid'),
Validator = require('validator').Validator,
val = new Validator(),
bcrypt = require('bcrypt');
Validator.prototype.error = function(msg) { return false; };
@manjeettahkur
manjeettahkur / node_debian_init.sh
Created October 30, 2015 05:11 — forked from peterhost/node_debian_init.sh
Daemon init script for node.js based app/server (DEBIAN/UBUNTU)
#!/bin/sh
# ------------------------------------------------------------------------------
# SOME INFOS : fairly standard (debian) init script.
# Note that node doesn't create a PID file (hence --make-pidfile)
# has to be run in the background (hence --background)
# and NOT as root (hence --chuid)
#
# MORE INFOS : INIT SCRIPT http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit
# INIT-INFO RULES http://wiki.debian.org/LSBInitScripts
# INSTALL/REMOVE http://www.debian-administration.org/articles/28
@manjeettahkur
manjeettahkur / why.js
Created November 21, 2015 10:46 — forked from cowboy/why.js
JavaScript: why do we write async code the way we do?
// This is an UBER simplified example, but I hope you get the idea...
function thisIsHowWeWriteSyncCode(arg) {
var foo = doSomething(arg);
var bar = doSomethingElse(foo);
var baz = doSomethingWith("test", bar, 123);
return doLastThing(baz);
}
function soThisSeemsSensibleForAsyncCode(arg) {
@manjeettahkur
manjeettahkur / shipitfile.js
Created December 29, 2015 11:35 — forked from jbraithwaite/shipitfile.js
Shipit deploy with Slack integration
var pack = require('./package.json');
var request = require('request');
var name = pack.name;
module.exports = function (shipit) {
require('shipit-deploy')(shipit);
var deployTo = '/var/www/FOLDER_TO_DEPLOY_TO';
var deployToCurrent = deployTo + '/current';
var slackWebhookURL = 'SLACK_HOOK_URL';
package main
import (
"io/ioutil"
"log"
"strings"
"net/http"
"encoding/json"
"fmt"
"time"
@manjeettahkur
manjeettahkur / toWeirdCase.js
Created May 31, 2016 13:00 — forked from jluismm2311/toWeirdCase.js
Write a function toWeirdCase (weirdcase in Ruby) that accepts a string, and returns the same string with all even indexed characters in each word upper cased, and all odd indexed characters in each word lower cased. The indexing just explained is zero based, so the zero-ith index is even, therefore that character should be upper cased. The passe…
function toWeirdCase(string){
let newString = string.split(' ');
for(let i = 0; i < newString.length; i++){
newString[i] = newString[i].weird();
}
return newString.join(' ');
}
String.prototype.weird = function( ){
var newString = '';