1.17.0 • Published 7 years ago

lowscore v1.17.0

Weekly downloads
1,273
License
MIT
Repository
github
Last release
7 years ago

Lowscore npm version npm

A very lightweight underscore, for browser apps that like to watch their weight.

Note, this is a very incomplete implementation of underscore, partly because many of underscore's functions can be found on Array and Object now, and also partly because I'm lazy. Pull requests for missing functions will be accepted gladly.

API

You can either import all of lowscore, or just the parts you need.

var _ = require('lowscore');
_.groupBy(...);
_.indexBy(...);

var groupBy = require('lowscore/groupBy');
var indexBy = require('lowscore/indexBy');
...

find (87 bytes)

ModuleSize (minified)
lowscore/find87
lodash.find12681
lodash/fp/find61376
underscore (all)16133
var find = require('lowscore/find');
var found = find(list, predicate);

Looks through each value in the list, returning the first one that passes a truth test (predicate), or undefined if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.

var even = find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
=> 2

findIndex (97 bytes)

ModuleSize (minified)
lowscore/findIndex97
lodash.findindex12509
lodash/fp/findIndex60952
underscore (all)16133
var findIndex = require('lowscore/findIndex');
var foundIndex = findIndex(list, predicate);

Returns the first index where the predicate truth test passes; otherwise returns -1. Predicate is passed the item and item index.

findIndex([4, 6, 8, 12], isPrime);
=> -1 // not found
findIndex([4, 6, 7, 12], isPrime);
=> 2

sortBy (327 bytes)

ModuleSize (minified)
lowscore/sortBy327
lodash.sortby13990
lodash/fp/sortBy63749
underscore (all)16133
var sortBy = require('lowscore/sortBy');
var sorted = sortBy(list, iteratee);

Returns a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee. iteratee may also be the string name of the property to sort by (eg. length).

sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
=> [5, 4, 6, 3, 1, 2]

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
sortBy(stooges, 'name');
=> [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}];

groupBy (165 bytes)

ModuleSize (minified)
lowscore/groupBy165
lodash.groupby12392
lodash/fp/groupBy62274
underscore (all)16133
var groupBy = require('lowscore/groupBy');
var groups = groupBy(list, iteratee);

Splits a collection into sets, grouped by the result of running each value through iteratee. If iteratee is a string instead of a function, groups by the property named by iteratee on each of the values.

groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
=> {1: [1.3], 2: [2.1, 2.4]}

groupBy(['one', 'two', 'three'], 'length');
=> {3: ["one", "two"], 5: ["three"]}

indexBy (144 bytes)

ModuleSize (minified)
lowscore/indexBy144
lodash.indexby10307
lodash/fp/indexBy62265
underscore (all)16133
var indexBy = require('lowscore/indexBy');
var index = indexBy(list, iteratee);

Given a list, and an iteratee function that returns a key for each element in the list (or a property name), returns an object with an index of each item. Just like groupBy, but for when you know your keys are unique.

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
indexBy(stooges, 'age');
=> {
  "40": {name: 'moe', age: 40},
  "50": {name: 'larry', age: 50},
  "60": {name: 'curly', age: 60}
}

pick (272 bytes)

ModuleSize (minified)
lowscore/pick272
lodash.pick2037
lodash/fp/pick61663
underscore (all)16133
var pick = require('lowscore/pick');
pick(object, key1, key2, ...);
pick(object, [key1, key2, ...]);
pick(object, function (value, key, object) { return key == key1; });

Return a copy of the object, filtered to only have values for the whitelisted keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick.

pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
=> {name: 'moe', age: 50}
pick({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
  return !isNaN(Number(value));
});
=> {age: 50}

omit (317 bytes)

ModuleSize (minified)
lowscore/omit317
lodash.omit6765
lodash/fp/omit62467
underscore (all)16133
var omit = require('lowscore/omit');
omit(object, key1, key2, ...);
omit(object, [key1, key2, ...]);
omit(object, function (value, key, object) { return key == key1; });

Return a copy of the object, filtered to omit the blacklisted keys (or array of keys). Alternatively accepts a predicate indicating which keys to omit.

omit({name: 'moe', age: 50, userid: 'moe1'}, 'userid');
=> {name: 'moe', age: 50}
omit({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
  return !isNaN(Number(value));
});
=> {name: 'moe', userid: 'moe1'}

mapObject (121 bytes)

ModuleSize (minified)
lowscore/mapObject121
lodash.mapvalues11991
lodash/fp/mapValues61328
underscore (all)16133
var mapObject = require('lowscore/mapObject');
mapObject(object, iteratee);

Like map, but for objects. Transform the value of each property in turn.

mapObject({start: 5, end: 12}, function(val, key, object) {
  return val + 5;
});
=> {start: 10, end: 17}

object (158 bytes)

ModuleSize (minified)
lowscore/object158
lodash.frompairs75
lodash/fp/fromPairs60787
underscore (all)16133
var object = require('lowscore/object');
object(list, [values]);

Converts arrays into objects. Pass either a single list of [key, value] pairs, or a list of keys, and a list of values. If duplicate keys exist, the last value wins.

_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
=> {moe: 30, larry: 40, curly: 50}

_.object([['moe', 30], ['larry', 40], ['curly', 50]]);
=> {moe: 30, larry: 40, curly: 50}

flatten (161 bytes)

ModuleSize (minified)
lowscore/flatten161
lodash.flatten1203
lodash/fp/flatten60775
underscore (all)16133
var flatten = require('lowscore/flatten');
var r = flatten(array, [shallow]);

Flattens a nested array (the nesting can be to any depth). If you pass shallow, the array will only be flattened a single level.

flatten([1, [2], [3, [[4]]]]);
=> [1, 2, 3, 4];

flatten([1, [2], [3, [[4]]]], true);
=> [1, 2, 3, [[4]]];

max (209 bytes)

ModuleSize (minified)
lowscore/max209
lodash.max373
lodash/fp/max61244
underscore (all)16133
var max = require('lowscore/max');
var smallestItem = max(items, [iteratee]);

Returns the maximum value in list. If an iteratee function is provided, it will be used on each value to generate the criterion by which the value is ranked. -Infinity is returned if list is empty, so an isEmpty guard may be required. Non-numerical values in list will be ignored.

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.max(stooges, function(stooge){ return stooge.age; });
=> {name: 'curly', age: 60};

min (206 bytes)

ModuleSize (minified)
lowscore/min206
lodash.min373
lodash/fp/min61244
underscore (all)16133
var min = require('lowscore/min');
var smallestItem = min(items, [iteratee]);

Returns the minimum value in list. If an iteratee function is provided, it will be used on each value to generate the criterion by which the value is ranked. Infinity is returned if list is empty, so an isEmpty guard may be required. Non-numerical values in list will be ignored.

var numbers = [10, 5, 100, 2, 1000];
_.min(numbers);
=> 2

compact (67 bytes)

ModuleSize (minified)
lowscore/compact67
lodash.compact78
lodash/fp/compact60912
underscore (all)16133
var compact = require('lowscore/compact');
var a = compact(array);

Returns a copy of the array with all falsy values removed. In JavaScript, false, null, 0, "", undefined and NaN are all falsy.

compact([0, 1, false, 2, '', 3]);
=> [1, 2, 3]

without (135 bytes)

ModuleSize (minified)
lowscore/without135
lodash.without4888
lodash/fp/without61951
underscore (all)16133
var without = require('lowscore/without');
var a = without(array, *values);

Returns a copy of the array with all instances of the values removed.

without([1, 2, 1, 0, 3, 1, 4], 0, 1);
=> [2, 3, 4]

range (224 bytes)

ModuleSize (minified)
lowscore/range224
lodash.range1505
lodash/fp/range61527
underscore (all)16133
var range = require('lowscore/range');
var r = range([start], stop, [step]);

A function to create flexibly-numbered lists of integers, handy for each and map loops. start, if omitted, defaults to 0; step defaults to 1. Returns a list of integers from start (inclusive) to stop (exclusive), incremented (or decremented) by step, exclusive. Note that ranges that stop before they start are considered to be zero-length instead of negative — if you'd like a negative range, use a negative step.

range(10);
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1, 11);
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(0, 30, 5);
=> [0, 5, 10, 15, 20, 25]
range(0, -10, -1);
=> [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
range(0);
=> []

repeat (115 bytes)

repeat(3, 'stuff')
=> ['stuff', 'stuff', 'stuff']

extend (142 bytes)

ModuleSize (minified)
lowscore/extend142
lodash.assign2378
lodash/fp/assign61936
underscore (all)16133
var extend = require('lowscore/extend');
var destination = extend(destination, *sources);

Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.

extend({name: 'moe'}, {age: 50});
=> {name: 'moe', age: 50}

times (77 bytes)

ModuleSize (minified)
lowscore/times77
lodash.times1013
lodash/fp/times61076
underscore (all)16133
var times = require('lowscore/times');
var t = times(n, iteratee);

Invokes the given iteratee function n times. Each invocation of iteratee is called with an index argument. Produces an array of the returned values.

times(3, function(n){ return "item: " + n; });
=> ['item: 0', 'item: 1', 'item: 2']

values (80 bytes)

ModuleSize (minified)
lowscore/values80
lodash.values1391
lodash/fp/values61053
underscore (all)16133
var values = require('lowscore/values');
var t = values(n, iteratee);

Return all of the values of the object's own properties.

values({one: 1, two: 2, three: 3});
=> [1, 2, 3]

uniq (199 bytes)

ModuleSize (minified)
lowscore/uniq199
lodash.uniq4357
lodash/fp/uniq61855
underscore (all)16133
var uniq = require('lowscore/uniq');
var t = uniq(array, [iteratee]);

Remove duplicate entries from the array. If iteratee is given then the entries uniqueness is based on the result returned by the iteratee. If iteratee is a string instead of a function, uniqueness is based on the property named by iteratee on each of the values.

uniq([1, 2, 2, 3, 1, 2]);
=> [1, 2, 3]

zip (230 bytes)

ModuleSize (minified)
lowscore/zip230
lodash.zip1272
lodash/fp/zip61514
underscore (all)16133
var zip = require('lowscore/zip');
var zipped = zip(*arrays);

Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. Use with apply to pass in an array of arrays. If you're working with a matrix of nested arrays, this can be used to transpose the matrix.

zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
1.17.0

7 years ago

1.16.0

7 years ago

1.15.0

7 years ago

1.14.0

7 years ago

1.13.0

7 years ago

1.12.1

8 years ago

1.12.0

8 years ago

1.11.0

8 years ago

1.10.0

8 years ago

1.9.0

8 years ago

1.8.1

8 years ago

1.8.0

8 years ago

1.7.0

8 years ago

1.6.0

8 years ago

1.5.0

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago