lodash-contrib
Advanced tools
Comparing version 241.4.0 to 241.4.2
@@ -18,3 +18,4 @@ // lodash-contrib (lodash.array.builders.js 0.0.1) | ||
var slice = Array.prototype.slice, | ||
concat = Array.prototype.concat; | ||
concat = Array.prototype.concat, | ||
sort = Array.prototype.sort; | ||
@@ -195,2 +196,32 @@ var existy = function(x) { return x != null; }; | ||
return slice.call(obj).reverse(); | ||
}, | ||
// Returns copy or array sorted according to arbitrary ordering | ||
// order must be an array of values; defines the custom sort | ||
// key must be one of: missing/null, a string, or a function; | ||
collate: function(array, order, key) { | ||
if (!_.isArray(array)) throw new TypeError("expected an array as the first argument"); | ||
if (!_.isArray(order)) throw new TypeError("expected an array as the second argument"); | ||
return sort.call(array, function (a, b) { | ||
if(_.isFunction(key)) { | ||
valA = key.call(a); | ||
valB = key.call(b); | ||
} else if(existy(key)) { | ||
valA = a[key]; | ||
valB = b[key]; | ||
} else { | ||
valA = a; | ||
valB = b; | ||
} | ||
var rankA = _.indexOf(order, valA); | ||
var rankB = _.indexOf(order, valB); | ||
if(rankA === -1) return 1; | ||
if(rankB === -1) return -1; | ||
return rankA - rankB; | ||
}); | ||
} | ||
@@ -197,0 +228,0 @@ }); |
@@ -16,2 +16,9 @@ // lodash-contrib (lodash.util.strings.js 0.0.1) | ||
// No reason to create regex more than once | ||
var plusRegex = /\+/g; | ||
var urlDecode = function (s) { | ||
return decodeURIComponent(s.replace(plusRegex, '%20')); | ||
}; | ||
// Mixing in the string utils | ||
@@ -26,2 +33,14 @@ // ---------------------------- | ||
// Parses a query string into a hash | ||
fromQuery: function(str) { | ||
var obj = str.split('&').reduce(function (seed, param) { | ||
var pair = param.split('='); | ||
var key = urlDecode(pair[0]); | ||
var val = urlDecode(pair[1]); | ||
seed[key] = val; | ||
return seed; | ||
}, {}); | ||
return obj; | ||
}, | ||
// Implodes and array of chars into a string | ||
@@ -48,2 +67,10 @@ implode: function (a) { | ||
// Creates a query string from a hash | ||
toQuery: function(obj) { | ||
var parameters = _.map(obj, function (v, k) { | ||
return encodeURIComponent(k) + '=' + encodeURIComponent(v); | ||
}); | ||
return parameters.join('&'); | ||
}, | ||
// Reports whether a string contains a search string. | ||
@@ -50,0 +77,0 @@ strContains: function (str, search) { |
@@ -0,0 +0,0 @@ // lodash-contrib v241.2.0 |
{ | ||
"name": "lodash-contrib", | ||
"description": "The brass buckles on lodash's utility belt", | ||
"version": "241.4.0", | ||
"version": "241.4.2", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
@@ -200,2 +200,25 @@ $(document).ready(function() { | ||
}); | ||
test('collate', function() { | ||
var properOrder = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']; | ||
var itemsBare = ['green', 'yellow', 'violet', 'red', 'indigo', 'orange', 'blue']; | ||
var itemsObj = [{'prop':'green'}, {'prop':'yellow'}, {'prop':'violet'}, {'prop':'red'}, {'prop':'indigo'}, {'prop':'orange'}, {'prop':'blue'}]; | ||
var itemsRaw = ['g', 'y', 'v', 'r', 'i', 'o', 'b']; | ||
var rawConvertFunc = function() { | ||
return ({ | ||
'r': 'red', | ||
'o': 'orange', | ||
'y': 'yellow', | ||
'g': 'green', | ||
'b': 'blue', | ||
'i': 'indigo', | ||
'v': 'violet' | ||
})[this]; | ||
}; | ||
deepEqual(_.collate(itemsBare, properOrder), properOrder, 'returns an array of scalars whose elements are ordered according to provided lexicon'); | ||
deepEqual(_.collate(itemsObj, properOrder, 'prop'), [{'prop':'red'}, {'prop':'orange'}, {'prop':'yellow'}, {'prop':'green'}, {'prop':'blue'}, {'prop':'indigo'}, {'prop':'violet'}], 'returns an array of objects that are ordered according to provided lexicon'); | ||
deepEqual(_.collate(itemsRaw, properOrder, rawConvertFunc), ['r', 'o', 'y', 'g', 'b', 'i', 'v'], 'returns an array whose elements are sorted by derived value according to provided lexicon'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1504148
90
28794