Преглед изворни кода

Refs #2792 Fixed and issue with $substr where -1 behavior was not
consistent with mongo

Adam Bell пре 12 година
родитељ
комит
547d102f6c

+ 2 - 0
lib/pipeline/expressions/SubstrExpression.js

@@ -39,5 +39,7 @@ proto.evaluate = function evaluate(doc) {
 	if (typeof(idx) != "number") throw new Error(this.getOpName() + ": starting index must be a numeric type; code 16034");
 	if (typeof(len) != "number") throw new Error(this.getOpName() + ": length must be a numeric type; code 16035");
 	if (idx >= str.length) return "";
+	//TODO: Need to handle -1
+	len = (len === -1 ? undefined : len);
 	return str.substr(idx, len);
 };

+ 10 - 1
test/lib/pipeline/expressions/SubstrExpression.js

@@ -70,6 +70,14 @@ module.exports = {
 				assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "");
 			},
 
+			"Should return the entire string if end is -1": function testIndexTooLarge(){
+				var s = "mystring",
+				start = 0,
+				end = -1;
+				assert.strictEqual(Expression.parseOperand({$substr:["$s", "$start", "$end"]}).evaluate({s:s, start:start, end:end}), "mystring");
+			},
+
+
 			"Should fail if end is before begin": function testUnorderedIndexes(){
 				var s = "mystring",
 				start = s.length,
@@ -82,7 +90,8 @@ module.exports = {
 				start = 0,
 				end = s.length+1;
 				assert.throws(function(){Expression.parseOperand({$substr:["$s", "$start"]}).evaluate({s:s, start:start, end:end});});
-			}
+			},
+
 
 		}