sort-object
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -12,3 +12,2 @@ /*! | ||
var sortAsc = require('sort-asc'); | ||
var _ = require ('lodash'); | ||
@@ -18,3 +17,3 @@ | ||
var sort = {desc: sortDesc, asc: sortAsc}; | ||
var fn, opts = {}, keys = _.keys(obj); | ||
var fn, opts = {}, keys = Object.keys(obj); | ||
@@ -29,3 +28,7 @@ // if `options` is an array, assume it's keys | ||
} else { | ||
_.extend(opts, options); | ||
for (var opt in options) { | ||
if (options.hasOwnProperty(opt)) { | ||
opts[opt] = options[opt] | ||
} | ||
} | ||
} | ||
@@ -32,0 +35,0 @@ |
{ | ||
"name": "sort-object", | ||
"description": "Sort the keys in an object.", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"homepage": "https://github.com/helpers/sort-object", | ||
@@ -53,3 +53,2 @@ "author": { | ||
"dependencies": { | ||
"lodash": "^2.4.1", | ||
"sort-asc": "^0.1.0", | ||
@@ -56,0 +55,0 @@ "sort-desc": "^0.1.1" |
@@ -18,3 +18,3 @@ # sort-object [![NPM version](https://badge.fury.io/js/sort-object.png)](http://badge.fury.io/js/sort-object) | ||
Sort the keys on an object | ||
By default, the keys on an object will be sorted in descending order: | ||
@@ -26,2 +26,27 @@ ```js | ||
The second param can be an object of `options` OR an array of `keys`: | ||
**object** | ||
```js | ||
console.log(sortObj({a: 1, c: 2, b: 3}, {keys: ['a', 'b']})); | ||
//=> {a: 1, b: 3} | ||
``` | ||
**array** | ||
```js | ||
console.log(sortObj({a: 1, c: 2, b: 3}, ['a', 'c'])); | ||
//=> {a: 1, c: 2} | ||
``` | ||
## Options | ||
* `keys` {Array} The returned object will contain only the specified keys, in the same order. | ||
* `sort` {Function} Sort function to sort the keys using JavaScript's `.sort()` method. | ||
* `sortOrder` {String} Valid values are `desc` or `asc`, case insensitive. | ||
* `sortBy` {String} Sort function that is passed the entire object, rather than just the keys - as with the `.sort()` method. | ||
### options.keys | ||
Create a new object with only the given keys. | ||
@@ -36,9 +61,13 @@ | ||
Sort the keys using a custom function. | ||
### options.sort | ||
Function to be passed to javascript's `.sort()` method: | ||
```js | ||
var o = {a: 1, c: 2, e: 5, d: 4, b: 3}; | ||
var obj = sortObj(o, {fn: function (a, b) { | ||
return a < b ? -1 : 1; | ||
}}); | ||
var obj = sortObj(o, { | ||
sort: function (a, b) { | ||
return a < b ? -1 : 1; | ||
} | ||
}); | ||
console.log(obj); | ||
@@ -48,2 +77,30 @@ //=> {a: 1, b: 3, c: 2, d: 4, e: 5} | ||
### options.sortOrder | ||
Valid values are `desc` or `asc`, case insensitive: | ||
```js | ||
var o = {a: 1, c: 2, e: 5, d: 4, b: 3}; | ||
console.log(sortObj(o, {sortOrder: 'ASC'})); | ||
//=> {e: 5, d: 4, c: 3, b: 2, a: 1} | ||
``` | ||
### options.sortBy | ||
Function that returns an array of keys to sort by: | ||
```js | ||
var old = {one: 'aa', two: 'bc', three: 'ab'}; | ||
var o = sortObj(old, { | ||
sortBy: function (obj) { | ||
var arr = []; | ||
Object.keys(obj).filter(function(key) { | ||
if (/^a/.test(obj[key])) arr.push(key); | ||
}); | ||
return arr.reverse(); | ||
} | ||
}); | ||
//=> {three: 'ab', one: 'aa'} | ||
``` | ||
## Author | ||
@@ -62,2 +119,2 @@ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 15, 2014._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 21, 2014._ |
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
12824
2
151
115
- Removedlodash@^2.4.1
- Removedlodash@2.4.2(transitive)