Add ducktyping Change.isChange method

Similar to Filter.isFilter, this should help mitigate instanceof issues.
This commit is contained in:
Patrick Mooney 2014-06-24 14:14:17 -05:00
parent a0fd56d7b3
commit 1d2cf79bff
1 changed files with 13 additions and 1 deletions

View File

@ -86,9 +86,21 @@ function Change(options) {
}
module.exports = Change;
Change.isChange = function isChange(change) {
if (!change || typeof (change) !== 'object') {
return false;
}
if ((change instanceof Change) ||
((typeof (change.toBer) === 'function') &&
(change.modification !== undefined) &&
(change.operation !== undefined))) {
return true;
}
return false;
};
Change.compare = function (a, b) {
if (!(a instanceof Change) || !(b instanceof Change))
if (!Change.isChange(a) || !Change.isChange(b))
throw new TypeError('can only compare Changes');
if (a.operation < b.operation)