From 6980ba0a78e376c2b95e1f24fffa2baea33500af Mon Sep 17 00:00:00 2001 From: segayuu Date: Fri, 30 Aug 2019 12:01:38 +0900 Subject: [PATCH 1/3] fix: Remove old compatbility code --- lib/error.js | 4 ---- lib/types/buffer.js | 27 ++------------------------- 2 files changed, 2 insertions(+), 29 deletions(-) diff --git a/lib/error.js b/lib/error.js index 7ef0706b..abda403f 100644 --- a/lib/error.js +++ b/lib/error.js @@ -11,11 +11,7 @@ class WarehouseError extends Error { constructor(msg, code) { super(msg); - if (Error.captureStackTrace) { Error.captureStackTrace(this); - } else { - this.stack = new Error().stack; - } this.name = 'WarehouseError'; this.code = code; diff --git a/lib/types/buffer.js b/lib/types/buffer.js index 5a91d739..02151711 100644 --- a/lib/types/buffer.js +++ b/lib/types/buffer.js @@ -63,7 +63,7 @@ SchemaTypeBuffer.prototype.validate = function(value_, data) { */ SchemaTypeBuffer.prototype.compare = (a, b) => { if (Buffer.isBuffer(a)) { - return Buffer.isBuffer(b) ? bufferCompare(a, b) : 1; + return Buffer.isBuffer(b) ? a.compare(b) : 1; } return Buffer.isBuffer(b) ? -1 : 0; @@ -101,33 +101,10 @@ SchemaTypeBuffer.prototype.value = function(value, data) { */ SchemaTypeBuffer.prototype.match = (value, query, data) => { if (Buffer.isBuffer(value) && Buffer.isBuffer(query)) { - return bufferEqual(value, query); + return value.equals(query); } return value === query; }; -function bufferCompare(a, b) { - if (typeof a.compare === 'function') return a.compare(b); - - if (a > b) { - return 1; - } else if (a < b) { - return -1; - } - - return 0; -} - -function bufferEqual(a, b) { - if (typeof a.equals === 'function') return a.equals(b); - if (a.length !== b.length) return false; - - for (let i = 0, len = a.length; i < len; i++) { - if (a[i] !== b[i]) return false; - } - - return true; -} - module.exports = SchemaTypeBuffer; From a8036ca992afa18cb7a12ee8f07a5af2d49c9955 Mon Sep 17 00:00:00 2001 From: segayuu Date: Fri, 30 Aug 2019 12:04:10 +0900 Subject: [PATCH 2/3] fix: **Error.name works with prototype fields --- lib/error.js | 4 ++-- lib/error/population.js | 14 ++------------ lib/error/validation.js | 15 ++------------- 3 files changed, 6 insertions(+), 27 deletions(-) diff --git a/lib/error.js b/lib/error.js index abda403f..1e097423 100644 --- a/lib/error.js +++ b/lib/error.js @@ -11,13 +11,13 @@ class WarehouseError extends Error { constructor(msg, code) { super(msg); - Error.captureStackTrace(this); + Error.captureStackTrace(this); - this.name = 'WarehouseError'; this.code = code; } } +WarehouseError.prototype.name = 'WarehouseError'; WarehouseError.ID_EXIST = 'ID_EXIST'; WarehouseError.ID_NOT_EXIST = 'ID_NOT_EXIST'; WarehouseError.ID_UNDEFINED = 'ID_UNDEFINED'; diff --git a/lib/error/population.js b/lib/error/population.js index 4febccc2..6e72e928 100644 --- a/lib/error/population.js +++ b/lib/error/population.js @@ -2,18 +2,8 @@ const WarehouseError = require('../error'); -class PopulationError extends WarehouseError { +class PopulationError extends WarehouseError {} - /** - * PopulationError constructor - * - * @param {string} msg - */ - constructor(msg) { - super(msg); - - this.name = 'PopulationError'; - } -} +PopulationError.prototype.name = 'PopulationError'; module.exports = PopulationError; diff --git a/lib/error/validation.js b/lib/error/validation.js index ab3bdeb9..f3f02efe 100644 --- a/lib/error/validation.js +++ b/lib/error/validation.js @@ -2,19 +2,8 @@ const WarehouseError = require('../error'); -/** - * ValidationError constructor - * - * @class - * @param {String} msg - * @extends WarehouseError - */ -class ValidationError extends WarehouseError { - constructor(msg) { - super(msg); +class ValidationError extends WarehouseError {} - this.name = 'ValidationError'; - } -} +ValidationError.prototype.name = 'ValidationError'; module.exports = ValidationError; From d5a63a8ec36454771e25dec998779eccccde3fc5 Mon Sep 17 00:00:00 2001 From: segayuu Date: Fri, 30 Aug 2019 12:39:05 +0900 Subject: [PATCH 3/3] refactor: Useful Object.assign() --- lib/document.js | 7 +------ lib/model.js | 16 ++-------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/lib/document.js b/lib/document.js index 42a9b1fe..9c0b1eb5 100644 --- a/lib/document.js +++ b/lib/document.js @@ -11,12 +11,7 @@ class Document { */ constructor(data) { if (data) { - const keys = Object.keys(data); - - for (let i = 0, len = keys.length; i < len; i++) { - const key = keys[i]; - this[key] = data[key]; - } + Object.assign(this, data); } } } diff --git a/lib/model.js b/lib/model.js index 03d65413..b11c2e38 100644 --- a/lib/model.js +++ b/lib/model.js @@ -67,22 +67,10 @@ class Model extends EventEmitter { _Query.prototype._schema = schema; // Apply static methods - const statics = schema.statics; - const staticKeys = Object.keys(statics); - - for (let i = 0, len = staticKeys.length; i < len; i++) { - const key = staticKeys[i]; - this[key] = statics[key]; - } + Object.assign(this, schema.statics); // Apply instance methods - const methods = schema.methods; - const methodKeys = Object.keys(methods); - - for (let i = 0, len = methodKeys.length; i < len; i++) { - const key = methodKeys[i]; - _Document.prototype[key] = methods[key]; - } + Object.assign(_Document.prototype, schema.methods); } }