From ec554d3515c410aa4ca7af35ac63bb3af80ae947 Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Fri, 31 Aug 2018 21:22:38 -0700 Subject: [PATCH 01/12] update codeclimate maintainability badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index babb9f3..21c064c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Metamon ====== -[![npm version](https://badge.fury.io/js/metamon.svg)](https://badge.fury.io/js/metamon) [![Build Status](https://travis-ci.org/JustinBeaudry/metamon.svg?branch=master)](https://travis-ci.org/JustinBeaudry/metamon) [![Coverage Status](https://coveralls.io/repos/github/JustinBeaudry/metamon/badge.svg?branch=master)](https://coveralls.io/github/JustinBeaudry/metamon?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/8573b69add63f7c41c66/maintainability)](https://codeclimate.com/github/JustinBeaudry/metamon/maintainability) +[![npm version](https://badge.fury.io/js/metamon.svg)](https://badge.fury.io/js/metamon) [![Build Status](https://travis-ci.org/JustinBeaudry/metamon.svg?branch=master)](https://travis-ci.org/JustinBeaudry/metamon) [![Coverage Status](https://coveralls.io/repos/github/JustinBeaudry/metamon/badge.svg?branch=master)](https://coveralls.io/github/JustinBeaudry/metamon?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/0d74c51f1c973bf7ff83/maintainability)](https://codeclimate.com/github/JustinBeaudry/metamon/maintainability) ![Ditto](https://media.giphy.com/media/uQZTgSuGZMTHG/giphy.gif) From e90f03b22abc5e6e395c7a6d7336f6996111d2dd Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Fri, 31 Aug 2018 21:24:59 -0700 Subject: [PATCH 02/12] remove uuid package --- package-lock.json | 5 ----- package.json | 3 +-- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index aabea30..98ffa5f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11385,11 +11385,6 @@ "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", "dev": true }, - "uuid": { - "version": "3.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", - "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" - }, "v8flags": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/v8flags/-/v8flags-2.1.1.tgz", diff --git a/package.json b/package.json index 7ba5b33..354d9b8 100644 --- a/package.json +++ b/package.json @@ -41,8 +41,7 @@ "dependencies": { "enjoi-browser": "1.x.x", "joi-browser": "13.x.x", - "lodash": "4.x.x", - "uuid": "3.x.x" + "lodash": "4.x.x" }, "devDependencies": { "ava": "^1.0.0-beta.7", From 5cc6824866f1010df4cc68dda2f1f8e104fb119f Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Fri, 31 Aug 2018 21:25:40 -0700 Subject: [PATCH 03/12] update to use new ErrorFactory constructor interface --- src/errors.js | 40 +++------------------------------------- 1 file changed, 3 insertions(+), 37 deletions(-) diff --git a/src/errors.js b/src/errors.js index 959aee7..c075eab 100644 --- a/src/errors.js +++ b/src/errors.js @@ -1,46 +1,12 @@ import ErrorFactory from './ErrorFactory'; -/** - * - * @class - * @ignore - * - */ -class UnsupportedErrorConstructor extends Error { - /** - * - * @desc An Error that is thrown when values that are not supported in an Enum are used - * @param value - */ - constructor(value) { - super(`Unsupported value for enum value: ${value}`); - } -} -/** - * - * @class - * @ignore - * - */ -class MissingIndexErrorConstructor extends Error { - /** - * - * @desc An Error that is thrown when a Model to be added to a Collection is missing the index field - * @param {Model} model - * @param {String} indexBy - */ - constructor(model, indexBy) { - super(`Model ${model.toJSON()} is missing index ${indexBy}`); - } -} /** * * @class UnsupportedError - * @type {Error} */ -export const UnsupportedError = ErrorFactory(null, UnsupportedErrorConstructor); +export const UnsupportedError = ErrorFactory((model, indexBy) => `Model ${model.toJSON()} is missing index ${indexBy}`); /** + * * @class MissingIndexError - * @type {Error} */ -export const MissingIndexError = ErrorFactory(null, MissingIndexErrorConstructor); +export const MissingIndexError = ErrorFactory(value => `Unsupported value for enum value: ${value}`); From 9a8a81f6a6a01dc031d2bf9afd0bcfa45c6a32aa Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Fri, 31 Aug 2018 21:26:18 -0700 Subject: [PATCH 04/12] fixes #7; removes defaults, and imports only pick and omit from lodash --- src/Model.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Model.js b/src/Model.js index 781d5d7..ba753aa 100644 --- a/src/Model.js +++ b/src/Model.js @@ -1,7 +1,5 @@ -import {unix} from './utils'; import Serializable from './Serializable'; -import uuid from 'uuid'; -import _ from 'lodash'; +import {pick, omit} from 'lodash'; import Enjoi from 'enjoi-browser/lib/enjoi'; const _defaults = Symbol('defaults'); @@ -80,9 +78,7 @@ class Model extends Serializable { * @param {Object} data * @param {Object} (options) [{ * defaults: { - * id: {String}, - * modified: {Number} - * created: {Number} + * : <*> * }, * schema: { * : { @@ -99,11 +95,7 @@ class Model extends Serializable { * @throws {Error} */ constructor(data={}, options={ - defaults: { - id: uuid.v4(), - modified: unix(), - created: unix() - }, + defaults: null, schema: null, schemaOptions: null, views: null @@ -197,9 +189,9 @@ class Model extends Serializable { } const view = this[_views][name]; if (view.whitelist) { - return _.pick(this, view.fields); + return pick(this, view.fields); } else { - return _.omit(this, view.fields); + return omit(this, view.fields); } } } From 574fe1e8c3b2197a4bd78b8b7c21d524f9355dfc Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Fri, 31 Aug 2018 21:26:30 -0700 Subject: [PATCH 05/12] adds newline --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index b105a05..b76f7fa 100644 --- a/src/utils.js +++ b/src/utils.js @@ -37,4 +37,4 @@ export function getType(values) { } else { return typeof values; } -} \ No newline at end of file +} From 71e1747177c16286428239f43157a10d9d377a99 Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Fri, 31 Aug 2018 21:27:27 -0700 Subject: [PATCH 06/12] BREAKING CHANGE, updates ErrorFactory to take a string or function as the message creator. Removes ability ot pass custom error constructor and simple inherits from Error --- src/ErrorFactory.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/ErrorFactory.js b/src/ErrorFactory.js index eacdee7..8491cc2 100644 --- a/src/ErrorFactory.js +++ b/src/ErrorFactory.js @@ -3,23 +3,30 @@ * construct a custom error constructor and streamline * the stack trace captured in the custom error * - * @param {String|ErrorConstructor} (message) - * @param {ErrorConstructor} (ErrorClass) + * @param {String|Function} message * @returns {Error} * */ -function ErrorFactory(message, ErrorClass=Error) { - if (typeof message === 'function') { - ErrorClass = message; - } - return class CustomError extends ErrorClass { +function ErrorFactory(message) { + return class CustomError extends Error { constructor(...args) { + let errorMessage; + // if a string is passed the string is the error message if (typeof message === 'string') { - args.unshift(message); + errorMessage = message; + } else if (typeof message === 'function') { + // if a function is passed pass along the values sent to the error constructor + // to allow the construction of more expressive error messages + errorMessage = message(args); + } else { + // otherwise we just pass the first argument passed to the constructor as the error message + errorMessage = args && args[0]; } - super(...args); - if (typeof ErrorClass.captureStackTrace === 'function') { - ErrorClass.captureStackTrace(this, CustomError); + super(errorMessage); + // this ensures that we don't include the ErrorFactory constructor in the stack + // SEE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types + if (typeof Error.captureStackTrace === 'function') { + Error.captureStackTrace(this, CustomError); } } } From f28c642afd05e9b617ae29fc3215a456f3205a7b Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Fri, 31 Aug 2018 21:28:32 -0700 Subject: [PATCH 07/12] BREAKING CHANGE, does not require a Model any longer and will lazily use anyobject as its model, will conditionally check if a model is set before trying to force into model type --- src/Collection.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Collection.js b/src/Collection.js index d9aa90a..5c4eb09 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -3,6 +3,7 @@ import Serializable from './Serializable'; import Model from './Model'; import {MissingIndexError} from './errors'; import {toJSON, getType} from './utils'; + /** * * @extends Serializable @@ -33,15 +34,12 @@ class Collection extends Serializable { * assert(pokemonCollection.get(name).quote, quote) // true * * @param {Object|Array} data - data to create a collection with on construction. data can be passed after instantiation - * @param {Model} model - the Model to be used when adding data to a collection. A Collection will force all data into this Model. + * @param {Model} (model) - the Model to be used when adding data to a collection. A Collection will force all data into this Model if used. * @param {String} (indexBy) [id] - the field that the collection should index on * @throws {Error} */ constructor(data, model, indexBy='id') { super(); - if (!model || !(model.prototype instanceof Model)) { - throw new Error('A Collection requires a Model, and the Model must inherit from the Model Base Class.'); - } this.index = {}; this.indexBy = indexBy; this.Model = model; @@ -246,9 +244,12 @@ function setFromObject(datum) { * * @ignore * @param {Object|Model} model - * @returns {Model} + * @returns {Model|Object} */ function convertToModel(model) { + if (!this.Model) { + return model; + } if (!(model instanceof this.Model)) { model = new this.Model(model); } From d9e3cdfb918daa2c232843db0a3cd724337cb1e2 Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Sat, 1 Sep 2018 13:17:38 -0700 Subject: [PATCH 08/12] adds onFieldUpdate, onFieldsUpdate to utils/model, and adds onModelUpdate, and onCollectionUpdate to Collection --- package-lock.json | 19 +++++++++++ package.json | 3 +- src/Collection.js | 64 ++++++++++++++++++++++++++++++++------ src/Model.js | 34 ++++++++++++++++++-- src/errors.js | 26 ++++++++++++++++ src/utils.js | 40 ++++++++++++++++++++++++ tests/collection.spec.js | 3 -- tests/errorfactory.spec.js | 30 ++++-------------- tests/model.spec.js | 13 -------- 9 files changed, 179 insertions(+), 53 deletions(-) diff --git a/package-lock.json b/package-lock.json index 98ffa5f..e58688e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10021,6 +10021,14 @@ "rx-lite": "*" } }, + "rxjs": { + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.3.1.tgz", + "integrity": "sha512-hRVfb1Mcf8rLXq1AZEjYpzBnQbO7Duveu1APXkWRTvqzhmkoQ40Pl2F9Btacx+gJCOqsMiugCGG4I2HPQgJRtA==", + "requires": { + "tslib": "^1.9.0" + } + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -11109,6 +11117,11 @@ "integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=", "dev": true }, + "tslib": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", + "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==" + }, "tunnel-agent": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", @@ -11385,6 +11398,12 @@ "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", "dev": true }, + "uuid": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", + "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==", + "dev": true + }, "v8flags": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/v8flags/-/v8flags-2.1.1.tgz", diff --git a/package.json b/package.json index 354d9b8..4c122e5 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ "dependencies": { "enjoi-browser": "1.x.x", "joi-browser": "13.x.x", - "lodash": "4.x.x" + "lodash": "4.x.x", + "rxjs": "^6.3.1" }, "devDependencies": { "ava": "^1.0.0-beta.7", diff --git a/src/Collection.js b/src/Collection.js index 5c4eb09..d30c351 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -1,8 +1,10 @@ import _ from 'lodash'; +import {throwError} from 'rxjs'; +import {merge} from 'rxjs/operators'; import Serializable from './Serializable'; import Model from './Model'; -import {MissingIndexError} from './errors'; -import {toJSON, getType} from './utils'; +import {MissingFieldError, MissingIndexError, MissingModelError, ModelExistsError, UnexpectedTypeError} from './errors'; +import {toJSON, getType, onFieldsUpdate} from './utils'; /** * @@ -34,7 +36,8 @@ class Collection extends Serializable { * assert(pokemonCollection.get(name).quote, quote) // true * * @param {Object|Array} data - data to create a collection with on construction. data can be passed after instantiation - * @param {Model} (model) - the Model to be used when adding data to a collection. A Collection will force all data into this Model if used. + * @param {Model|Function} (model) - The Model to be used when adding data to a collection. + * A Collection will force all data into this Model if used. * @param {String} (indexBy) [id] - the field that the collection should index on * @throws {Error} */ @@ -85,7 +88,7 @@ class Collection extends Serializable { setFromObject.call(this, data); break; default: - throw new TypeError(`Expected data to be of type {Object|Array} and got ${typeof data}`); + throw new UnexpectedTypeError(); } return this; } @@ -108,7 +111,7 @@ class Collection extends Serializable { let convertedModel = convertToModel.call(this, model); const key = convertedModel[this.indexBy]; if (this.index[key]) { - throw new Error('Model already exists in Collection index. Call update if you wish to update model'); + throw new ModelExistsError(); } this.index[key] = convertedModel; return this; @@ -129,7 +132,7 @@ class Collection extends Serializable { remove(modelId) { let model = this.get(modelId); if (!model) { - throw new Error('Model does not exist in Collection index. Call add().'); + throw new MissingModelError(); } let convertedModel = convertToModel.call(this, model); const key = convertedModel[this.indexBy]; @@ -156,10 +159,13 @@ class Collection extends Serializable { update(model) { let convertedModel = convertToModel.call(this, model); const key = convertedModel[this.indexBy]; - if (!this.index[key]) { - throw new Error('Model to update does not exist in Collection index'); + if (!key || !this.get(key)) { + throw new MissingIndexError(); } - this.index[key] = convertedModel; + // if we have a metamon Model instance, use `model.set()` so we can validate the schema + this.index[key] = (convertedModel instanceof Model) + ? this.get(key).set(model) + : convertedModel; return this; } /** @@ -221,11 +227,49 @@ class Collection extends Serializable { * * converts index to an array and stringifies * + * @param {Boolean} asIndex [false] - return the index as JSON * @returns {String} */ - toJSON() { + toJSON(asIndex=false) { + if (asIndex) { + return toJSON(this.index); + } return toJSON(this.toArray()); } + onCollectionUpdate() { + return merge( + this.toArray().map(model => { + if (model instanceof Model) { + return model.onFieldsUpdate().pipe( + map(value => { + return { + [model[this.indexBy]]: value + } + }) + ) + } + return onFieldsUpdate + .call(model, Object.keys(model)) + .pipe(map(value => { + return { + [model[this.indexBy]]: value + } + })) + }) + ); + } + onModelUpdate(id) { + const model = this.get(id); + if (!model) { + return throwError(new MissingModelError()); + } + // if the model is an instance of model, just use the Model method + if (model instanceof Model) { + return model.onFieldsUpdate(); + } + // if the model is a plain object just use onFieldsUpdate util + return onFieldsUpdate.call(model, Object.keys(model)); + } } /** * diff --git a/src/Model.js b/src/Model.js index ba753aa..189e924 100644 --- a/src/Model.js +++ b/src/Model.js @@ -1,6 +1,15 @@ -import Serializable from './Serializable'; import {pick, omit} from 'lodash'; import Enjoi from 'enjoi-browser/lib/enjoi'; +import Serializable from './Serializable'; +import {onFieldsUpdate, onFieldUpdate} from './utils'; + +const MODEL_METHODS = [ + 'set', + 'addView', + 'getView', + 'onFieldUpdate', + 'onFieldsUpdate' +]; const _defaults = Symbol('defaults'); const _schema = Symbol('schema'); @@ -127,7 +136,7 @@ class Model extends Serializable { } }); } - this.set(data); + return this.set(data); } /** * @@ -153,6 +162,7 @@ class Model extends Serializable { Object.assign(this, this[_defaults], data); } Object.assign(this, data); + return this; } /** * @@ -175,6 +185,7 @@ class Model extends Serializable { whitelist: isWhitelist, fields: fields }; + return this; } /** * @@ -194,5 +205,24 @@ class Model extends Serializable { return omit(this, view.fields); } } + /** + * + * @param {String} field + * @returns {Observable} + */ + onFieldUpdate(field) { + onFieldUpdate.call(this, field); + } + /** + * + * @param {Array} (fields) - defaults to all fields on model + * @returns {Observable} + */ + onFieldsUpdate(fields) { + if (!fields) { + fields = Object.keys(omit(this.toObject(), MODEL_METHODS)); + } + onFieldsUpdate.call(this, fields); + } } export default Model; diff --git a/src/errors.js b/src/errors.js index c075eab..a07b9e7 100644 --- a/src/errors.js +++ b/src/errors.js @@ -1,12 +1,38 @@ import ErrorFactory from './ErrorFactory'; +/** + * + * @class UnexpectedTypeError + * @type {Error|Function} + */ +export const UnexpectedTypeError = ErrorFactory(data => `Expected data to be of type {Object|Array} and got ${typeof data}`); /** * * @class UnsupportedError + * @type {Error|Function} */ export const UnsupportedError = ErrorFactory((model, indexBy) => `Model ${model.toJSON()} is missing index ${indexBy}`); /** * * @class MissingIndexError + * @type {Error|Function} */ export const MissingIndexError = ErrorFactory(value => `Unsupported value for enum value: ${value}`); +/** + * + * @class MissingModelError + * @type {Error|Function} + */ +export const MissingModelError = ErrorFactory('Model does not exist in collection. call add() to add a model to the index.'); +/** + * + * @class ModelExistsError + * @type {Error|Function} + */ +export const ModelExistsError = ErrorFactory('Model already exists in collection index. call update() if you wish to update the model'); +/** + * + * @class MissingFieldError + * @type {Error|Function} + */ +export const MissingFieldError = ErrorFactory(field => `Requested field ${field} does not exist on model`); diff --git a/src/utils.js b/src/utils.js index b76f7fa..03926f3 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,7 @@ +import {Observable} from 'rxjs'; +import {map, merge} from 'rxjs/operators'; import _ from 'lodash'; +import {MissingFieldError} from './errors'; /** * * returns the current time as a unix timestamp @@ -38,3 +41,40 @@ export function getType(values) { return typeof values; } } +/** + * + * @note Must be bound to be used + * + * @param {String} field + * @returns {Observable} + */ +export function onFieldUpdate(field) { + return new Observable(observer => { + if (!this[field]) { + observer.error(new MissingFieldError()); + } + const revoke = Proxy.revocable(this[field], { + set: value => { + observer.next(value); + this[field] = value; + } + }); + return () => revoke(); + }); +} + +/** + * + * @param {Array} fields + * @returns {MonoTypeOperatorFunction | OperatorFunction} + */ +export function onFieldsUpdate(fields) { + return merge( + fields.map(field => { + return this.onFieldUpdate.call(this, field).pipe(map(value => { + return { + [field]: value + } + })); + })); +} diff --git a/tests/collection.spec.js b/tests/collection.spec.js index 208f40a..e080c74 100644 --- a/tests/collection.spec.js +++ b/tests/collection.spec.js @@ -11,9 +11,6 @@ test.beforeEach(t => { t.context.field3 = 'field3'; }); -test('should throw an error if no model is supplied', t => { - t.throws(() => new Collection()); -}); test('should throw an MissingIndexError if an Object TestModel is missing the indexed field', t => { t.throws(() => new Collection({}, TestModel, 'name')); }); diff --git a/tests/errorfactory.spec.js b/tests/errorfactory.spec.js index bbaf8da..5cd70d4 100644 --- a/tests/errorfactory.spec.js +++ b/tests/errorfactory.spec.js @@ -1,21 +1,7 @@ import test from 'ava'; -import sinon from 'sinon'; import faker from 'faker'; import ErrorFactory from '../src/ErrorFactory'; -const captureSpy = sinon.spy(); - -class CaptureStackError extends Error { - constructor() { - super(); - } - static captureStackTrace() { - return captureSpy(); - } -} - -class NoCaptureStackError {} - test('ErrorFactory() should return an Error Constructor', t => { const text = faker.random.word(); const ErrorConstructor = ErrorFactory(); @@ -26,7 +12,7 @@ test('ErrorFactory() should return an Error Constructor', t => { t.is(CustomError.message, text); }); -test('ErrorFactory() should have a baked error message if passed into its constructor', t => { +test('ErrorFactory() should have a baked error message if string message passed into its constructor', t => { const text = faker.random.word(); const ErrorConstructor = ErrorFactory(text); const CustomError = new ErrorConstructor(); @@ -35,13 +21,9 @@ test('ErrorFactory() should have a baked error message if passed into its constr t.is(CustomError.message, text); }); -test('ErrorFactory() should call captureStackTrace and capture the constructors stack frames', t => { - const ErrorConstructor = ErrorFactory(null, CaptureStackError); - new ErrorConstructor(); - t.true(captureSpy.called); +test('ErrorFactory() should have a baked error message if function message passed into its constructor', t => { + const text = faker.random.word(); + const ErrorConstructor = ErrorFactory(value => `${value}`); + const CustomError = new ErrorConstructor(text);`` + t.is(CustomError.message, text); }); - -test('ErrorFactory() should not call captureStackTrace if not present on constructor', t => { - const ErrorConstructor = ErrorFactory(null, NoCaptureStackError); - t.not(typeof ErrorConstructor.captureStackTrace, 'function'); -}); \ No newline at end of file diff --git a/tests/model.spec.js b/tests/model.spec.js index 2706be2..4694a08 100644 --- a/tests/model.spec.js +++ b/tests/model.spec.js @@ -21,19 +21,6 @@ test('should be constructable', t => { t.falsy(err); t.true(m instanceof Model); }); -test('should be constructable with default defaults', t => { - const {field1, field2, model} = t.context; - let {m, err} = model({ - field1, - field2 - }); - t.falsy(err); - t.true(typeof m.id === 'string'); - t.true(typeof m.modified === 'number'); - t.true(typeof m.created === 'number'); - t.true(m.field1 === field1); - t.true(m.field2 === field2); -}); test('should be constructable with custom defaults', t => { const _id = '_id'; const _created = new Date(); From 8675d66479b85669bb61c708b6a06236117d4db4 Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Sat, 1 Sep 2018 13:20:06 -0700 Subject: [PATCH 09/12] run tests before building inn prepublish --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4c122e5..33de646 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "build-browser": "rm -rf dist && npx rollup -c", "generate-docs": "rm -rf docs && npx jsdoc -c ./.jsdoc.json", "serve-docs": "cd docs && python -m SimpleHTTPServer", - "prepublishOnly": "npm run build-prod; npm run generate-docs;npm test" + "prepublishOnly": "npm test;npm run build-prod; npm run generate-docs" }, "keywords": [ "joi", From a3e7ee5a12b0e061639daa69042385fb9a2582b2 Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Sat, 1 Sep 2018 13:22:30 -0700 Subject: [PATCH 10/12] fixes a type in error constructors and throws MissingModelError instead of MissingIndexError --- src/Collection.js | 2 +- src/errors.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Collection.js b/src/Collection.js index d30c351..5236931 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -160,7 +160,7 @@ class Collection extends Serializable { let convertedModel = convertToModel.call(this, model); const key = convertedModel[this.indexBy]; if (!key || !this.get(key)) { - throw new MissingIndexError(); + throw new MissingModelError(); } // if we have a metamon Model instance, use `model.set()` so we can validate the schema this.index[key] = (convertedModel instanceof Model) diff --git a/src/errors.js b/src/errors.js index a07b9e7..9b887c6 100644 --- a/src/errors.js +++ b/src/errors.js @@ -11,13 +11,13 @@ export const UnexpectedTypeError = ErrorFactory(data => `Expected data to be of * @class UnsupportedError * @type {Error|Function} */ -export const UnsupportedError = ErrorFactory((model, indexBy) => `Model ${model.toJSON()} is missing index ${indexBy}`); +export const UnsupportedError = ErrorFactory(value => `Unsupported value for enum value: ${value}`); /** * * @class MissingIndexError * @type {Error|Function} */ -export const MissingIndexError = ErrorFactory(value => `Unsupported value for enum value: ${value}`); +export const MissingIndexError = ErrorFactory((model, indexBy) => `Model ${model.toJSON()} is missing index ${indexBy}`); /** * * @class MissingModelError From 14fe94bcfabe557346e9dca03c1ceab5b6bbca5f Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Sat, 1 Sep 2018 13:24:41 -0700 Subject: [PATCH 11/12] 3.0.0-0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index e58688e..2feeb0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "metamon", - "version": "2.6.2", + "version": "3.0.0-0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 33de646..2216b30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "metamon", - "version": "2.6.2", + "version": "3.0.0-0", "description": "model, and collection library for class based or pojo models", "main": "index.js", "browser": "dist/metamon.min.js", From 9d626f032e4aa0d12f4e68a1d044eeb780cb11fc Mon Sep 17 00:00:00 2001 From: Justin Beaudry Date: Sat, 1 Sep 2018 13:25:36 -0700 Subject: [PATCH 12/12] use x.x.x syntax for rxjs --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2216b30..e0c3613 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "enjoi-browser": "1.x.x", "joi-browser": "13.x.x", "lodash": "4.x.x", - "rxjs": "^6.3.1" + "rxjs": "6.x.x" }, "devDependencies": { "ava": "^1.0.0-beta.7",