Agile.js
Like Underscore, but with zero callbacks and really more fun, v0.0.2
Get Started
(1) You can install agile.js using 3 different methods:
- clone & build this repository
- via Bower: by running
$ bower install agile
from your terminal - via npm: by running
$ npm install agile
from your terminal - soon, cdnjs will be one of the options
(2) Add to your project:
For the Browser:
Include agile.js
(or agile.min.js
) in your index.html
.
For Node Apps:
var _ = ;
(3) Start Playing with agile.js:
var orders = id: 21 customer: id: 2 name: 'John P.' product: price: 2112 id: 22 customer: id: 1 name: 'Cati P.' product: price: 8921 id: 23 customer: id: 1 name: 'Cati P.' product: price: 4900 id: 24 customer: id: 3 name: 'Poul S.' product: price: 1022 id: 25 customer: id: 4 name: 'Erik L.' product: price: 1131 id: 26 customer: id: 4 name: 'Erik L.' product: price: 9099 id: 27 customer: id: 2 name: 'Cati P.' product: price: 8899 ; // ArrayWrapper // [21.12, 89.21, 49, 10.22, 11.31, 90.99, 88.99] // 360.84 // 361 // 371 value; // get the value;
Collection
after
get a collection and specified count, and returns all of the items in the collection after the specified count.
Usage: _.after(array, count)
var users = name: 'foo' name: 'bar' name: 'baz' name: 'zap' ; _;// → [ { name: 'baz' }, { name: 'zap' } ]
afterWhere
get a collection and expression
/callback
, and returns all of the items in the collection after the first that return true, include it.
Usage: _.afterWhere(array, expression/callback)
var orders = id: 1 date: 'Tue Jul 15 2014' id: 2 date: 'Tue Jul 16 2014' id: 3 date: 'Tue Jul 17 2014' id: 4 date: 'Tue Jul 18 2014' id: 5 date: 'Tue Jul 19 2014' ; _;// → [ orders[2], orders[3], orders[4] ] _;// → [ orders[3], orders[4] ] _;// → [ orders[3], orders[4] ]
add
add
is similar to Array.push
, but can get a multiple arguments, and return the array instead of the value.
Usage: _.add(array, args)
_; // → [1, 2, 3, 4, 5, 6]
before
get a collection and specified count, and returns all of the items in the collection before the specified count.
Usage: _.before(array, count)
var users = name: 'foo' name: 'bar' name: 'baz' name: 'zap' ; _;// → [ { name: 'foo' }, { name: 'bar' } ]
beforeWhere
get a collection and expression
/callback
, and returns all of the items in the collection before the first that return true, including it.
Usage: _.beforeWhere(array, expression/callback)
var orders = id: 1 date: 'Tue Jul 15 2014' id: 2 date: 'Tue Jul 16 2014' id: 3 date: 'Tue Jul 17 2014' id: 4 date: 'Tue Jul 18 2014' id: 5 date: 'Tue Jul 19 2014' ; _;// → [ orders[0], orders[1], orders[2] ] _;// → [ orders[0], orders[1] ] _;// → [ orders[0], orders[1] ]
contains
Checks if given expression(or value) is present in one or more object in the array.
Usage: _.contains(array, expression/callback/value)
Aliases: _.some
var nums = 1234;_; // → true var users = user: id: 2 name: 'foo' user: id: 4 name: 'bar' user: id: 6 name: 'baz' ;_;// → true_;// → false
countBy
Create an object composed of keys generated from the result of the running expression, each key is the count of objects in each group.
Usage: _.countBy(array, expression/callback)
var players = name: 'Gene' team: name: 'alpha' name: 'George' team: name: 'beta' name: 'Steve' team: name: 'gamma' name: 'Paula' team: name: 'beta' name: 'Scruath' team: name: 'gamma' ;_;// → { alpha: 1, beta: 2, gamma:2 }
defaults
defaults allows to specify a default fallback value for properties that resolve to undefined.
Usage: _.defaults(array, object)
var orders = id:1 destination: zip: 21908 name: 'Ariel M' id:2 name: 'John F' id:3 destination: zip: 45841 id:4 destination: zip: 78612 name: 'Danno L' ;var fallback = name: 'Customer name not available' destination: zip: 'Pickup' ;_;// Results:// [{ id: 1, destination: { zip: 21908 }, name: 'Ariel M' },// { id: 2, destination: { zip: 'Pickup' }, name: 'John F' },// { id: 3, destination: { zip: 45841 }, name: 'Customer name not available' },// { id: 4, destination: { zip: 78612 }, name: 'Danno L' }]
every
Checks if given expression/callback is present in all members in the array.
Usage: _.every(array, expression/callback)
var nums = 1234;_; // → false var users = id: 2 name: 'bob' } id: 4 name: 'bar' } id: 6 name: 'baz' };_;// → true_;// → false
filter
filter by expression/callback
return all elements that return true
, avoid the rest.
Usage: _.filter(array, expression/callback)
Aliases: _.pick
var users = id: 1 user: name: 'foo' isAdmin: true id: 2 user: name: 'bar' isAdmin: false id: 3 user: name: 'baz' isAdmin: true ;_;// → [ users[0], users[2] ]
find
Iterate over the given array and return the first member that the expression
returns truthy for.
Usage: _.find(array, expression/callback)
var orders = id: 21 product: price: 2112 auth: '3s!sa0' id: 22 product: price: 8921 auth: '@3dRg1' id: 23 product: price: 4900 auth: 'a44Fy+' id: 24 product: price: 1022 auth: 'WS4%a0' id: 25 product: price: 1131 auth: '7Y#d_1' ;_; // → { id: 22, product: { price: 89.21 }, auth: ['@3dRg1'] } _;// → { id: 25, product: { price: 11.31 }, auth: ['7Y#d_1'] } _;// → { id: 22, product: { price: 89.21 }, auth: ['@3dRg1'] }
findLast
Iterate over the given array and return the last member that the expression
returns truthy for.
Usage: _.findLast(array, expression/callback)
var orders = id: 21 product: price: 2112 auth: '3s!sa0' id: 22 product: price: 8921 auth: '@3dRg1' id: 23 product: price: 4900 auth: '7Y#d_1' id: 24 product: price: 1022 auth: 'WS4%a0' id: 25 product: price: 9131 auth: '7Y#d_1' ;_; // → { id: 25, product: { price: 91.31 }, auth: ['7Y#d_1'] } _;// → { id: 25, product: { price: 91.31 }, auth: ['7Y#d_1'] } _;// → { id: 24, product: { price: 10.22 }, auth: ['WS4%a0'] }
findIndex
Iterate over the given array and return the index of the first member that the expression
returns truthy for.
Usage: _.findIndex(array, expression/callback)
var orders = id: 21 product: price: 2112 auth: '3s!sa0' id: 22 product: price: 8921 auth: '@3dRg1' id: 23 product: price: 4900 auth: 'a44Fy+' id: 24 product: price: 1022 auth: 'WS4%a0' id: 25 product: price: 1131 auth: '7Y#d_1' ;_; // → 1_; // → 4
findLastIndex
Iterate over the given array and return the index of the last member that the expression
returns truthy for.
Usage: _.findLastIndex(array, expression/callback)
var orders = id: 21 product: price: 2112 auth: '3s!sa0' id: 22 product: price: 8921 auth: '@3dRg1' id: 23 product: price: 4900 auth: 'a44Fy+' id: 24 product: price: 9022 auth: 'a44Fy+' id: 25 product: price: 1131 auth: '7Y#d_1' ;_; // → 3_; // → 3
first
Gets the first element or first n
elements of an array.
if expression is provided, is returns as long the expression return truthy.
Usage: See below
var users = id: 1 user: name: 'foo' isAdmin: true id: 2 user: name: 'bar' isAdmin: false id: 3 user: name: 'baz' isAdmin: true ;// Returns the first user_;// → { id: 1, user: { name: 'foo', isAdmin: true } } // Return the first user whose not `admin`_;// → [{ id: 2, user: { name: 'bar', isAdmin: false } }] // Returns the first 2 users_;// → [users[0], users[1]] // Returns the first 2 `admin` users _;// → [users[0], users[2]]
flatten
Flattens a nested array (the nesting can be to any depth).
if shallow
set to true, the array will only be flattened a one level.
Usage: _.flatten(array, shallow[optional])
_;// → ['out', 'out', ['in'], 'out', 'out', ['in', 'in'], 'out', 'out'] _);// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
groupBy
Get a collection, expression/callback
and return an object composed of keys generated from the result of running each members in the collection on the expression
.
each key is an array contains the results members.
Usage: _.groupBy(array, expression)
var players = name: 'Gene' team: name: 'alpha' name: 'George' team: name: 'beta' name: 'Steve' team: name: 'gamma' name: 'Paula' team: name: 'beta' name: 'Scruath' team: name: 'gamma' ;_;// { // alpha: [{name: 'Gene', team: { name: 'alpha' } }],// betta: [{name: 'George', team: { name: 'beta' } }, {name: 'Paula', team: { name: 'beta' } }],// gamma: [{name: 'Steve', team: { name: 'gamma' } }, {name: 'Scruath', team: { name: 'gamma' } }]// }
last
Gets the last element or last n
elements of an array.
if expression is provided, is returns as long the expression return truthy.
Usage: See below
var users = id: 1 user: name: 'foo' isAdmin: true id: 2 user: name: 'bar' isAdmin: false id: 3 user: name: 'baz' isAdmin: false id: 4 user: name: 'zak' isAdmin: true ;// Returns the last user_;// → { id: 4, user: { name: 'zak', isAdmin: true } } // Return the last user whose not `admin`_;// → [{ id: 3, user: { name: 'baz', isAdmin: false } }] // Returns the last 2 users_;// → [users[2], users[3]] // Returns the last 2 `admin` users _;// → [users[0], users[3]]
map
Returns a new Array with the results of each expression execution.
Usage: _.map(array, expression)
Aliases: _.pluck
var users = id:1 user: name: 'Foo' id:2 user: name: 'Bar' id:3 user: name: 'Baz' ;_;// → ['Foo', 'Bar', 'Baz'] _// → [1, 2, 0]
max
Find and return the largest number in a given array.
if an expression
is provided, will return max value by expression.
Usage: _.max(array)
_ // → 9 //By expressionvar users = name: 'foo' score: 89 name: 'bar' score: 32 name: 'baz' score: 49 ;_; // → { name: 'foo', score: 89 } //Chaining examplevar users = player: ... score: 891 player: ... score: 121 player: ... score: 641 player: ... score: 491 value // → 891
min
Find and return the lowest number in a given array.
if an expression
is provided, will return min value by expression.
Usage: _.min(array)
_ // → 1 //By expressionvar users = user: score: 197 user: score: 212 user: score: 978 user: score: 121 ;_ // → { user: { score: 121 } } //Chaining examplevar users = player: ... score: 891 player: ... score: 121 player: ... score: 641 player: ... score: 491 value // → 121
omit
Get an array, and return a new array without the omitted members(by expression
).
Usage: _.omit(array, expression)
var users = id: 1 name: 'foo' id: 2 name: 'bar' id: 3 name: 'baz' ;_;// → [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }]
orderBy
Orders a specified array by the expression predicate.
It is ordered alphabetically for strings and numerically for numbers.
Usage: _.orderBy(array, expression/callback, reverse[optional])
Aliases: _.sortBy
var orders = id: 1 product: price: 2112 date: '01/01/2014' id: 2 product: price: 9921 date: '01/01/2014' id: 3 product: price: 9990 date: '01/01/2013' id: 4 product: price: 9999 date: '01/01/1970' ; _;// → [orders[3], orders[2], orders[1], orders[0]]; _;// → [orders[0], orders[1], orders[2], orders[3]]; _;// → [orders[3], orders[2], orders[1], orders[0]] _;// → [orders[1], orders[0], orders[2], orders[3]] _; // → [1, 2, 3, 4, 5]_; // → [5, 4, 3, 2, 1]; // → [1, 2, 3, 4, 5] // sort by multiple arguments_;// → [{ a:0, b:5 }, { a:1, b:4 }, { a:1, b:10 }]
remove
remove specific members from array by equality.
Usage: _.remove(array, args)
var collection = name: 'bar' name: 'foo' null 1;_;// → [{ name: 'bar' }]
reverse
Reverses a string or array(doesn't change the source array).
Usage: _.reverse(array/string)
_ // → [3, 2, 1]_ // → eliga
sum
Sum up all values within an array.
Usage: _.sum(array)
_ // → 15 //Chainig examplevar scores = player: ... score: 891 player: ... score: 121 player: ... score: 641 player: ... score: 491 ; value; // → 2144
unique
Get an array and filter duplicate members.
if expression
is provided it's filters by this expression
as unique identifier.
Usage: _.unique(array, expression[optional])
Aliases: uniq
_ // → [12, 3, 4, 5, 6] var orders = id:1 customer: name: 'John' id: 10 id:2 customer: name: 'William' id: 20 id:3 customer: name: 'John' id: 10 id:4 customer: name: 'William' id: 20 id:5 customer: name: 'Clive' id: 30 ;_;// Results:// [{ id:1, customer: { name: 'John', id: 10 } },// { id:2, customer: { name: 'William', id: 20 } },// { id:5, customer: { name: 'Clive', id: 30 } }] //Chaining Example: value; // → John, William, Clive
xor
Exclusive or filter by expression.
Usage: _.xor(arr1, arr2, expression[optional])
_;// → [2, 5] //Example with expression:var users1 = id: 0 details: first_name: 'foo' last_name: 'bar' id: 1 details: first_name: 'foo' last_name: 'baz' id: 2 details: first_name: 'foo' last_name: 'bag' ;var users2 = id: 3 details: first_name: 'foo' last_name: 'bar' id: 4 details: first_name: 'foo' last_name: 'baz' ;_;// → [{ id: 2, details: { first_name: 'foo', last_name: 'bag' } }]
Object
toArray
Convert objects into stable arrays.
if addKey set to true,the filter also attaches a new property $key
to the value containing the original key that was used in the object we are iterating over to reference the property.
Usage: _.toArray(object, boolean[optional])
var users = 0: name: 'Ariel' age: 25 1: name: 'Dan' age: 21 2: name: 'John' age: 31 ;_;// → [{name:'Ariel', age:25}, {name:'Dan', age:21}, {name:'John', age:31}] //Chaining example value; // → [{$key:'Ariel', age:25}, {$key:'Dan', age:21}, {$key:'John', age:31}]
keys
Creates an array composed of the own enumerable property names of an object.
if nested
set to true, it will return the properties in a recursively nested style(used mainly with parse.setter
, parse.getter
).
Usage: _.keys(object, nested[optional])
var user = name: 'Ariel M' age: 26 permissions: isAdmin: true details: address: city: 'Tel Aviv' zip: 61019 ;_; // → ['name', 'age', 'permissions', 'details'] _;// → ['name', 'age', 'permissions.isAdmin', 'details.address.city', 'details.address.zip']
parse
Convert expression
into function.
Usage: _.parse(expression)
Returns: Function(context, local)
context
: an object against which any expressions embedded in the strings are evaluated against.
local
: local variables context object, useful for overriding values in context.
Note: The returned function also has the following properties:
literal
: whether the expression's top-level node is a JavaScript literal.
constant
: whether the expression is made entirely of JavaScript constant literals.
assign
: {?function(context, value)}
– if the expression is assignable, this will be set to a function to change its value on the given context.
//Simple getter / setter functionsvar user = name: 'Ariel M.' age : 26 details: address: city: 'Tel Aviv' zip: 61019 ;var nameGetter = _;var nameSetter = nameGetterassign; ; // → 'Ariel M.';; // → 'Dan T.' //Example use local(override) objectvar local = age: 50 { return 'Hello ' + name + ' I\'m '+ age + ' years old.' };_user local;// → Hello Ariel M. I'm 50 years old. _literal // → true_constant // → true_constant // → false
String
endsWith
return whether string ends with the ends parameter.
Usage: _.endsWith(string, end, case-sensitive[optional])
_; // → true _; // → false
ltrim
Left trim. Similar to trim
, but only for left side.
Usage: _.ltrim(string, chars[optional])
_ // → 'foo '_ // → 'foobar'
rtrim
Reft trim. Similar to trim
, but only for right side.
Usage: _.rtrim(string, chars[optional])
_ // → ' foo'_ // → 'barfoo'
repeat
Repeats a string n times(fast).
Usage: _.repeat(string, n)
_; // → '**********'_; // → 'foo'
slugify
Transform text into a URL slug. Replaces whitespaces, with dash("-") or given argument.
Usage: _.slugify(input, sub[optional])
_; // → 'some-string-with-spaces'_; // → 'some#string#with#hashtags'
startsWith
return whether string
starts with the starts parameter.
Usage: _.startsWith(str, case-sensitive[optional])
_; // → true //Chaining example: ; // → false
stringular
get string with {n}
and replace matches with enumeration values.
Usage: _.stringular(str, args...)
_; // → 'lorem ipsum dolor sit amet' _;// → 'lorem ipsum dolor sit amet'
stripTags
strip out html
tags from string.
Usage: _.stripTags(string)
_;// → 'foo' //Chaining example value; // → 'Lorem Ipsum...'
trim
Strip whitespace (or other characters) from the beginning and end of a string.
Usage: _.trim(string, chars[optional])
_; // → 'bar'_; // → 'foo'
truncate
truncates a string given a specified length, providing a custom string to denote an omission.
Usage: _.truncate(str, length, suffix[optional], preserve[optinal])
var text = 'lorem ipsum dolor sit amet'; _; // → 'lorem ipsum dolor...'_; // → 'lorem ipsum d...'_; // → 'lorem ipsum dolor sit amet'
ucfirst
upper case first char.
Usage: _.ucfirst(string)
_; // → 'Ariel Mashraki' //Chaining example value; // → 'Ariel, Dan, John'
wrap
Wrap a string with another string.
Usage: _.wrap(string, start, end[optional])
_; // → 'barfoobar'_; // → '<p>text</p>' //Chaining example: value; // → 'Team members: Ariel, Dan, John'
Utils
copy
Creates a recursive copy of source
object into dest
object, could be an object or an array.
Usage: _.copy(src, dst)
var a = 123 a: 1 b: 2 ;var b;_; //Test result_; // → true
dictionary
Creates a new object without a prototype.
Usage: _.dictionary()
var map = _;console; // → undefined
equals
Determines if two objects or two values are equivalent.
Usage: _.equals(o1, o2)
_; // → true_; // → true_; // → true
extend
Extends the destination object dst
by copying own enumerable properties from the src
object(s) to dst
. You can specify multiple src
objects.
Usage: _.extend(dst, arg...)
_; // → {a: 3, b: 2, c: 4}
identity
identity
function returns its first argument.
Usage: _.identity(val)
_; // → 1
forEach
Invokes an iterator
function once for each member in a collection(object, array).
The iterator
function is invoked with (value, key/index, obj/array).
Usage: _.forEach(collection, iteratorFn, context[optional]);
noop
A function that performs no operations.
Usage: _.noop()
{ //... return cb || _noopargs;}