Skip to content

Instantly share code, notes, and snippets.

View btargac's full-sized avatar
🏠
Working from home

Burak Targaç btargac

🏠
Working from home
View GitHub Profile
@btargac
btargac / uniqBy.js
Created February 20, 2020 13:46
When you want to get rid of duplications in an array
/**
* Returns an array of unique Items by applying the supplied function to each of the original list
* @param {Object[]} list - The array to consider.
* @param {Function} fn - A function used to produce a value to use during comparisons.
* @returns {Object[]} The list of unique items.
* @example
*
* uniqBy([{a: 1, c:4}, {a: 2, c:5}, {a: 2, c:5}], item => item['a']); //=> [{a: 1, c:4}, {a: 2, c:5}]
*/
const uniqBy = (list, fn) =>
@btargac
btargac / groupWith.js
Last active February 20, 2020 12:14
Native javascript grouping utility functions
import groupBy from './groupBy';
/**
* Takes an array and returns an array of objects that each objects list elements are all satisfied according to the provided function or a string key.
* @param {Object[]} list The array to consider.
* @param {(Function|String)} criteria function or a string key
* @returns {Object[]} Array of subarray's of objects
* @example
*
* groupWith([{a: 4, b: 1}, {a: 4, b: 3}, {a: 5, b: 1}, {a: 5, b: 3}], 'a');
@btargac
btargac / groupBy.js
Last active February 20, 2020 12:13
Native javascript grouping utility functions
/**
* Returns a grouped Object from a given array of Objects where a certain key of these objects are expected to be the same
* what ramda js states as Splits a list into sub-lists stored in an object, based on the result of calling a String-returning
* function on each element, and grouping the results according to values returned.
* @param {Object[]} list - The array to consider.
* @param {(Function|String)} criteria - function or a string key
* @returns {(Object.<string, Object[]>| void)} an object where keys are the values of each object's criteria key.
* @example
*
* groupBy([{a: 4, b: 1}, {a: 4, b: 3}, {a: 5, b: 1}, {a: 5, b: 3}], 'a');