Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View saltukalakus's full-sized avatar
💭

Saltuk Alakus saltukalakus

💭
View GitHub Profile
@saltukalakus
saltukalakus / replace_json_property.js
Created September 10, 2015 11:23
Replace a JSON property dynamically in a function call
var test = function(key, value) {
a = {foo:{bar:{baz:"placeholder"}}};
a.foo.bar.baz = {}; //creating a new object as `baz` value then assign
a.foo.bar.baz[key] = value;
console.log(JSON.stringify(a));
};
test("Jon", "Dough");
@saltukalakus
saltukalakus / typeof_vs_instanceof.js
Last active September 10, 2015 12:26
typeof vs instanceof
/*
typeof is a construct that "returns" the primitive type of whatever you pass it.
instanceof tests to see if the right operand appears anywhere in the prototype chain of the left.
*/
var test = require('tape');
test('typeof vs instanceof tests', function (t) {
t.plan(13);
@saltukalakus
saltukalakus / with_function.js
Last active September 10, 2015 19:54
Object generation patterns.
function Apple (type) {
this.type = type;
this.color = "red";
}
Apple.prototype.getInfo = function() {
return this.color + ' ' + this.type + ' apple';
};
var apple = new Apple('macintosh');
@saltukalakus
saltukalakus / function_overload.js
Last active September 17, 2015 11:18
Function overloading
test = require("tape");
function addMethod(object, name, fn){
object._store = object._store || {};
object._store[name] = object._store[name] || {};
object._store[name][fn.length] = fn;
object[name] = function() {
if(this._store[name][arguments.length])
return this._store[name][arguments.length].apply(this, arguments);
};
@saltukalakus
saltukalakus / module_pattern.js
Last active September 29, 2015 13:36
Structural patterns
test = require('tape');
// Basic module with closure
var Module = (function () {
var my = {},
privateVariable = 1;
function setPrivateMethod(val) {
privateVariable = val;
@saltukalakus
saltukalakus / elk_bulk.js
Last active October 6, 2015 17:08
Elastic search tests for API:1.7
var elasticsearch = require('elasticsearch');
var client = elasticsearch.Client({
hosts: [
'localhost:9200'
]
});
var tk = {
_bulkArray: function(idx, type, data) {
@saltukalakus
saltukalakus / string_to_array
Created August 7, 2016 10:33
js, convert array to string with space
var arr = [1,2,3,4];
var res= arr.join(' ');
@saltukalakus
saltukalakus / npm_view_old
Created August 7, 2016 15:14
npm view list old versions of a module
npm view module_name versions
@saltukalakus
saltukalakus / window.onload
Created August 9, 2016 14:10
js window on load page refresh trigger
window.onload = function() {
doSomethingElse();
};
@saltukalakus
saltukalakus / call.js
Created August 14, 2016 21:02
js call
function personContainer() {
var person = {
name: "James Smith",
hello: function() {
console.log(this.name + " says hello " + arguments[1]);
}
}
person.hello.apply(person, arguments);
}
personContainer("world", "mars"); // output: "James Smith says hello mars", note: arguments[0] = "world" , arguments[1] = "mars"