Skip to content

Instantly share code, notes, and snippets.

View ronen-e's full-sized avatar

Ronen Elster ronen-e

  • Tel Aviv, Israel
View GitHub Profile
@ronen-e
ronen-e / flatten.js
Last active September 16, 2016 07:42
Flatten array
/**
* Flattens native JS Array objects.
* Uses the native Array.isArray method, therefore array like objects (e.g Arguments object) need to be converted to Array
* instances for this function to work on them (e.g ...arguments)
* @param {Array} list
* @param {Array} newList - not required for the first call of function
* @return {Array} newList
*/
function flatten(list, newList = []) {
for (var i = 0, length = list.length; i < length; i++) {
@ronen-e
ronen-e / form-validator.js
Last active September 18, 2016 08:00
Sample jQuery form validator
(function ( $ ) {
var invalidations = [ 'invalidRequiredField', 'invalidInput' ];
invalidations.invalidRequiredField = function invalidRequiredField(params) {
return params.isRequiredField && !params.value;
};
invalidations.invalidInput = function invalidInput(params) {
var isRequiredField = params.isRequiredField,
@ronen-e
ronen-e / binary-to-decimal.js
Created October 1, 2016 21:26
Convert to binary from decimal and vice versa
function binToDec(n) {
var result = 0;
var s = n.toString();
var len = s.length - 1;
for (var i = len; i >= 0; i--) {
result += s[i] * Math.pow(2, len-i);
}
return result;
}
@ronen-e
ronen-e / solution.js
Last active August 16, 2018 13:29
Table render exercise with pure javascript
(function(window){
// sample data from server
var data1 = [ { "company_name":"Medline Industries, Inc.", "product":"Benzalkonium Chloride", "price":"481.63" }, { "company_name":"PD-Rx Pharmaceuticals, Inc.", "product":"Alprazolam", "price":"167.62", "fda_date_approved":"02/12/2015" }, { "company_name":"West-ward Pharmaceutical Corp.", "product":"Flumazenil", "fda_date_approved":"23/04/2015" }, { "company_name":"HyVee Inc", "product":"Aspirin", "price":"218.32", "fda_date_approved":"26/07/2015" }, { "company_name":"Aurobindo Pharma Limited", "product":"carisoprodol", "price":"375.58", "fda_date_approved":"28/11/2014" }, { "company_name":"Apotex Corp", "product":"Risperidone", "price":"213.49", "fda_date_approved":"06/11/2015" }, { "company_name":"Unit Dose Services", "product":"Lovastatin", "price":"169.14", "fda_date_approved":"14/09/2015" }, { "company_name":"Jubilant HollisterStier LLC", "product":"Dog Hair Canis spp.", "fda_date_approved":"31/12/2014" }, { "company_name":"AAA Pharmaceutical, Inc
@ronen-e
ronen-e / compose.js
Created October 9, 2016 16:14
compose functions
/**
* add the result of a function as an argument for the next function, e.g f(g(c))
* @param {...Function} functions to be composed
* @return {Function} the composed function
*/
function compose() {
var funcs = arguments;
return function composed(value) {
var i = funcs.length;
@ronen-e
ronen-e / search-modules.js
Created October 10, 2016 14:18
Search modules
var Modules = new Map();
function addModule(id, searchFn) {
if (Modules.has(id)) {
throw 'Error: module: ' + id + ' already exists.';
}
Modules.set(id, searchFn);
}
function searchFn(id, query) {
@ronen-e
ronen-e / generator.js
Created December 13, 2016 17:05
Fun with generators
function* gen(param) {
if (!Array.isArray(param))
yield param;
else
for (let item of param)
yield* gen(item);
}
function* genNumbers(iterable) {
var items = gen(iterable);
@ronen-e
ronen-e / connect.js
Created December 17, 2016 19:35
Redux connect implementation
function connect(mapStateToProps, mapDispatchToProps) {
return function connector(Component) {
class ContainerComponent extends React.Component {
componentDidMount() {
const { store } = this.context;
this.unsubscribe = store.subscribe(() => this.forceUpdate());
}
componentWillUnmount() {
this.unsubscribe();
@ronen-e
ronen-e / scope.js
Created April 5, 2017 05:15
Create new scope in javascript
function func() {
for (var i = 0; i < 10; i++) {
setTimeout(function() {
console.log('i:' + i);
}, 0);
}
}
// func();
function solution1() {
for (let i = 0; i < 10; i++) {
@ronen-e
ronen-e / bind.js
Created April 5, 2017 05:16
autobind implementation - bind all prototype methods to instance
function binder(instance) {
for (var prop in instance) {
if (typeof instance[prop] === 'function' && !instance.hasOwnProperty(prop)) {
instance[prop] = instance[prop].bind(instance);
}
}
}
function binder2(instance, prototype) {
Object.keys(prototype)