immutable-class
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -20,2 +20,3 @@ // Generated by dts-bundle v0.4.3 | ||
} | ||
export function generalEqual<T>(a: T, b: T): boolean; | ||
export function immutableEqual<T extends Equalable>(a: T, b: T): boolean; | ||
@@ -22,0 +23,0 @@ export function immutableArraysEqual<T extends Equalable>(arrayA: T[], arrayB: T[]): boolean; |
@@ -17,2 +17,3 @@ export interface Instance<ValueType, JSType> { | ||
} | ||
export declare function generalEqual<T>(a: T, b: T): boolean; | ||
export declare function immutableEqual<T extends Equalable>(a: T, b: T): boolean; | ||
@@ -19,0 +20,0 @@ export declare function immutableArraysEqual<T extends Equalable>(arrayA: T[], arrayB: T[]): boolean; |
@@ -40,2 +40,16 @@ "use strict"; | ||
exports.isImmutableClass = isImmutableClass; | ||
function generalEqual(a, b) { | ||
if (a === b) | ||
return true; | ||
if (a && b) { | ||
if (typeof a.toISOString === 'function' && typeof b.toISOString === 'function') { | ||
return a.valueOf() === b.valueOf(); | ||
} | ||
if (typeof a.equals === 'function') { | ||
return immutableEqual(a, b); | ||
} | ||
} | ||
return false; | ||
} | ||
exports.generalEqual = generalEqual; | ||
function immutableEqual(a, b) { | ||
@@ -42,0 +56,0 @@ if (a === b) |
{ | ||
"name": "immutable-class", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "A template for creating immutable classes", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -74,5 +74,24 @@ /** | ||
/** | ||
* Checks if two things (that might be immutable) are equal (if both null it counts as yes) | ||
* @param a - thing to compare | ||
* @param b - thing to compare | ||
* @returns {boolean} | ||
*/ | ||
export function generalEqual<T>(a: T, b: T): boolean { | ||
if (a === b) return true; | ||
if (a && b) { | ||
if (typeof (a as any).toISOString === 'function' && typeof (b as any).toISOString === 'function') { | ||
return a.valueOf() === b.valueOf(); | ||
} | ||
if (typeof (a as any).equals === 'function') { | ||
return immutableEqual(a as any, b as any); | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Checks if two immutable classes are equal (if both null it counts as yes) | ||
* @param arrayA - array to compare | ||
* @param arrayB - array to compare | ||
* @param a - thing to compare | ||
* @param b - thing to compare | ||
* @returns {boolean} | ||
@@ -79,0 +98,0 @@ */ |
@@ -130,4 +130,36 @@ /// <reference path="../typings/tsd.d.ts" /> | ||
}); | ||
describe("generalEqual", function () { | ||
it("works with basics (strings)", function () { | ||
var tom = 'Tom'; | ||
var bob1 = 'Bob'; | ||
var bob2 = 'Bob'; | ||
chai_1.expect(index_1.generalEqual(null, null)).to.equal(true); | ||
chai_1.expect(index_1.generalEqual(tom, null)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(null, tom)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(tom, bob1)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(bob1, bob2)).to.equal(true); | ||
}); | ||
it("works with basics (Dates)", function () { | ||
var tom = new Date('2016'); | ||
var bob1 = new Date('2015'); | ||
var bob2 = new Date('2015'); | ||
chai_1.expect(index_1.generalEqual(null, null)).to.equal(true); | ||
chai_1.expect(index_1.generalEqual(tom, null)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(null, tom)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(tom, bob1)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(bob1, bob2)).to.equal(true); | ||
}); | ||
it("works immutables", function () { | ||
var tom = new Person('Tom'); | ||
var bob1 = new Person('Bob'); | ||
var bob2 = new Person('Bob'); | ||
chai_1.expect(index_1.generalEqual(null, null)).to.equal(true); | ||
chai_1.expect(index_1.generalEqual(tom, null)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(null, tom)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(tom, bob1)).to.equal(false); | ||
chai_1.expect(index_1.generalEqual(bob1, bob2)).to.equal(true); | ||
}); | ||
}); | ||
describe("immutableEqual", function () { | ||
it("works", function () { | ||
it("works immutables", function () { | ||
var tom = new Person('Tom'); | ||
@@ -134,0 +166,0 @@ var bob1 = new Person('Bob'); |
@@ -5,3 +5,3 @@ /// <reference path="../typings/tsd.d.ts" /> | ||
import { isInstanceOf, isArrayOf, isImmutableClass, immutableEqual, immutableArraysEqual, immutableLookupsEqual } from '../build/index'; | ||
import { isInstanceOf, isArrayOf, isImmutableClass, generalEqual, immutableEqual, immutableArraysEqual, immutableLookupsEqual } from '../build/index'; | ||
@@ -154,4 +154,50 @@ class Animal { | ||
describe("generalEqual", () => { | ||
it("works with basics (strings)", () => { | ||
var tom = 'Tom'; | ||
var bob1 = 'Bob'; | ||
var bob2 = 'Bob'; | ||
expect(generalEqual(null, null)).to.equal(true); | ||
expect(generalEqual(tom, null)).to.equal(false); | ||
expect(generalEqual(null, tom)).to.equal(false); | ||
expect(generalEqual(tom, bob1)).to.equal(false); | ||
expect(generalEqual(bob1, bob2)).to.equal(true); | ||
}); | ||
it("works with basics (Dates)", () => { | ||
var tom = new Date('2016'); | ||
var bob1 = new Date('2015'); | ||
var bob2 = new Date('2015'); | ||
expect(generalEqual(null, null)).to.equal(true); | ||
expect(generalEqual(tom, null)).to.equal(false); | ||
expect(generalEqual(null, tom)).to.equal(false); | ||
expect(generalEqual(tom, bob1)).to.equal(false); | ||
expect(generalEqual(bob1, bob2)).to.equal(true); | ||
}); | ||
it("works immutables", () => { | ||
var tom = new Person('Tom'); | ||
var bob1 = new Person('Bob'); | ||
var bob2 = new Person('Bob'); | ||
expect(generalEqual(null, null)).to.equal(true); | ||
expect(generalEqual(tom, null)).to.equal(false); | ||
expect(generalEqual(null, tom)).to.equal(false); | ||
expect(generalEqual(tom, bob1)).to.equal(false); | ||
expect(generalEqual(bob1, bob2)).to.equal(true); | ||
}); | ||
}); | ||
describe("immutableEqual", () => { | ||
it("works", () => { | ||
it("works immutables", () => { | ||
var tom = new Person('Tom'); | ||
@@ -158,0 +204,0 @@ var bob1 = new Person('Bob'); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74755
1523