workflow:
$ rails g model NameOfModel
invoke active_record
create db/migrate/YYYYMMDDHHMMSS_create_name_of_models.rb| var param1 = 'value1'; | |
| function method(){ | |
| console.log(this.param1); | |
| } | |
| var obj = { | |
| param1: 'value2', | |
| method: function(){ | |
| console.log(this.param1); | |
| } |
| var obj ={ | |
| param1: 'value1', | |
| param2: 'value2', | |
| method: function(){ | |
| console.log(this.param1, this.param2); | |
| console.log(obj.param1, obj.param2); | |
| } | |
| } |
| /* | |
| * Let server be running at http://localhost:3000 | |
| */ | |
| // server.js | |
| var index = require('./routes/index'); | |
| var users = require('./routes/users'); | |
| app.use('/', index); | |
| app.use('/users', users);//this will prefix all user routes with users. Namespace effect. |
| /** | |
| * Regex to find occurences in the lines in any order from different pools (OR and AND) | |
| **/ | |
| var pattern = /(?=(?=.*\beducation\b)|(?=.*\bhealth\b))(?=(?=.*\bvideo\b)|(?=.*\baudio\b))/ | |
| pattern.test('this education media support both audio video streams') // true | |
| /** | |
| *To add education and educational add .* with education in regex | |
| * */ |
| // to clone one collection to other | |
| db.demo1.find().forEach( function(x){db.demo2.insert(x)} ); | |
| // to remove dupliactes based on a key | |
| db.demo1.find({}, {field:1}).sort({_id:1}).forEach(function(doc){ | |
| db.demo1.remove({_id:{$gt:doc._id}, field:doc.field}); | |
| }) | |
| //to find documents added today | |
| var d = new Date() |
| var startups = [{'score':1},{'score':2},{'score':2},{'score':3},{'score':4}]; | |
| startups.sort(function(a, b){ | |
| return (b.score - a.score); | |
| }); | |
| for(var i =0, rank= 1, j=1; i <startups.length; i++){ | |
| startups[i].rank = rank; | |
| if(startups[i+1] && startups[i].score != startups[i+1].score){ | |
| rank = rank+j; | |
| j++; | |
| } |
| var reURLInformation = new RegExp([ | |
| '^(https?:)//', // protocol | |
| '(([^:/?#]*)(?::([0-9]+))?)', // host (hostname and port) | |
| '(/[^?#]*)', // pathname | |
| '(\\?[^#]*|)', // search | |
| '(#.*|)$' // hash | |
| ].join('')); | |
| var href ="http://www.example.com"; | |
| if(href[href.length -1]!='/') | |
| href = href.concat('/'); |
| // Returns the value at a given percentile in a sorted numeric array. | |
| // "Linear interpolation between closest ranks" method | |
| function percentile(arr, p) { | |
| if (arr.length === 0) return 0; | |
| if (typeof p !== 'number') throw new TypeError('p must be a number'); | |
| if (p <= 0) return arr[0]; | |
| if (p >= 1) return arr[arr.length - 1]; | |
| var index = arr.length * p, | |
| lower = Math.floor(index), |
| var matchString = "/ol?link=http://www.facebook.com/flipkart-practo/kevin-kasrt"; | |
| var res = matchString.match(/facebook\.com\/(.+)\/+/); | |
| console.log(res[1]); |