Skip to content

Instantly share code, notes, and snippets.

@nevill
nevill / filterDuplicated.js
Created May 19, 2014 03:48
Simpe test on how to strip out duplications from array in Javascript
var _ = require('underscore');
var ObjectID = require('mongodb').ObjectID;
var duplicatedIds = [];
var lengthOfDuplicatedIds = 10;
for (var i = 0; i < lengthOfDuplicatedIds; i++) {
duplicatedIds.push(new ObjectID());
}
var ArrToTest = [];
@nevill
nevill / app.js
Last active August 29, 2015 13:56
An example of usage on swagger-jack, to validate against uploaded files
// To test with it, run with `curl`
// curl -F "photo=@/path/to/your/photo" localhost:3000/api/uploads
var express = require('express');
var swagger = require('swagger-jack');
var app = express();
app.use(express.logger('dev'))
.use(express.bodyParser())
.use(express.methodOverride())
.use(swagger.generator(app, {
@nevill
nevill / creation.js
Created December 11, 2013 05:54
A sample to use swagger-jack
module.exports = {
resourcePath: '/orgs',
apis: [{
path: '/orgs',
operations: [{
httpMethod: 'POST',
responseClass: 'void',
nickname: 'returnParams',
parameters: [{
dataType: 'Org',
@nevill
nevill / spread.js
Created October 16, 2013 06:47
Try to understand how to use `spread` in Q - a tool to compose asynchronous promises.
var Q = require('q');
var util = require('util');
function randomDelayedCall(func) {
var delay = Math.floor(Math.random() * (1200 - 100) + 100);
setTimeout(func, delay);
}
function findUser(id, cb) {
var err = null;
@nevill
nevill / astro.js
Created October 8, 2013 13:15
获得星座名称
// 输入 m: 月, d: 天
// 例如 getAstro(5, 1) 返回 `金牛`
function getAstro(m, d) {
return "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(m * 2 - (d < "102123444543".charAt(m - 1) - -19) * 2, 2);
}
#!/bin/env ruby
# -*- coding: utf-8 -*-
class MatrixCalc
def initialize(matrix)
@matrix = matrix
end
def input
@input ||= traverse
# to memorize the collatz sequence length for a specific number
memory = []
Number::collatz = ->
if @ % 2 == 0
@ / 2
else
3 * @ + 1
Number::collatzSequenceLength = ->
String::reverse = ->
size = @length + 1
result = ""
while size -= 1
result += @[size - 1]
result
String::reversable = ->
@reverse() == @toString()