Permalink
Browse files

EJSON: Provide default implementation of equals for custom types

  • Loading branch information...
1 parent 0be15bd commit de294ddb1984f76278da86947b35525291fc5298 @marcandre marcandre committed with glasser Jan 4, 2014
Showing with 19 additions and 19 deletions.
  1. +12 −10 docs/client/api.html
  2. +0 −8 packages/ejson/custom_models_for_tests.js
  3. +7 −1 packages/ejson/ejson.js
View
@@ -2943,16 +2943,6 @@ <h2 id="ejson"><span>EJSON</span></h2>
Instances of your type should implement the following interface:
-{{> api_box ejsonTypeEquals}}
-
-The `equals` method should define an [equivalence
-relation](http://en.wikipedia.org/wiki/Equivalence_relation). It should have
-the following properties:
-
- - *Reflexivity* - for any instance `a`: `a.equals(a)` must be true.
- - *Symmetry* - for any two instances `a` and `b`: `a.equals(b)` if and only if `b.equals(a)`.
- - *Transitivity* - for any three instances `a`, `b`, and `c`: `a.equals(b)` and `b.equals(c)` implies `a.equals(c)`.
-
{{> api_box ejsonTypeName}}
{{> api_box ejsonTypeToJSONValue}}
@@ -2967,6 +2957,18 @@ <h2 id="ejson"><span>EJSON</span></h2>
{{> api_box ejsonTypeClone}}
+By default, `EJSON.equals` will use `toJSONValue` to compare instances of your types, but you may implement the following interface instead:
+
+{{> api_box ejsonTypeEquals}}
+
+The `equals` method should define an [equivalence
+relation](http://en.wikipedia.org/wiki/Equivalence_relation). It should have
+the following properties:
+
+ - *Reflexivity* - for any instance `a`: `a.equals(a)` must be true.
+ - *Symmetry* - for any two instances `a` and `b`: `a.equals(b)` if and only if `b.equals(a)`.
+ - *Transitivity* - for any three instances `a`, `b`, and `c`: `a.equals(b)` and `b.equals(c)` implies `a.equals(c)`.
+
<h2 id="http"><span>HTTP</span></h2>
`HTTP` provides an HTTP request API on the client and server. To use
@@ -6,10 +6,6 @@ function Address (city, state) {
Address.prototype = {
constructor: Address,
- equals: function (other) {
- return EJSON.stringify(this) == EJSON.stringify(other);
- },
-
typeName: function () {
return "Address";
},
@@ -35,10 +31,6 @@ function Person (name, dob, address) {
Person.prototype = {
constructor: Person,
- equals: function (other) {
- return EJSON.stringify(this) == EJSON.stringify(other);
- },
-
typeName: function () {
return "Person";
},
@@ -6,13 +6,14 @@ var customTypes = {};
// from a basic JSON-able representation. The factory argument
// is a function of JSON-able --> your object
// The type you add must have:
-// - A equals() method, so that Meteor can compare it
// - A toJSONValue() method, so that Meteor can serialize it
// - a typeName() method, to show how to look it up in our type table.
// It is okay if these methods are monkey-patched on.
// EJSON.clone will use toJSONValue and the given factory to produce
// a clone, but you may specify a method clone() that will be
// used instead.
+// Similarly, EJSON.equals will use toJSONValue to make comparisons,
+// but you may provide a method equals() instead.
//
EJSON.addType = function (name, factory) {
if (_.has(customTypes, name))
@@ -304,6 +305,11 @@ EJSON.equals = function (a, b, options) {
}
return true;
}
+ // fallback for custom types that don't implement their own equals
+ switch (EJSON._isCustomType(a) + EJSON._isCustomType(b)) {
+ case 1: return false;
+ case 2: return EJSON.equals(EJSON.toJSONValue(a), EJSON.toJSONValue(b));
+ }
// fall back to structural equality of objects
var ret;
if (keyOrderSensitive) {

0 comments on commit de294dd

Please sign in to comment.