Skip to content

Instantly share code, notes, and snippets.

View asduser's full-sized avatar

Network Engineer asduser

View GitHub Profile
@asduser
asduser / closest-number.js
Last active July 24, 2016 16:54
Closest number in array
/*
* @param num {number} - required digit
* @param arr {array} - input set of digits
* @returns {number} - closest item
*/
function closest (num, arr) {
try {
if(isNaN(num)){
throw new Error("The 1st argument is not a number.");
@asduser
asduser / consumer.py
Created August 17, 2016 18:53
A simple code, which explains the process of entity status changing by using of private and public methods.
#!/usr/bin/env python
class Fruit:
def __init__(self):
self.__state__ = False
def isEaten(self):
return self.__state__
@asduser
asduser / callable_function_es5.js
Created August 25, 2016 19:20
A way to modify an internal function checking.
//
// ES5 IMPLEMENTATION
//
function applyFilter(binary){
return function(a,b){
return binary(a,b);
}
}
function add(a,b){ return a + b; }
function mult(a,b){ return a * b }
@asduser
asduser / url_parameters.js
Created August 28, 2016 21:20
Get all URL parameters.
function getAllUrlParams(url) {
// get query string from url (optional) or window
var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
// we'll store the parameters here
var obj = {};
// if query string exists
if (queryString) {
@asduser
asduser / nextPrevItem.js
Last active October 11, 2016 21:24
A special feature to find next\prev item in array only when item has value. Will be skipped all unnecessary items by their id's.
// Go to the next item.
function nextIndex(index, arr){
var item = null;
var minI = 0;
var i = index;
while (i < arr.length) {
i++;
if (i == arr.length) { i = minI; }
if (arr[i].id && arr[i].val) { item = arr[i]; break;}
}
@asduser
asduser / try-catch-json-parse(generic).ts
Last active January 12, 2017 20:38
try\catch for JSON.parse
const loadDataOrDefault = <T>(response: any, defInstance?: T) => {
try {
return JSON.parse(response);
} catch(e) {
return defInstance ? defInstance : null;
}
};
class Person {
constructor(public name: string = "Default", public age: number = 100) {}
@asduser
asduser / nodejs_snippets.js
Created January 12, 2017 22:20
Node.js snippets #1
function copySync(src, dest) {
if (!fs.existsSync(src)) return false;
var data = fs.readFileSync(src, 'utf-8');
fs.writeFileSync(dest, data);
}
function replaceInFile(src, replaceKey){
fs.readFile(src, 'utf8', function (err, data) {
if (err) return console.log(err);
console.log(data);
@asduser
asduser / async_await_chain.ts
Created February 7, 2017 22:08
Chaining promises with async\await in TypeScript.
let combine = (num: number, combiner: number = num) => {
return new Promise<any>((resolve, reject) => {
resolve(num + combiner);
});
};
let multiple = (num: number, multiplier: number = num) => {
return new Promise<any>((resolve, reject) => {
resolve(num * multiplier);
});
@asduser
asduser / promise-all-loop.ts
Last active February 8, 2017 20:50
Different operations with Promise via Typescript/Javascript.
let divide = (num: number, divider: number = 10) => {
return new Promise<any>((resolve, reject) => {
resolve(num / divider);
});
};
let collection: number[] = [30, 40, 50];
Promise.all(collection.map((n) => {
return divide(n);
})).then((res: number[]) => {
@asduser
asduser / next_previous_item.md
Created March 23, 2017 21:26
Typescript\Python implementation

The code below assumes that each item should has two attributes: unique id and value field. Due to boolean comparison operator we can get an appropriate element.

Next item algorithm.

  1. Check if current index in iteration is not equal to collection.length, otherwise - specify index to 0.
  2. Increase current index by 1.
  3. Check if current element has value. If not - do 2nd step, if yes - return it and break loop.

Previous item algorithm.