Skip to content

Instantly share code, notes, and snippets.

@callumlocke
Created October 26, 2015 14:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callumlocke/bbfc524eaed6b3556dab to your computer and use it in GitHub Desktop.
Save callumlocke/bbfc524eaed6b3556dab to your computer and use it in GitHub Desktop.
Importing individual lodash-es functions
import chunk from 'lodash-es/array/chunk';
import zipObject from 'lodash-es/array/zipObject';

console.log(zipObject(chunk(['a', 'b', 'c', 'd'], 2)));
$ rollup -f=iife demo.js > output.js
(function () { 'use strict';
/** Used as references for various `Number` constants. */
var INFINITY = 1 / 0;
var MAX_INTEGER = 1e308;
/**
* Converts `value` to an integer.
*
* **Note:** This function is loosely based on [`ToInteger`](http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger).
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to convert.
* @returns {number} Returns the converted integer.
* @example
*
* _.toInteger('3.14');
* // => 3
*
* _.toInteger(NaN);
* // => 0
*
* _.toInteger(-Infinity)
* // => -1e308
*/
function toInteger(value) {
if (value === INFINITY || value === -INFINITY) {
return (value < 0 ? -1 : 1) * MAX_INTEGER;
}
value = +value;
var remainder = value % 1;
return value === value ? (remainder ? value - remainder : value) : 0;
}
/**
* The base implementation of `_.slice` without an iteratee call guard.
*
* @private
* @param {Array} array The array to slice.
* @param {number} [start=0] The start position.
* @param {number} [end=array.length] The end position.
* @returns {Array} Returns the slice of `array`.
*/
function baseSlice(array, start, end) {
var index = -1,
length = array.length;
start = start == null ? 0 : toInteger(start);
if (start < 0) {
start = -start > length ? 0 : (length + start);
}
end = (end === undefined || end > length) ? length : toInteger(end);
if (end < 0) {
end += length;
}
length = start > end ? 0 : ((end - start) >>> 0);
start >>>= 0;
var result = Array(length);
while (++index < length) {
result[index] = array[index + start];
}
return result;
}
var nativeCeil = Math.ceil;
var nativeMax = Math.max;
/**
* Creates an array of elements split into groups the length of `size`.
* If `collection` can't be split evenly, the final chunk will be the remaining
* elements.
*
* @static
* @memberOf _
* @category Array
* @param {Array} array The array to process.
* @param {number} [size=0] The length of each chunk.
* @returns {Array} Returns the new array containing chunks.
* @example
*
* _.chunk(['a', 'b', 'c', 'd'], 2);
* // => [['a', 'b'], ['c', 'd']]
*
* _.chunk(['a', 'b', 'c', 'd'], 3);
* // => [['a', 'b', 'c'], ['d']]
*/
function chunk(array, size) {
size = nativeMax(toInteger(size), 0);
var length = array ? array.length : 0;
if (!length || size < 1) {
return [];
}
var index = 0,
resIndex = -1,
result = Array(nativeCeil(length / size));
while (index < length) {
result[++resIndex] = baseSlice(array, index, (index += size));
}
return result;
}
/**
* Checks if `value` is classified as an `Array` object.
*
* @static
* @memberOf _
* @type Function
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
* @example
*
* _.isArray([1, 2, 3]);
* // => true
*
* _.isArray(document.body.children);
* // => false
*
* _.isArray('abc');
* // => false
*
* _.isArray(_.noop);
* // => false
*/
var isArray = Array.isArray;
/**
* The inverse of `_.pairs`; this method returns an object composed from arrays
* of property names and values. Provide either a single two dimensional array,
* e.g. `[[key1, value1], [key2, value2]]` or two arrays, one of property names
* and one of corresponding values.
*
* @static
* @memberOf _
* @category Array
* @param {Array} props The property names.
* @param {Array} [values=[]] The property values.
* @returns {Object} Returns the new object.
* @example
*
* _.zipObject([['fred', 30], ['barney', 40]]);
* // => { 'fred': 30, 'barney': 40 }
*
* _.zipObject(['fred', 'barney'], [30, 40]);
* // => { 'fred': 30, 'barney': 40 }
*/
function zipObject(props, values) {
var index = -1,
length = props ? props.length : 0,
result = {};
if (length && !values && !isArray(props[0])) {
values = [];
}
while (++index < length) {
var key = props[index];
if (values) {
result[key] = values[index];
} else if (key) {
result[key[0]] = key[1];
}
}
return result;
}
console.log(zipObject(chunk(['a', 'b', 'c', 'd'], 2)));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment