waterline-criteria
Advanced tools
Comparing version 0.9.2 to 0.9.3
{ | ||
"name": "waterline-criteria", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"main": "lib/waterline-criteria.js", | ||
@@ -17,4 +17,4 @@ "ignore": [ | ||
"dependencies": { | ||
"lodash": "~1.3.1" | ||
"lodash": "~2.1.0" | ||
} | ||
} |
@@ -14,2 +14,10 @@ module.exports = function (grunt) { | ||
replace: { | ||
version: { | ||
src: ['bower.json'], | ||
dest: 'bower.json', | ||
replacements: [{ | ||
from: /"version":\s*"[^"]+"/, | ||
to: '"version": "<%= pkg.version %>"' | ||
}] | ||
}, | ||
bower: { | ||
@@ -39,3 +47,3 @@ src: ['index.js'], | ||
grunt.registerTask('default', ['clean', 'replace', 'uglify']); | ||
grunt.registerTask('default', ['clean', 'replace:version', 'replace:bower', 'uglify']); | ||
}; |
61
index.js
@@ -48,44 +48,39 @@ var _ = require('lodash'); | ||
// Sort Function | ||
// Taken From: http://stackoverflow.com/a/4760279/909625 | ||
function sortData(data, sortCriteria) { | ||
var keyPair; | ||
data.sort(function (a, b) { | ||
var sortIndex = 0; | ||
function dynamicSort(property) { | ||
var sortOrder = 1; | ||
if(property[0] === '-') { | ||
sortOrder = -1; | ||
property = property.substr(1); | ||
} | ||
function comp(key, val) { | ||
return function (a,b) { | ||
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; | ||
return result * sortOrder; | ||
}; | ||
} | ||
// Check if one record has the value and the other doesn't | ||
if(a[key] && !b[key]) { | ||
if(val === 1) return 1; | ||
return -1; | ||
} | ||
function dynamicSortMultiple() { | ||
var props = arguments; | ||
return function (obj1, obj2) { | ||
var i = 0, result = 0, numberOfProperties = props.length; | ||
if(!a[key] && b[key]) { | ||
if(val === 1) return -1; | ||
return 1; | ||
while(result === 0 && i < numberOfProperties) { | ||
result = dynamicSort(props[i])(obj1, obj2); | ||
i++; | ||
} | ||
return result; | ||
}; | ||
} | ||
if(a[key] < b[key]){ | ||
if(val === 1) return -1; | ||
return 1; | ||
} | ||
if (a[key] > b[key]) { | ||
if(val === 1) return 1; | ||
return -1; | ||
} | ||
if(!sortCriteria.length){ | ||
return 0; | ||
} | ||
if (++sortIndex >= sortCriteria.length) return 0; | ||
keyPair = getKeyPair(sortCriteria, sortIndex); | ||
return comp(keyPair.key, keyPair.val); | ||
} | ||
keyPair = getKeyPair(sortCriteria, sortIndex); | ||
return comp(keyPair.key, keyPair.val); | ||
// build sort criteria in the format ['firstName', '-lastName'] | ||
var sortArray = []; | ||
_.each(_.keys(sortCriteria), function(key) { | ||
if(sortCriteria[key] === -1) sortArray.push('-' + key.toLowerCase()); | ||
else sortArray.push(key.toLowerCase()); | ||
}); | ||
data.sort(dynamicSortMultiple.apply(null, sortArray)); | ||
return data; | ||
@@ -92,0 +87,0 @@ } |
{ | ||
"name": "waterline-criteria", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"description": "Utility library for use in Sails adapters where you need to filter a list of output using a criteria object", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha", | ||
"prepublish": "grunt" | ||
}, | ||
@@ -20,3 +21,3 @@ "repository": { | ||
"dependencies": { | ||
"lodash": "~1.3.1" | ||
"lodash": "~2.1.0" | ||
}, | ||
@@ -29,4 +30,5 @@ "devDependencies": { | ||
"grunt-contrib-clean": "~0.5.0", | ||
"grunt-contrib-uglify": "~0.2.2" | ||
"grunt-contrib-uglify": "~0.2.2", | ||
"grunt-cli": "~0.1.9" | ||
} | ||
} |
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
15671
450
7
+ Addedlodash@2.1.0(transitive)
- Removedlodash@1.3.1(transitive)
Updatedlodash@~2.1.0