From 6d23c885e2aed0d62d6c406db790665edd896438 Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Sat, 18 Jul 2015 03:11:36 -0400 Subject: [PATCH] adds examples --- examples/blog-posts.js | 14 ++++++++++++++ examples/custom-function.js | 25 +++++++++++++++++++++++++ examples/custom-functions.js | 22 ++++++++++++++++++++++ examples/multiple-props.js | 21 +++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 examples/blog-posts.js create mode 100644 examples/custom-function.js create mode 100644 examples/custom-functions.js create mode 100644 examples/multiple-props.js diff --git a/examples/blog-posts.js b/examples/blog-posts.js new file mode 100644 index 0000000..bf6156e --- /dev/null +++ b/examples/blog-posts.js @@ -0,0 +1,14 @@ +var arraySort = require('..'); + +var posts = [ + { path: 'c.md', locals: { date: '2014-01-09' } }, + { path: 'a.md', locals: { date: '2014-01-02' } }, + { path: 'b.md', locals: { date: '2013-05-06' } }, +]; + +// by `locals.date` +console.log(arraySort(posts, 'locals.date')); + +// by `path` +console.log(arraySort(posts, 'path')); + diff --git a/examples/custom-function.js b/examples/custom-function.js new file mode 100644 index 0000000..682148d --- /dev/null +++ b/examples/custom-function.js @@ -0,0 +1,25 @@ +var arraySort = require('..'); + +var arr = [ + {one: 'w', two: 'b'}, + {one: 'z', two: 'a'}, + {one: 'x', two: 'c'}, + {one: 'y', two: 'd'}, +]; + +function compare(prop) { + return function (a, b) { + return a[prop].localeCompare(b[prop]); + }; +} + +var result = arraySort(arr, function (a, b) { + return a.two.localeCompare(b.two); +}); + +console.log(result); +// Results in: +// [ { one: 'z', two: 'a' }, +// { one: 'w', two: 'b' }, +// { one: 'x', two: 'c' }, +// { one: 'y', two: 'd' } ] diff --git a/examples/custom-functions.js b/examples/custom-functions.js new file mode 100644 index 0000000..81dec4d --- /dev/null +++ b/examples/custom-functions.js @@ -0,0 +1,22 @@ +var arraySort = require('..'); + +var arr = [ + {foo: 'w', bar: 'y', baz: 'w'}, + {foo: 'x', bar: 'y', baz: 'w'}, + {foo: 'x', bar: 'y', baz: 'z'}, + {foo: 'x', bar: 'x', baz: 'w'}, +]; + +function compare(prop) { + return function (a, b) { + return a[prop].localeCompare(b[prop]); + }; +} + +console.log(arraySort(arr, compare('foo'), compare('bar'), compare('baz'))); + +// Results in: +// [ { foo: 'w', bar: 'y', baz: 'w' }, +// { foo: 'x', bar: 'x', baz: 'w' }, +// { foo: 'x', bar: 'y', baz: 'w' }, +// { foo: 'x', bar: 'y', baz: 'z' } ] diff --git a/examples/multiple-props.js b/examples/multiple-props.js new file mode 100644 index 0000000..bb6a385 --- /dev/null +++ b/examples/multiple-props.js @@ -0,0 +1,21 @@ +var arraySort = require('..'); + +var posts = [ + { locals: { foo: 'bbb', date: '2013-05-06' }}, + { locals: { foo: 'aaa', date: '2012-01-02' }}, + { locals: { foo: 'ccc', date: '2014-01-02' }}, + { locals: { foo: 'ccc', date: '2015-01-02' }}, + { locals: { foo: 'bbb', date: '2014-06-01' }}, + { locals: { foo: 'aaa', date: '2014-02-02' }}, +]; + +// sort by `locals.foo`, then `locals.date` +var result = arraySort(posts, ['locals.foo', 'locals.date']); + +console.log(result); +// [ { locals: { foo: 'aaa', date: '2012-01-02' } }, +// { locals: { foo: 'aaa', date: '2014-02-02' } }, +// { locals: { foo: 'bbb', date: '2013-05-06' } }, +// { locals: { foo: 'bbb', date: '2014-06-01' } }, +// { locals: { foo: 'ccc', date: '2014-01-02' } }, +// { locals: { foo: 'ccc', date: '2015-01-02' } } ]