Skip to content

Instantly share code, notes, and snippets.

Avatar
😇
Living the JS life

Vladimir de Turckheim vdeturckheim

😇
Living the JS life
View GitHub Profile
@vdeturckheim
vdeturckheim / Array.prototype.isPresent.js
Last active Oct 28, 2015
Proper (according to me) "contains" method for arrays in javascript. This allows the use of callback on testing.
View Array.prototype.isPresent.js
/**
* Proper (according to me) "isPresent" method for arrays in javascript. This allows the use of callback on testing.
* An empty callback is like using '==' as test.
* @param item item to find
* @param cpf -optional- test callback function: function(a,b) where a is the element to find and b the element tested
* from the array.
* @returns {boolean} true if at least one element is present in the array equals to the one to find according to the
* compare function.
* @author Vladimir de Turckheim
*/
View leanhub-intro-package.json
{
"name": "intro",
"version": "0.1.0",
"description": "Short introduction to Lab and Hapi",
"main": "index.js",
"scripts": {
"test": "lab -cLv -t 100",
"start": "node ./index"
},
View leanhub-intro-index.js
'use strict';
// Hapi framework
const Hapi = require('hapi');
// our plugin in the file api.js
const ApiPlugin = require('./api');
// We create a server object
const server = new Hapi.Server();
// Server will listen on port 8080
server.connection({
View leanhub-intro-test.js
'use strict';
// Hapi framework
const Hapi = require('hapi');
// our plugin in the file api.js
const ApiPlugin = require('../api');
// assertion library
const Code = require('code');
// Lab test runner
const Lab = require('lab');
View susie-rethink-server.js
const Hapi = require('hapi');
const Path = require('path');
const server = new Hapi.Server();
server.connection({ port: 4000 });
server.register([require('inert'), require('susie')], err => {
if (err) {
View susie-rethink-post.js
{
config: {
validate: {
payload: {
message: Joi.string(),
username: Joi.string()
}
}
},
method: 'POST',
View susie-rethink-get.js
{
method: 'GET',
path: '/chat-feed',
handler: function (request, reply) {
Message
.changes()
.run()
.then((feed) => {
feed.each((err, doc) => {
View rethink-chat-rooms-client.js
var evtSource = new EventSource("/chat-feed");
evtSource.addEventListener("message", e => {
// e.data == the message that has been received
});
View nest-1-index.js
'use strict';
const Mongoose = require('mongoose');
const validateOptions = require('./lib').validateOptions;
const builConnectionString = require('./lib').builConnectionString;
module.exports.register = function (server, options, next) {
const validation = validateOptions(options);
View nest-1-lib-index.js
'use strict';
const Joi = require('joi'); // object schema validation tool
const optionsSchema = Joi.object().keys({
// a string representing a hostname or an ip
host: Joi.alternatives().try(Joi.string().hostname(), Joi.string().ip()).default('127.0.0.1'),
// a number in the allowed TCP port range, default to default mongodb port
port: Joi.number().min(1).max(65535).default(27017),
// If no database name, we create an random one that look like 'database_abcd' with a,b,c,d random numbers
database: Joi.string().min(1).replace(' ', '_').default(`database_${Math.floor(Math.random() * 10000)}`),