Skip to content

Commit

Permalink
Fixes #19.
Browse files Browse the repository at this point in the history
Stable Version 1.0.0-alpha.4-3.
  • Loading branch information
jmdobry committed Nov 12, 2014
1 parent fa95ba6 commit d936ad9
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 75 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
##### 1.0.0-alpha.4-3 - 11 November 2014

###### Backwards compatible bug fixes
- #19 - multiple orderBy does not work

##### 1.0.0-alpha.4-2 - 09 November 2014

###### Backwards compatible API changes
Expand Down
22 changes: 14 additions & 8 deletions Gruntfile.js
Expand Up @@ -34,6 +34,10 @@ module.exports = function (grunt) {
files: ['src/**/*.js'],
tasks: ['build']
},
lite: {
files: ['src/**/*.js'],
tasks: ['jshint', 'browserify']
},
n: {
files: ['src/**/*.js', 'test/both/**/*.js', 'test/node/**/*.js'],
tasks: ['n']
Expand All @@ -43,14 +47,14 @@ module.exports = function (grunt) {
main: {
options: {
banner: '/**\n' +
'* @author Jason Dobry <jason.dobry@gmail.com>\n' +
'* @file js-data.min.js\n' +
'* @version <%= pkg.version %> - Homepage <http://www.js-data.io/>\n' +
'* @copyright (c) 2014 Jason Dobry\n' +
'* @license MIT <https://github.com/js-data/js-data/blob/master/LICENSE>\n' +
'*\n' +
'* @overview Data store.\n' +
'*/\n'
'* @author Jason Dobry <jason.dobry@gmail.com>\n' +
'* @file js-data.min.js\n' +
'* @version <%= pkg.version %> - Homepage <http://www.js-data.io/>\n' +
'* @copyright (c) 2014 Jason Dobry\n' +
'* @license MIT <https://github.com/js-data/js-data/blob/master/LICENSE>\n' +
'*\n' +
'* @overview Data store.\n' +
'*/\n'
},
files: {
'dist/js-data.min.js': ['dist/js-data.js']
Expand All @@ -77,6 +81,7 @@ module.exports = function (grunt) {
dev: {
browsers: ['Chrome'],
autoWatch: true,
autoWatchBatchDelay: 1000,
singleRun: false,
reporters: ['spec'],
preprocessors: {}
Expand Down Expand Up @@ -153,5 +158,6 @@ module.exports = function (grunt) {
'uglify:main'
]);
grunt.registerTask('go', ['build', 'watch:dist']);
grunt.registerTask('golite', ['jshint', 'browserify', 'watch:lite']);
grunt.registerTask('default', ['build']);
};
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@ Unlike Backbone and Ember Models, js-data does not require the use of getters an

Supporting relations, computed properties, model lifecycle control and a slew of other features, js-data is the tool for giving your data the respect it deserves.

__Latest Release:__ [1.0.0-alpha.4-2](https://github.com/js-data/js-data/releases/tag/1.0.0-alpha.4-2)
__Latest Release:__ [1.0.0-alpha.4-3](https://github.com/js-data/js-data/releases/tag/1.0.0-alpha.4-3)

js-data is pre-release. The API is subject to change, though the current api is well tested.

Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "js-data",
"description": "Robust, framework-agnostic in-memory data store.",
"version": "1.0.0-alpha.4-2",
"version": "1.0.0-alpha.4-3",
"homepage": "http://www.js-data.io",
"repository": {
"type": "git",
Expand Down
85 changes: 54 additions & 31 deletions dist/js-data.js
@@ -1,7 +1,7 @@
/**
* @author Jason Dobry <jason.dobry@gmail.com>
* @file js-data.js
* @version 1.0.0-alpha.4-2 - Homepage <http://www.js-data.io/>
* @version 1.0.0-alpha.4-3 - Homepage <http://www.js-data.io/>
* @copyright (c) 2014 Jason Dobry
* @license MIT <https://github.com/js-data/js-data/blob/master/LICENSE>
*
Expand Down Expand Up @@ -3433,6 +3433,42 @@ function lifecycleNoop(resourceName, attrs) {
return attrs;
}

function compare(orderBy, index, a, b) {
var def = orderBy[index];
var cA = a[def[0]], cB = b[def[0]];
if (DSUtils.isString(cA)) {
cA = DSUtils.upperCase(cA);
}
if (DSUtils.isString(cB)) {
cB = DSUtils.upperCase(cB);
}
if (def[1] === 'DESC') {
if (cB < cA) {
return -1;
} else if (cB > cA) {
return 1;
} else {
if (index < orderBy.length - 1) {
return compare(orderBy, index + 1, a, b);
} else {
return 0;
}
}
} else {
if (cA < cB) {
return -1;
} else if (cA > cB) {
return 1;
} else {
if (index < orderBy.length - 1) {
return compare(orderBy, index + 1, a, b);
} else {
return 0;
}
}
}
}

function Defaults() {
}

Expand Down Expand Up @@ -3599,38 +3635,23 @@ defaultsPrototype.defaultFilter = function (collection, resourceName, params, op

// Apply 'orderBy'
if (orderBy) {
DSUtils.forEach(orderBy, function (def) {
var index = 0;
DSUtils.forEach(orderBy, function (def, i) {
if (DSUtils.isString(def)) {
def = [def, 'ASC'];
orderBy[i] = [def, 'ASC'];
} else if (!DSUtils.isArray(def)) {
throw new _this.errors.IllegalArgumentError('DS.filter(resourceName[, params][, options]): ' + DSUtils.toJson(def) + ': Must be a string or an array!', { params: { 'orderBy[i]': { actual: typeof def, expected: 'string|array' } } });
}
filtered = DSUtils.sort(filtered, function (a, b) {
var cA = a[def[0]], cB = b[def[0]];
if (DSUtils.isString(cA)) {
cA = DSUtils.upperCase(cA);
}
if (DSUtils.isString(cB)) {
cB = DSUtils.upperCase(cB);
}
if (def[1] === 'DESC') {
if (cB < cA) {
return -1;
} else if (cB > cA) {
return 1;
} else {
return 0;
}
} else {
if (cA < cB) {
return -1;
} else if (cA > cB) {
return 1;
} else {
return 0;
throw new _this.errors.IllegalArgumentError('DS.filter(resourceName[, params][, options]): ' + DSUtils.toJson(def) + ': Must be a string or an array!', {
params: {
'orderBy[i]': {
actual: typeof def,
expected: 'string|array'
}
}
}
});
});
}
});
filtered = DSUtils.sort(filtered, function (a, b) {
return compare(orderBy, index, a, b);
});
}

Expand Down Expand Up @@ -5128,7 +5149,9 @@ module.exports = {
isEmpty: require('mout/lang/isEmpty'),
isRegExp: isRegExp,
toJson: JSON.stringify,
fromJson: JSON.parse,
fromJson: function (json) {
return this.isString(json) ? JSON.parse(json) : json;
},
makePath: require('mout/string/makePath'),
upperCase: require('mout/string/upperCase'),
pascalCase: require('mout/string/pascalCase'),
Expand Down
4 changes: 2 additions & 2 deletions dist/js-data.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion karma.start.js
@@ -1,5 +1,5 @@
// Setup global test variables
var store, DSUtils, dsHttpAdapter, dsLocalStorageAdapter, p1, p2, p3, p4, p5;
var store, DSUtils, dsHttpAdapter, dsLocalStorageAdapter, p1, p2, p3, p4, p5, p6;

var Post, User, Organization, Comment, Profile;
var user1, organization2, comment3, profile4;
Expand Down Expand Up @@ -212,6 +212,7 @@ beforeEach(function () {
p3 = { author: 'Mike', age: 32, id: 7 };
p4 = { author: 'Adam', age: 33, id: 8 };
p5 = { author: 'Adam', age: 33, id: 9 };
p6 = { author: 'Adam', age: 33, id: 10 };

user1 = {
name: 'John Anderson',
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "js-data",
"description": "Robust, framework-agnostic in-memory data store.",
"version": "1.0.0-alpha.4-2",
"version": "1.0.0-alpha.4-3",
"homepage": "http://www.js-data.io",
"repository": {
"type": "git",
Expand Down
79 changes: 50 additions & 29 deletions src/datastore/index.js
Expand Up @@ -18,6 +18,42 @@ function lifecycleNoop(resourceName, attrs) {
return attrs;
}

function compare(orderBy, index, a, b) {
var def = orderBy[index];
var cA = a[def[0]], cB = b[def[0]];
if (DSUtils.isString(cA)) {
cA = DSUtils.upperCase(cA);
}
if (DSUtils.isString(cB)) {
cB = DSUtils.upperCase(cB);
}
if (def[1] === 'DESC') {
if (cB < cA) {
return -1;
} else if (cB > cA) {
return 1;
} else {
if (index < orderBy.length - 1) {
return compare(orderBy, index + 1, a, b);
} else {
return 0;
}
}
} else {
if (cA < cB) {
return -1;
} else if (cA > cB) {
return 1;
} else {
if (index < orderBy.length - 1) {
return compare(orderBy, index + 1, a, b);
} else {
return 0;
}
}
}
}

function Defaults() {
}

Expand Down Expand Up @@ -184,38 +220,23 @@ defaultsPrototype.defaultFilter = function (collection, resourceName, params, op

// Apply 'orderBy'
if (orderBy) {
DSUtils.forEach(orderBy, function (def) {
var index = 0;
DSUtils.forEach(orderBy, function (def, i) {
if (DSUtils.isString(def)) {
def = [def, 'ASC'];
orderBy[i] = [def, 'ASC'];
} else if (!DSUtils.isArray(def)) {
throw new _this.errors.IllegalArgumentError('DS.filter(resourceName[, params][, options]): ' + DSUtils.toJson(def) + ': Must be a string or an array!', { params: { 'orderBy[i]': { actual: typeof def, expected: 'string|array' } } });
}
filtered = DSUtils.sort(filtered, function (a, b) {
var cA = a[def[0]], cB = b[def[0]];
if (DSUtils.isString(cA)) {
cA = DSUtils.upperCase(cA);
}
if (DSUtils.isString(cB)) {
cB = DSUtils.upperCase(cB);
}
if (def[1] === 'DESC') {
if (cB < cA) {
return -1;
} else if (cB > cA) {
return 1;
} else {
return 0;
}
} else {
if (cA < cB) {
return -1;
} else if (cA > cB) {
return 1;
} else {
return 0;
throw new _this.errors.IllegalArgumentError('DS.filter(resourceName[, params][, options]): ' + DSUtils.toJson(def) + ': Must be a string or an array!', {
params: {
'orderBy[i]': {
actual: typeof def,
expected: 'string|array'
}
}
}
});
});
}
});
filtered = DSUtils.sort(filtered, function (a, b) {
return compare(orderBy, index, a, b);
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/utils.js
Expand Up @@ -248,7 +248,9 @@ module.exports = {
isEmpty: require('mout/lang/isEmpty'),
isRegExp: isRegExp,
toJson: JSON.stringify,
fromJson: JSON.parse,
fromJson: function (json) {
return this.isString(json) ? JSON.parse(json) : json;
},
makePath: require('mout/string/makePath'),
upperCase: require('mout/string/upperCase'),
pascalCase: require('mout/string/pascalCase'),
Expand Down
65 changes: 65 additions & 0 deletions test/both/datastore/sync_methods/filter.test.js
Expand Up @@ -208,6 +208,71 @@ describe('DS#filter', function () {

assert.deepEqual(JSON.stringify(store.filter('post', params)), JSON.stringify([p4, p1, p3, p2]), 'should accept an array of a string and sort in ascending for strings');
});
it('should work with multiple orderBy', function () {
var items = [
{ id: 1, test: 1, test2: 1 },
{ id: 2, test: 2, test2: 2 },
{ id: 3, test: 3, test2: 3 },
{ id: 4, test: 1, test2: 4 },
{ id: 5, test: 2, test2: 5 },
{ id: 6, test: 3, test2: 6 },
{ id: 7, test: 1, test2: 1 },
{ id: 8, test: 2, test2: 2 },
{ id: 9, test: 3, test2: 3 },
{ id: 10, test: 1, test2: 4 },
{ id: 11, test: 2, test2: 5 },
{ id: 12, test: 3, test2: 6 }
];
var params = {};

Post.inject(items);

params.orderBy = [
['test', 'DESC'],
['test2', 'ASC'],
['id', 'ASC']
];

var posts = store.filter('post', params);

assert.deepEqual(JSON.stringify(posts), JSON.stringify([
items[2],
items[8],
items[5],
items[11],
items[1],
items[7],
items[4],
items[10],
items[0],
items[6],
items[3],
items[9]
]));

params.orderBy = [
['test', 'DESC'],
['test2', 'ASC'],
['id', 'DESC']
];

posts = store.filter('post', params);

assert.deepEqual(JSON.stringify(posts), JSON.stringify([
items[8],
items[2],
items[11],
items[5],
items[7],
items[1],
items[10],
items[4],
items[6],
items[0],
items[9],
items[3]
]));
});
it('should correctly apply "skip" predicates', function () {
assert.doesNotThrow(function () {
store.inject('post', p1);
Expand Down

0 comments on commit d936ad9

Please sign in to comment.