Skip to content

Commit

Permalink
Internal methods call fixed (only essential methods registered).
Browse files Browse the repository at this point in the history
  • Loading branch information
jwaliszko committed Sep 2, 2017
1 parent 151612d commit 41d5f6e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/expressive.annotations.validate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* expressive.annotations.validate.js - v2.7.2
/* expressive.annotations.validate.js - v2.7.3
* Client-side component of ExpressiveAnnotations - annotation-based conditional validation library.
* https://github.com/jwaliszko/ExpressiveAnnotations
*
Expand Down Expand Up @@ -137,6 +137,7 @@ var
}
},
initialize: function() {
var that = this;
this.addMethod('Now', function() { // return milliseconds
return Date.now(); // now() is faster than new Date().getTime()
});
Expand Down Expand Up @@ -182,31 +183,31 @@ var
this.addMethod('CompareOrdinalIgnoreCase', function(strA, strB) {
strA = (strA !== null && strA !== undefined) ? strA.toLowerCase() : null;
strB = (strB !== null && strB !== undefined) ? strB.toLowerCase() : null;
return this.CompareOrdinal(strA, strB);
return that.methods.CompareOrdinal(strA, strB);
});
this.addMethod('StartsWith', function(str, prefix) {
return str !== null && str !== undefined && prefix !== null && prefix !== undefined && str.slice(0, prefix.length) === prefix;
});
this.addMethod('StartsWithIgnoreCase', function(str, prefix) {
str = (str !== null && str !== undefined) ? str.toLowerCase() : null;
prefix = (prefix !== null && prefix !== undefined) ? prefix.toLowerCase() : null;
return this.StartsWith(str, prefix);
return that.methods.StartsWith(str, prefix);
});
this.addMethod('EndsWith', function(str, suffix) {
return str !== null && str !== undefined && suffix !== null && suffix !== undefined && str.slice(-suffix.length) === suffix;
});
this.addMethod('EndsWithIgnoreCase', function(str, suffix) {
str = (str !== null && str !== undefined) ? str.toLowerCase() : null;
suffix = (suffix !== null && suffix !== undefined) ? suffix.toLowerCase() : null;
return this.EndsWith(str, suffix);
return that.methods.EndsWith(str, suffix);
});
this.addMethod('Contains', function(str, substr) {
return str !== null && str !== undefined && substr !== null && substr !== undefined && str.indexOf(substr) > -1;
});
this.addMethod('ContainsIgnoreCase', function(str, substr) {
str = (str !== null && str !== undefined) ? str.toLowerCase() : null;
substr = (substr !== null && substr !== undefined) ? substr.toLowerCase() : null;
return this.Contains(str, substr);
return that.methods.Contains(str, substr);
});
this.addMethod('IsNullOrWhiteSpace', function(str) {
return str === null || !/\S/.test(str);
Expand Down Expand Up @@ -294,14 +295,14 @@ var
if (typeHelper.isArray(values)) {
if (values.length === 0)
throw "empty sequence";
sum = this.Sum(values);
sum = that.methods.Sum(values);
return sum / values.length;
}
}
for (i = 0, l = arguments.length; i < l; i++) {
arr.push(arguments[i]);
}
sum = this.Sum(arr);
sum = that.methods.Sum(arr);
return sum / arguments.length;
});
}
Expand Down
24 changes: 24 additions & 0 deletions src/expressive.annotations.validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,30 @@
assert.equal(m.Guid('a1111111-1111-1111-1111-111111111111'), m.Guid('A1111111-1111-1111-1111-111111111111'));
});

qunit.test("internal_methods_are_called_as_expected", function(assert) {
var model = {};
eapriv.toolchain.registerMethods(model, ["CompareOrdinalIgnoreCase", "StartsWithIgnoreCase", "EndsWithIgnoreCase", "ContainsIgnoreCase", "Average"]);

function getMethods(obj) {
var res = [];
for (var m in obj) {
if (typeof obj[m] == "function") {
res.push(m);
}
}
return res;
}

var registered = getMethods(model);
assert.equal(registered.length, 5);
assert.equal(model.CompareOrdinalIgnoreCase('a', 'A'), 0); // internally calls CompareOrdinal
assert.ok(model.StartsWithIgnoreCase(' ab c', ' A')); // internally calls StartsWith
assert.ok(model.EndsWithIgnoreCase(' ab c', ' C')); // internally calls EndsWith
assert.ok(model.ContainsIgnoreCase(' ab c', 'B ')); // internally calls Contains
assert.equal(model.Average([1]), 1); // internally calls Sum
assert.equal(model.Average(1, 2, 3), 2); // internally calls Sum
});

qunit.module("settings");

qunit.test("verify_allowed_settings_setup", function(assert) {
Expand Down

0 comments on commit 41d5f6e

Please sign in to comment.