Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove old compatibility code #57

Merged
merged 3 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ class WarehouseError extends Error {
constructor(msg, code) {
super(msg);

if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
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';
Expand Down
14 changes: 2 additions & 12 deletions lib/error/population.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
15 changes: 2 additions & 13 deletions lib/error/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
16 changes: 2 additions & 14 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
27 changes: 2 additions & 25 deletions lib/types/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;