tiny-types
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -75,3 +75,3 @@ import { JSONObject, NonNullJSONPrimitive, Serialisable } from './types'; | ||
*/ | ||
equals(another: TinyType): any; | ||
equals(another: TinyType): boolean; | ||
toString(): string; | ||
@@ -107,11 +107,4 @@ /** | ||
* @returns {JSONValue} | ||
* | ||
* @todo should also serialise arrays | ||
*/ | ||
toJSON(): JSONObject | NonNullJSONPrimitive; | ||
/** | ||
* @access private | ||
* @returns {string[]} names of significant fields that determine the identity of the object | ||
*/ | ||
private significantFields(); | ||
} |
@@ -14,2 +14,3 @@ "use strict"; | ||
var check_1 = require("./check"); | ||
var objects_1 = require("./objects"); | ||
var predicates_1 = require("./predicates"); | ||
@@ -67,2 +68,3 @@ /** | ||
} | ||
// todo: can I make all fields `readonly` without making it a dictionary? | ||
/** | ||
@@ -101,19 +103,7 @@ * @desc Compares two tiny types by value | ||
TinyType.prototype.equals = function (another) { | ||
var _this = this; | ||
if (another === this) { | ||
return true; | ||
} | ||
if (!(another instanceof this.constructor)) { | ||
return false; | ||
} | ||
return this.significantFields().reduce(function (previousFieldsAreEqual, field) { | ||
var currentFieldIsEqual = (_this[field].equals | ||
? _this[field].equals(another[field]) | ||
: _this[field] === another[field]); | ||
return previousFieldsAreEqual && currentFieldIsEqual; | ||
}, true); | ||
return objects_1.equal(this, another); | ||
}; | ||
TinyType.prototype.toString = function () { | ||
var _this = this; | ||
var fields = this.significantFields().reduce(function (acc, field) { | ||
var fields = objects_1.significantFieldsOf(this).reduce(function (acc, field) { | ||
return acc.concat(field + "=" + _this[field]); | ||
@@ -152,4 +142,2 @@ }, []); | ||
* @returns {JSONValue} | ||
* | ||
* @todo should also serialise arrays | ||
*/ | ||
@@ -163,2 +151,4 @@ TinyType.prototype.toJSON = function () { | ||
return value.toJSON(); | ||
case value && Array.isArray(value): | ||
return value.map(function (v) { return toJSON(v); }); | ||
case value && !isPrimitive(value): | ||
@@ -170,3 +160,3 @@ return JSON.stringify(value); | ||
} | ||
var fields = this.significantFields(); | ||
var fields = objects_1.significantFieldsOf(this); | ||
if (fields.length === 1) { | ||
@@ -180,12 +170,2 @@ return toJSON(this[fields[0]]); | ||
}; | ||
/** | ||
* @access private | ||
* @returns {string[]} names of significant fields that determine the identity of the object | ||
*/ | ||
TinyType.prototype.significantFields = function () { | ||
var _this = this; | ||
return Object.getOwnPropertyNames(this) | ||
.filter(function (field) { return typeof _this[field] !== 'function'; }) | ||
.sort(); | ||
}; | ||
return TinyType; | ||
@@ -192,0 +172,0 @@ }()); |
{ | ||
"name": "tiny-types", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "A tiny library that brings Tiny Types to JavaScript and TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
import { check } from './check'; | ||
import { equal, significantFieldsOf } from './objects'; | ||
import { isDefined } from './predicates'; | ||
import { JSONObject, JSONPrimitive, JSONValue, NonNullJSONPrimitive, Serialisable, Serialised } from './types'; | ||
import { JSONObject, JSONValue, NonNullJSONPrimitive, Serialisable, Serialised } from './types'; | ||
@@ -51,2 +52,4 @@ /** | ||
// todo: can I make all fields `readonly` without making it a dictionary? | ||
/** | ||
@@ -85,22 +88,7 @@ * @desc Compares two tiny types by value | ||
equals(another: TinyType) { | ||
if (another === this) { | ||
return true; | ||
} | ||
if (! (another instanceof this.constructor)) { | ||
return false; | ||
} | ||
return this.significantFields().reduce((previousFieldsAreEqual: boolean, field: string) => { | ||
const currentFieldIsEqual = (this[field].equals | ||
? this[field].equals(another[field]) | ||
: this[field] === another[field]); | ||
return previousFieldsAreEqual && currentFieldIsEqual; | ||
}, true); | ||
return equal(this, another); | ||
} | ||
toString() { | ||
const fields = this.significantFields().reduce((acc: string[], field: string) => { | ||
const fields = significantFieldsOf(this).reduce((acc: string[], field: string) => { | ||
return acc.concat(`${field}=${this[field]}`); | ||
@@ -141,4 +129,2 @@ }, []); | ||
* @returns {JSONValue} | ||
* | ||
* @todo should also serialise arrays | ||
*/ | ||
@@ -151,2 +137,4 @@ toJSON(): JSONObject | NonNullJSONPrimitive { | ||
return value.toJSON(); | ||
case value && Array.isArray(value): | ||
return value.map(v => toJSON(v)); | ||
case value && ! isPrimitive(value): | ||
@@ -159,3 +147,3 @@ return JSON.stringify(value); | ||
const fields = this.significantFields(); | ||
const fields = significantFieldsOf(this); | ||
@@ -171,12 +159,2 @@ if (fields.length === 1) { | ||
} | ||
/** | ||
* @access private | ||
* @returns {string[]} names of significant fields that determine the identity of the object | ||
*/ | ||
private significantFields(): string[] { | ||
return Object.getOwnPropertyNames(this) | ||
.filter(field => typeof this[field] !== 'function') | ||
.sort(); | ||
} | ||
} |
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
143336
164
2806