Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Jul 18, 2015
0 parents commit bc0e996
Show file tree
Hide file tree
Showing 11 changed files with 956 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
end_of_line = lf
charset = utf-8
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
insert_final_newline = false

[{,test/}{actual,fixtures}/**]
trim_trailing_whitespace = false
insert_final_newline = false

[templates/**]
trim_trailing_whitespace = false
insert_final_newline = false
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Enforce Unix newlines
* text eol=lf

# binaries
*.ai binary
*.psd binary
*.jpg binary
*.gif binary
*.png binary
*.jpeg binary
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.DS_Store
*.sublime-*
_gh_pages
bower_components
node_modules
npm-debug.log
actual
test/actual
temp
tmp
TODO.md
vendor
.idea
benchmark
coverage
18 changes: 18 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"asi": false,
"boss": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"esnext": true,
"immed": true,
"latedef": false,
"laxcomma": false,
"mocha": true,
"newcap": true,
"noarg": true,
"node": true,
"sub": true,
"undef": true,
"unused": true
}
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.12"
- "0.13"
- "iojs"
matrix:
fast_finish: true
allow_failures:
- node_js: "0.13"
178 changes: 178 additions & 0 deletions .verb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# {%= name %} {%= badge("fury") %}

> {%= description %}
## Install
{%= include("install-npm", {save: true}) %}

## Usage

Sort an array by the given object property:

```js
var arraySort = require('{%= name %}');

arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo');
//=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]
```

**Reverse order**

```js
arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true});
//=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]
```

## Table of contents

<!-- toc -->

## Params

```js
arraySort(array, comparisonArgs);
```

- `array`: **{Array}** The array to sort
- `comparisonArgs`: **{Function|String|Array}**: Any number of functions, object paths, or arrays or objects paths and functions may be passed.


## Examples

### Object paths

Say you have the following blog posts:

```js
var collection = [
{ path: 'a.md', locals: { date: '2014-01-09' } },
{ path: 'f.md', locals: { date: '2014-01-02' } },
{ path: 'd.md', locals: { date: '2013-05-06' } },
{ path: 'e.md', locals: { date: '2015-01-02' } },
{ path: 'b.md', locals: { date: '2012-01-02' } },
{ path: 'f.md', locals: { date: '2014-06-01' } },
{ path: 'c.md', locals: { date: '2015-04-12' } },
{ path: 'g.md', locals: { date: '2014-02-02' } },
];
```

and you want to sort them by `locals.date`:

```js
arraySort(collection, 'locals.date');
```

**Result**

```js
[
{ path: 'b.md', locals: { date: '2012-01-02' } },
{ path: 'd.md', locals: { date: '2013-05-06' } },
{ path: 'f.md', locals: { date: '2014-01-02' } },
{ path: 'a.md', locals: { date: '2014-01-09' } },
{ path: 'g.md', locals: { date: '2014-02-02' } },
{ path: 'f.md', locals: { date: '2014-06-01' } },
{ path: 'e.md', locals: { date: '2015-01-02' } },
{ path: 'c.md', locals: { date: '2015-04-12' } }
];
```

### Sort by multiple properties and functions

Here are the "posts" we want to sort:

```js
var posts = [
{ path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } },
{ path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } },
{ path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } },
{ path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } },
{ path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } },
{ path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } },
{ path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } },
{ path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } },
{ path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } },
{ path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } },
{ path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } },
{ path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } },
{ path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } },
];
```

**The goal**

Sort "posts" by the following:

- `locals.date`: our first choice
- `locals.foo`: Use this when mulitiple posts have the same `locals.date` value
- `locals.bar`: Use this when mulitiple posts have the same `locals.foo` value


```js
// let's create a reusable comparison function
var compare = function(prop) {
// the last arg, `fn` is the internal comparison function
// used by the lib. it's exposed here for convenience
return function (a, b, fn) {
var valA = get(a, prop);
var valB = get(b, prop);
return fn(valA, valB);
};
};
```

**Time to sort!**

```js
// the comparison args can be an array or a list, makes no difference
arraySort(posts, ['locals.date', compare('locals.foo'), compare('locals.bar')]);
```

**Result**

```js
[
{ path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } },
{ path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } },
{ path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } },
{ path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } },
{ path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } },
{ path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } },
{ path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } },
{ path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } },
{ path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } },
{ path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } },
{ path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } },
{ path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } },
{ path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } }
]);
```

### Custom functions

If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the [docs for `Array.sort()`][mozilla].


## Related projects
{%= related(verb.related.list, {remove: name}) %}

## Running tests
{%= include("tests") %}

## Contributing
{%= include("contributing") %}

## Author
{%= include("author") %}

## License
{%= copyright() %}
{%= license() %}

***

{%= include("footer") %}

[mozilla]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

{%= reflinks(['verb']) %}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015, Jon Schlinkert.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading

0 comments on commit bc0e996

Please sign in to comment.