Comparing version
245
node/core.js
@@ -13,3 +13,3 @@ 'use strict'; | ||
var _get = function get(_x10, _x11, _x12) { var _again = true; _function: while (_again) { var object = _x10, property = _x11, receiver = _x12; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x10 = parent; _x11 = property; _x12 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
var _get = function get(_x9, _x10, _x11) { var _again = true; _function: while (_again) { var object = _x9, property = _x10, receiver = _x11; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x9 = parent; _x10 = property; _x11 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
@@ -36,6 +36,2 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _cuid = require('cuid'); | ||
var _cuid2 = _interopRequireDefault(_cuid); | ||
var _bid = require('./bid'); | ||
@@ -55,4 +51,15 @@ | ||
var noop = function noop() {}; | ||
var omit = ['authorization', '_events', 'bids', 'outBid', 'bestBid', 'started', 'ended', 'state']; | ||
var READ_ONLY_FIELDS = { | ||
authorization: true, | ||
auctionStatus: true, | ||
currentPrice: true, | ||
_events: true, | ||
outBid: true, | ||
bestBid: true, | ||
started: true, | ||
ended: true, | ||
bids: true | ||
}; | ||
var _default = (function (_Emitter) { | ||
@@ -62,3 +69,3 @@ _inherits(_default, _Emitter); | ||
/** | ||
* Initialize `order` object. | ||
* Initialize `auction` object. | ||
* | ||
@@ -71,5 +78,4 @@ * @param {Object} options | ||
function _default() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var fn = arguments.length <= 1 || arguments[1] === undefined ? noop : arguments[1]; | ||
function _default(data, fn) { | ||
if (data === undefined) data = {}; | ||
@@ -79,68 +85,187 @@ _classCallCheck(this, _default); | ||
_get(Object.getPrototypeOf(_default.prototype), 'constructor', this).call(this); | ||
this.writable = (0, _predefine2['default'])(this, _predefine2['default'].WRITABLE); | ||
this.readable = (0, _predefine2['default'])(this, _predefine2['default'].READABLE); | ||
this.predefineProperties(); | ||
var error = this.check(data); | ||
if (error) { | ||
if (!this.listeners('error', true) && !fn) throw error; | ||
if (fn) setImmediate(fn.bind(null, error)); | ||
this.emit('error', error); | ||
return this; | ||
} | ||
this.id = data.id; | ||
this.reset(); | ||
this.mergeProperties(data); | ||
debug('auction initialized %j', this.data); | ||
if (fn) setImmediate(fn.bind(null, null, this.data)); | ||
} | ||
var data = null; | ||
var error = null; | ||
var writable = (0, _predefine2['default'])(this, _predefine2['default'].WRITABLE); | ||
var readable = (0, _predefine2['default'])(this, _predefine2['default'].READABLE); | ||
/** | ||
* Set predefined properties. | ||
* | ||
* @api private | ||
*/ | ||
writable('_events', {}); | ||
_createClass(_default, [{ | ||
key: 'predefineProperties', | ||
value: function predefineProperties() { | ||
this.writable('_events', {}); | ||
} | ||
if (error = this.check(options)) { | ||
if (!fn) throw error; | ||
return setImmediate(function () { | ||
fn(error); | ||
}); | ||
/** | ||
* Merge 'auction' properties. | ||
* | ||
* @param {Object} data | ||
* @type {Auction} this | ||
* @api private | ||
*/ | ||
}, { | ||
key: 'mergeProperties', | ||
value: function mergeProperties(data) { | ||
for (var key in data) { | ||
if ('undefined' !== typeof data[key]) { | ||
this.mergeProperty(key, data[key]); | ||
} | ||
} | ||
} | ||
this.reset(); | ||
_lodash2['default'].merge(this, _lodash2['default'].omit(options, omit)); | ||
this.id = options.id || (0, _cuid2['default'])(); | ||
data = this.data; | ||
/** | ||
* Merge 'auction' property. | ||
* | ||
* @param {String} key | ||
* @param {Mixed} value | ||
* @type {Auction} this | ||
* @api private | ||
*/ | ||
if (options.authorization) { | ||
this._auth = options.authorization; | ||
}, { | ||
key: 'mergeProperty', | ||
value: function mergeProperty(key, value) { | ||
if (READ_ONLY_FIELDS[key]) return; | ||
this[key] = value; | ||
} | ||
debug('order initialized %j', data); | ||
/** | ||
* Merge data to auction. | ||
* | ||
* @return {Object} data | ||
* @return {Boolean} | ||
* @api private | ||
*/ | ||
setImmediate(function () { | ||
fn(null, data); | ||
}); | ||
} | ||
}, { | ||
key: 'merge', | ||
value: function merge(data) { | ||
this.mergeProperties(this.deff(data)); | ||
return this.data; | ||
} | ||
/** | ||
* Check validation data. | ||
* | ||
* @param {Object} data | ||
* @return {Error|Undefined} | ||
* @api private | ||
*/ | ||
/** | ||
* Diff auction data. | ||
* | ||
* @return {Object} data | ||
* @return {Object} diff | ||
* @api private | ||
*/ | ||
_createClass(_default, [{ | ||
}, { | ||
key: 'diff', | ||
value: function diff(data) { | ||
var res = {}; | ||
for (var key in data) { | ||
if (_lodash2['default'].isEqual(this[key], data[key])) continue; | ||
if (!Order.READ_ONLY_FIELDS[key]) { | ||
res[key] = data[key]; | ||
} | ||
} | ||
return res; | ||
} | ||
/** | ||
* Validate auction data. | ||
* | ||
* @param {Object} data | ||
* @return {Error|Boolean} | ||
* @api private | ||
*/ | ||
}, { | ||
key: 'validate', | ||
value: function validate(data) { | ||
// Extend this method | ||
return false; | ||
} | ||
/** | ||
* Normalize auction data. | ||
* | ||
* @param {Object} data | ||
* @api private | ||
*/ | ||
}, { | ||
key: 'normalize', | ||
value: function normalize(data) { | ||
// Extend this method | ||
return data; | ||
} | ||
/** | ||
* Check validation data. | ||
* | ||
* @param {Object} data | ||
* @return {Error|Undefined} | ||
* @api private | ||
*/ | ||
}, { | ||
key: 'check', | ||
value: function check(data) { | ||
var error = null; | ||
var message = null; | ||
if (!data.id) { | ||
error = 'Invalid auction ID.'; | ||
message = 'Invalid auction ID.'; | ||
} else if (!_lodash2['default'].isNumber(data.openPrice)) { | ||
message = 'Invalid open price.'; | ||
} else if ('minPrice' in data && !_lodash2['default'].isNumber(data.minPrice)) { | ||
message = 'Invalid minimum price.'; | ||
} else { | ||
message = this.validate(data); | ||
} | ||
if ('number' !== typeof data.openPrice) { | ||
error = 'Invalid opening price.'; | ||
if (message) { | ||
var error = new _errors.AuctionError(message); | ||
debug('auction error %s', message); | ||
this.emit('error', error); | ||
return error; | ||
} | ||
} | ||
if ('minPrice' in data && 'number' !== typeof data.minPrice) { | ||
error = 'Invalid minimum price.'; | ||
} | ||
/** | ||
* Update `auction`. | ||
* | ||
* @param {Object} data | ||
* @param {Function} fn | ||
* @return {Order} this | ||
* @api public | ||
*/ | ||
}, { | ||
key: 'update', | ||
value: function update(data) { | ||
var fn = arguments.length <= 1 || arguments[1] === undefined ? noop : arguments[1]; | ||
data = this.normalize(data); | ||
var error = this.validate(data); | ||
if (error) { | ||
error = new _errors.AuctionError(error); | ||
debug('auction error %s', error); | ||
error = new OrderError(error); | ||
this.emit('error', error); | ||
return error; | ||
return fn(error); | ||
} | ||
fn(null, this.merge(data)); | ||
} | ||
/** | ||
* Destroy `order`. | ||
* Destroy `auction`. | ||
* | ||
@@ -222,5 +347,5 @@ * @param {Function} fn | ||
debug('auction %d error %s', this.id, error); | ||
return setImmediate(function () { | ||
fn(new _errors.AuctionError(error)); | ||
}); | ||
error = new _errors.AuctionError(error); | ||
this.emit('error', error); | ||
return setImmediate(fn.bind(null, error)); | ||
} | ||
@@ -327,6 +452,6 @@ | ||
if (error) { | ||
debug('auction %s error %j', id, error); | ||
return setImmediate(function () { | ||
fn(new _errors.AuctionError(error)); | ||
}); | ||
debug('auction %d error %s', this.id, error); | ||
error = new _errors.AuctionError(error); | ||
this.emit('error', error); | ||
return setImmediate(fn.bind(null, error)); | ||
} | ||
@@ -373,5 +498,5 @@ | ||
debug('auction %d error %s', this.id, error); | ||
return setImmediate(function () { | ||
fn(new _errors.AuctionError(error)); | ||
}); | ||
error = new _errors.AuctionError(error); | ||
this.emit('error', error); | ||
return setImmediate(fn.bind(null, error)); | ||
} | ||
@@ -378,0 +503,0 @@ |
@@ -13,3 +13,3 @@ 'use strict'; | ||
var _get = function get(_x3, _x4, _x5) { var _again = true; _function: while (_again) { var object = _x3, property = _x4, receiver = _x5; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x3 = parent; _x4 = property; _x5 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; | ||
@@ -55,9 +55,15 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function Auction() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var fn = arguments.length <= 1 || arguments[1] === undefined ? noop : arguments[1]; | ||
function Auction(data, options, fn) { | ||
if (options === undefined) options = {}; | ||
_classCallCheck(this, Auction); | ||
_get(Object.getPrototypeOf(Auction.prototype), 'constructor', this).call(this, options, fn); | ||
if ('function' === typeof options) { | ||
fn = options; | ||
options = {}; | ||
} | ||
_get(Object.getPrototypeOf(Auction.prototype), 'constructor', this).call(this, data, fn); | ||
if (options.authorization) { | ||
this._auth = options.authorization; | ||
} | ||
} | ||
@@ -64,0 +70,0 @@ |
{ | ||
"name": "auction", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Easy way to create auctions", | ||
@@ -5,0 +5,0 @@ "author": { |
45875
5.97%1486
9.1%