Change sortedBy API as discussed

If provided an array, then each member can optionally be an array of [propName, reverse]
This commit is contained in:
Scott Kyle 2016-02-18 12:47:18 -08:00
parent 76865d0da4
commit 05d84b23a2
4 changed files with 35 additions and 34 deletions

View File

@ -16,7 +16,7 @@ class Results {}
util.createMethods(Results.prototype, constants.objectTypes.RESULTS, [
'filtered',
'snapshot',
'sortedBy',
'sorted',
]);
function create(realmId, info) {

View File

@ -95,25 +95,39 @@ JSValueRef ResultsSorted(JSContextRef ctx, JSObjectRef function, JSObjectRef thi
try {
Results *results = RJSGetInternal<Results *>(thisObject);
RJSValidateArgumentRange(argumentCount, 1, 2);
size_t prop_count;
std::vector<std::string> prop_names;
std::vector<bool> ascending;
if (RJSIsValueArray(ctx, arguments[0])) {
RJSValidateArgumentCount(argumentCount, 1);
JSObjectRef js_prop_names = RJSValidatedValueToObject(ctx, arguments[0]);
prop_count = RJSValidatedListLength(ctx, js_prop_names);
prop_names.resize(prop_count);
ascending.resize(prop_count);
for (unsigned int i = 0; i < prop_count; i++) {
prop_names[i] = RJSValidatedStringForValue(ctx, RJSValidatedPropertyAtIndex(ctx, js_prop_names, i));
JSValueRef val = RJSValidatedPropertyAtIndex(ctx, js_prop_names, i);
if (RJSIsValueArray(ctx, val)) {
prop_names[i] = RJSValidatedStringForValue(ctx, RJSValidatedPropertyAtIndex(ctx, (JSObjectRef)val, 0));
ascending[i] = !JSValueToBoolean(ctx, RJSValidatedPropertyAtIndex(ctx, (JSObjectRef)val, 1));
}
else {
prop_names[i] = RJSValidatedStringForValue(ctx, val);
ascending[i] = true;
}
}
}
else {
prop_count = 1;
prop_names.push_back(RJSValidatedStringForValue(ctx, arguments[0]));
ascending.push_back(argumentCount == 1 ? true : !JSValueToBoolean(ctx, arguments[1]));
}
std::vector<size_t> columns(prop_count);
std::vector<bool> ascending(prop_count, true);
size_t index = 0;
for (std::string prop_name : prop_names) {
@ -124,20 +138,6 @@ JSValueRef ResultsSorted(JSContextRef ctx, JSObjectRef function, JSObjectRef thi
columns[index++] = prop->table_column;
}
if (argumentCount == 2) {
if (RJSIsValueArray(ctx, arguments[1])) {
JSObjectRef js_reverse = RJSValidatedValueToObject(ctx, arguments[1]);
size_t js_reverse_count = std::min(RJSValidatedListLength(ctx, js_reverse), prop_count);
for (unsigned int i = 0; i < js_reverse_count; i++) {
ascending[i] = !JSValueToBoolean(ctx, RJSValidatedPropertyAtIndex(ctx, js_reverse, i));
}
}
else if (JSValueToBoolean(ctx, arguments[1])) {
ascending.assign(prop_count, false);
}
}
results = new Results(results->sort({std::move(columns), std::move(ascending)}));
return RJSWrapObject<Results *>(ctx, RJSResultsClass(), results);
}
@ -213,7 +213,7 @@ JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, const ObjectSc
static const JSStaticFunction RJSResultsFuncs[] = {
{"snapshot", ResultsStaticCopy, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"sortedBy", ResultsSorted, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"sorted", ResultsSorted, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"filtered", ResultsFiltered, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL},
};

View File

@ -153,44 +153,47 @@ module.exports = BaseTest.extend({
});
};
objects = objects.sortedBy('primaryCol');
objects = objects.sorted('primaryCol');
TestCase.assertArraysEqual(primaries(objects), [0, 1, 2, 3, 4]);
objects = objects.sortedBy('primaryCol', true);
objects = objects.sorted('primaryCol', true);
TestCase.assertArraysEqual(primaries(objects), [4, 3, 2, 1, 0]);
objects = objects.sortedBy(['primaryCol', 'valueCol']);
objects = objects.sorted(['primaryCol', 'valueCol']);
TestCase.assertArraysEqual(primaries(objects), [0, 1, 2, 3, 4]);
objects = objects.sortedBy(['primaryCol', 'valueCol'], true);
objects = objects.sorted([['primaryCol', true], ['valueCol', true]]);
TestCase.assertArraysEqual(primaries(objects), [4, 3, 2, 1, 0]);
objects = objects.sortedBy(['primaryCol', 'valueCol'], [false]);
objects = objects.sorted([['primaryCol', false], 'valueCol']);
TestCase.assertArraysEqual(primaries(objects), [0, 1, 2, 3, 4]);
objects = objects.sortedBy(['valueCol', 'primaryCol']);
objects = objects.sorted(['valueCol', 'primaryCol']);
TestCase.assertArraysEqual(primaries(objects), [2, 3, 1, 0, 4]);
objects = objects.sortedBy(['valueCol', 'primaryCol'], [false, true]);
objects = objects.sorted([['valueCol', false], ['primaryCol', true]]);
TestCase.assertArraysEqual(primaries(objects), [3, 2, 1, 4, 0]);
objects = objects.sortedBy(['valueCol', 'primaryCol'], [true, false]);
objects = objects.sorted([['valueCol', true], ['primaryCol', false]]);
TestCase.assertArraysEqual(primaries(objects), [0, 4, 1, 2, 3]);
objects = objects.sortedBy(['valueCol', 'primaryCol'], true);
objects = objects.sorted([['valueCol', true], ['primaryCol', true]]);
TestCase.assertArraysEqual(primaries(objects), [4, 0, 1, 3, 2]);
TestCase.assertThrows(function() {
objects.sortedBy(1);
objects.sorted(1);
});
TestCase.assertThrows(function() {
objects.sortedBy([1]);
objects.sorted([1]);
});
TestCase.assertThrows(function() {
objects.sortedBy('fish');
objects.sorted('fish');
});
TestCase.assertThrows(function() {
objects.sortedBy(['valueCol', 'fish']);
objects.sorted(['valueCol', 'fish']);
});
TestCase.assertThrows(function() {
objects.sorted(['valueCol', 'primaryCol'], true);
});
},
});

View File

@ -41,9 +41,7 @@ module.exports = {
testDataSource() {
let realm = createRealm();
let objects = realm.objects('UniqueObject');
objects.sortByProperty('id');
let objects = realm.objects('UniqueObject').sorted('id');
let dataSource = createDataSource().cloneWithRows(objects);
let count = objects.length;