Skip to content

Instantly share code, notes, and snippets.

@tzechienchu
tzechienchu / singleton.js
Created August 24, 2016 07:34
Javascript Singleton
var timeZoneService = (function () {
var instance;
function init() {
var prop
var method = function() {
}
return {
prop:prop,
method:method
@tzechienchu
tzechienchu / ec2.js
Created August 26, 2016 09:58
AWS Node.js SDK; EC2 instance creation and termination example
var aws = require('aws-sdk');
aws.config.update({
accessKeyId: 'YOUR_ACCESS_KEY',
secretAccessKey: 'YOUR_SECRET_KEY',
region: 'us-west-2'
});
var ec2 = new aws.EC2();
@tzechienchu
tzechienchu / stringify.js
Created September 1, 2016 06:20 — forked from cowboy/stringify.js
JavaScript: like JSON.stringify but handles functions, good for creating arbitrary .js objects?
var stringify = function(obj, prop) {
var placeholder = '____PLACEHOLDER____';
var fns = [];
var json = JSON.stringify(obj, function(key, value) {
if (typeof value === 'function') {
fns.push(value);
return placeholder;
}
return value;
}, 2);
@tzechienchu
tzechienchu / Javascript ISO country code to country name conversion
Created September 23, 2016 04:45 — forked from maephisto/Javascript ISO country code to country name conversion
ISO 3166-1 alpha-2 country code to country name conversion with a simple Javascript implementation, an array and a function.
var isoCountries = {
'AF' : 'Afghanistan',
'AX' : 'Aland Islands',
'AL' : 'Albania',
'DZ' : 'Algeria',
'AS' : 'American Samoa',
'AD' : 'Andorra',
'AO' : 'Angola',
'AI' : 'Anguilla',
'AQ' : 'Antarctica',
@tzechienchu
tzechienchu / accessIP.js
Last active October 1, 2016 03:23
Loopback Access IP
//Following Code in boot
module.exports = function (app) {
//
//Loopback 3.0 don't support app.remotes().before
//
app.remotes().before('*.*', function(ctx,next1,next2) {
//console.log(ctx.req.headers)
//console.log('IP-1')
var context = LoopBackContext.getCurrentContext();
@tzechienchu
tzechienchu / disableAllMethods.js
Created October 13, 2016 09:49 — forked from ratik/disableAllMethods.js
LoopbackJs disable all remote methods and enable only selected
/**
* Based on @ericprieto code https://github.com/strongloop/loopback/issues/651#issuecomment-140879983
* place this file into common/mixins/disableAllMethods.js
*
**/
module.exports = function(Model, options) {
if(Model && Model.sharedClass) {
var methodsToExpose = options.expose || [];
@tzechienchu
tzechienchu / javascriptSnippet.js
Last active October 27, 2016 01:48
VSC Javascript snippet
{
"coRoutine": {
"prefix": "coBody",
"body": [
"co(function*() {",
" cb(null,'ToDo');",
"})",
".catch(function(err){",
" cb(null,{err:err});",
"})"
@tzechienchu
tzechienchu / csvToArray.js
Created October 20, 2016 09:07
Javascript CSV to Array
var lines = fs.readFileSync(path.join(__dirname, '../actionData/'+fileName)).toString().split('\r\n');
var rawData = [];
lines.map(function(line) {
rawData.push(line.split(','))
})
@tzechienchu
tzechienchu / pickOne.js
Last active November 10, 2016 04:50
PickOne
var _ = require('lodash');
var fs = require("fs");
var path = require('path');
var DataGenerator = (function () {
var instance;
var lines = fs.readFileSync(path.join(__dirname, '../actionData/alice.txt')).toString().split('\n');
lines = lines.filter(function(line) {
return (line.length > 0)
})
function init() {
var redisServer = require('redis');
var RedisService = (function () {
var instance;
function init() {
//6381 for Develop and Test
//6379 for Production
var port = 6379;
var host = "localhost";
var redis = redisServer.createClient(port,host);