Skip to content

Commit bc0e996

Browse files
committed
first commit
0 parents  commit bc0e996

11 files changed

+956
-0
lines changed

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
end_of_line = lf
7+
charset = utf-8
8+
indent_size = 2
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+
insert_final_newline = false
15+
16+
[{,test/}{actual,fixtures}/**]
17+
trim_trailing_whitespace = false
18+
insert_final_newline = false
19+
20+
[templates/**]
21+
trim_trailing_whitespace = false
22+
insert_final_newline = false

.gitattributes

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Enforce Unix newlines
2+
* text eol=lf
3+
4+
# binaries
5+
*.ai binary
6+
*.psd binary
7+
*.jpg binary
8+
*.gif binary
9+
*.png binary
10+
*.jpeg binary

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.DS_Store
2+
*.sublime-*
3+
_gh_pages
4+
bower_components
5+
node_modules
6+
npm-debug.log
7+
actual
8+
test/actual
9+
temp
10+
tmp
11+
TODO.md
12+
vendor
13+
.idea
14+
benchmark
15+
coverage

.jshintrc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"asi": false,
3+
"boss": true,
4+
"curly": true,
5+
"eqeqeq": true,
6+
"eqnull": true,
7+
"esnext": true,
8+
"immed": true,
9+
"latedef": false,
10+
"laxcomma": false,
11+
"mocha": true,
12+
"newcap": true,
13+
"noarg": true,
14+
"node": true,
15+
"sub": true,
16+
"undef": true,
17+
"unused": true
18+
}

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- "0.10"
5+
- "0.12"
6+
- "0.13"
7+
- "iojs"
8+
matrix:
9+
fast_finish: true
10+
allow_failures:
11+
- node_js: "0.13"

.verb.md

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# {%= name %} {%= badge("fury") %}
2+
3+
> {%= description %}
4+
5+
## Install
6+
{%= include("install-npm", {save: true}) %}
7+
8+
## Usage
9+
10+
Sort an array by the given object property:
11+
12+
```js
13+
var arraySort = require('{%= name %}');
14+
15+
arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo');
16+
//=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]
17+
```
18+
19+
**Reverse order**
20+
21+
```js
22+
arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true});
23+
//=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]
24+
```
25+
26+
## Table of contents
27+
28+
<!-- toc -->
29+
30+
## Params
31+
32+
```js
33+
arraySort(array, comparisonArgs);
34+
```
35+
36+
- `array`: **{Array}** The array to sort
37+
- `comparisonArgs`: **{Function|String|Array}**: Any number of functions, object paths, or arrays or objects paths and functions may be passed.
38+
39+
40+
## Examples
41+
42+
### Object paths
43+
44+
Say you have the following blog posts:
45+
46+
```js
47+
var collection = [
48+
{ path: 'a.md', locals: { date: '2014-01-09' } },
49+
{ path: 'f.md', locals: { date: '2014-01-02' } },
50+
{ path: 'd.md', locals: { date: '2013-05-06' } },
51+
{ path: 'e.md', locals: { date: '2015-01-02' } },
52+
{ path: 'b.md', locals: { date: '2012-01-02' } },
53+
{ path: 'f.md', locals: { date: '2014-06-01' } },
54+
{ path: 'c.md', locals: { date: '2015-04-12' } },
55+
{ path: 'g.md', locals: { date: '2014-02-02' } },
56+
];
57+
```
58+
59+
and you want to sort them by `locals.date`:
60+
61+
```js
62+
arraySort(collection, 'locals.date');
63+
```
64+
65+
**Result**
66+
67+
```js
68+
[
69+
{ path: 'b.md', locals: { date: '2012-01-02' } },
70+
{ path: 'd.md', locals: { date: '2013-05-06' } },
71+
{ path: 'f.md', locals: { date: '2014-01-02' } },
72+
{ path: 'a.md', locals: { date: '2014-01-09' } },
73+
{ path: 'g.md', locals: { date: '2014-02-02' } },
74+
{ path: 'f.md', locals: { date: '2014-06-01' } },
75+
{ path: 'e.md', locals: { date: '2015-01-02' } },
76+
{ path: 'c.md', locals: { date: '2015-04-12' } }
77+
];
78+
```
79+
80+
### Sort by multiple properties and functions
81+
82+
Here are the "posts" we want to sort:
83+
84+
```js
85+
var posts = [
86+
{ path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } },
87+
{ path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } },
88+
{ path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } },
89+
{ path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } },
90+
{ path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } },
91+
{ path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } },
92+
{ path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } },
93+
{ path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } },
94+
{ path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } },
95+
{ path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } },
96+
{ path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } },
97+
{ path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } },
98+
{ path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } },
99+
];
100+
```
101+
102+
**The goal**
103+
104+
Sort "posts" by the following:
105+
106+
- `locals.date`: our first choice
107+
- `locals.foo`: Use this when mulitiple posts have the same `locals.date` value
108+
- `locals.bar`: Use this when mulitiple posts have the same `locals.foo` value
109+
110+
111+
```js
112+
// let's create a reusable comparison function
113+
var compare = function(prop) {
114+
// the last arg, `fn` is the internal comparison function
115+
// used by the lib. it's exposed here for convenience
116+
return function (a, b, fn) {
117+
var valA = get(a, prop);
118+
var valB = get(b, prop);
119+
return fn(valA, valB);
120+
};
121+
};
122+
```
123+
124+
**Time to sort!**
125+
126+
```js
127+
// the comparison args can be an array or a list, makes no difference
128+
arraySort(posts, ['locals.date', compare('locals.foo'), compare('locals.bar')]);
129+
```
130+
131+
**Result**
132+
133+
```js
134+
[
135+
{ path: 'b.md', locals: { date: '2012-01-02', foo: 'ccc', bar: 9 } },
136+
{ path: 'f.md', locals: { date: '2014-01-01', foo: 'mmm', bar: 2 } },
137+
{ path: 'k.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 1 } },
138+
{ path: 'd.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 3 } },
139+
{ path: 'j.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 4 } },
140+
{ path: 'i.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 5 } },
141+
{ path: 'h.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 6 } },
142+
{ path: 'l.md', locals: { date: '2014-01-01', foo: 'xxx', bar: 7 } },
143+
{ path: 'a.md', locals: { date: '2014-01-01', foo: 'zzz', bar: 1 } },
144+
{ path: 'g.md', locals: { date: '2014-02-02', foo: 'yyy', bar: 12 } },
145+
{ path: 'f.md', locals: { date: '2014-06-01', foo: 'rrr', bar: 10 } },
146+
{ path: 'e.md', locals: { date: '2015-01-02', foo: 'aaa', bar: 8 } },
147+
{ path: 'c.md', locals: { date: '2015-04-12', foo: 'ttt', bar: 11 } }
148+
]);
149+
```
150+
151+
### Custom functions
152+
153+
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].
154+
155+
156+
## Related projects
157+
{%= related(verb.related.list, {remove: name}) %}
158+
159+
## Running tests
160+
{%= include("tests") %}
161+
162+
## Contributing
163+
{%= include("contributing") %}
164+
165+
## Author
166+
{%= include("author") %}
167+
168+
## License
169+
{%= copyright() %}
170+
{%= license() %}
171+
172+
***
173+
174+
{%= include("footer") %}
175+
176+
[mozilla]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
177+
178+
{%= reflinks(['verb']) %}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015, Jon Schlinkert.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

0 commit comments

Comments
 (0)