Skip to content

Commit

Permalink
Convert source to JavaScript compatible with Node4
Browse files Browse the repository at this point in the history
- This also renames the src directory to lib.
  • Loading branch information
ricardograca committed May 1, 2018
1 parent b9670c1 commit 2ebd1af
Show file tree
Hide file tree
Showing 24 changed files with 413 additions and 417 deletions.
44 changes: 17 additions & 27 deletions src/base/collection.js → lib/base/collection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// Base Collection
// ---------------
'use strict';

// All exernal dependencies required in this scope.
import _, { invokeMap, noop, negate, isNull } from 'lodash';
import inherits from 'inherits';
const _ = require('lodash');
const inherits = require('inherits'); // TODO: Possibly unnecessary

// All components that need to be referenced in this scope.
import Events from './events';
import Promise from './promise';
import ModelBase from './model';
import extend from '../extend';

const { splice, slice } = Array.prototype;
const Events = require('./events');
const Promise = require('./promise');
const ModelBase = require('./model');
const extend = require('../extend');

/**
* @class CollectionBase
Expand Down Expand Up @@ -98,7 +95,7 @@ const addOptions = {add: true, remove: false};
*
* @see Collection
*/
CollectionBase.prototype.initialize = noop;
CollectionBase.prototype.initialize = function() {};

/**
* @method
Expand Down Expand Up @@ -146,7 +143,7 @@ CollectionBase.prototype.toString = function() {
* @returns {Object} Serialized model as a plain object.
*/
CollectionBase.prototype.serialize = function(options) {
return invokeMap(this.models, 'toJSON', options).filter(negate(isNull));
return this.invokeMap('toJSON', options).filter(_.negate(_.isNull));
}

/**
Expand Down Expand Up @@ -196,8 +193,7 @@ CollectionBase.prototype.set = function(models, options) {
const toAdd = []
const toRemove = [];
const modelMap = {};
const { add, merge, remove } = options;
let order = add && remove ? [] : false;
let order = options.add && options.remove ? [] : false;

// Turn bare objects into model references, and prevent invalid models
// from being added.
Expand All @@ -213,17 +209,17 @@ CollectionBase.prototype.set = function(models, options) {
// optionally merge it into the existing model.
const existing = this.get(id)
if (existing) {
if (remove) {
if (options.remove) {
modelMap[existing.cid] = true;
}
if (merge) {
if (options.merge) {
attrs = attrs === model ? model.attributes : attrs;
if (options.parse) attrs = existing.parse(attrs, options);
existing.set(attrs, options);
}

// This is a new model, push it to the `toAdd` list.
} else if (add) {
} else if (options.add) {
if (!(model = this._prepareModel(attrs, options))) continue;
toAdd.push(model);
this._byId[model.cid] = model;
Expand All @@ -233,7 +229,7 @@ CollectionBase.prototype.set = function(models, options) {
}

// Remove nonexistent models if appropriate.
if (remove) {
if (options.remove) {
for (i = 0, l = this.length; i < l; ++i) {
if (!modelMap[(model = this.models[i]).cid]) toRemove.push(model);
}
Expand All @@ -244,7 +240,7 @@ CollectionBase.prototype.set = function(models, options) {
if (toAdd.length || (order && order.length)) {
this.length += toAdd.length;
if (at != null) {
splice.apply(this.models, [at, 0].concat(toAdd));
Array.prototype.splice.apply(this.models, [at, 0].concat(toAdd));
} else {
if (order) {
this.models.length = 0;
Expand Down Expand Up @@ -470,7 +466,7 @@ CollectionBase.prototype.shift = function(options) {
* Slice out a sub-array of models from the collection.
*/
CollectionBase.prototype.slice = function() {
return slice.apply(this.models, arguments);
return Array.prototype.slice.apply(this.models, arguments);
};

/**
Expand Down Expand Up @@ -746,7 +742,7 @@ const methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight',
// Mix in each Lodash method as a proxy to `Collection#models`.
_.each(methods, function(method) {
CollectionBase.prototype[method] = function() {
return _[method](this.models, ...arguments);
return _[method].apply(_, [this.models].concat(Array.from(arguments)));
};
});

Expand Down Expand Up @@ -794,10 +790,4 @@ _.each(attributeMethods, function(method) {
*/
CollectionBase.extend = extend;

/*
* NOTE: For some reason `export` is failing in the version of Babel I'm
* currently using. At some stage it should be corrected to:
*
* export default CollectionBase;
*/
module.exports = CollectionBase;
15 changes: 9 additions & 6 deletions src/base/eager.js → lib/base/eager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
// a database specific `eagerFetch` method, which then may utilize
// `pushModels` for pairing the models depending on the database need.

import _, { map, noop } from 'lodash';
import Promise from './promise';
'use strict';

const _ = require('lodash');
const Promise = require('./promise');

function EagerBase(parent, parentResponse, target) {
this.parent = parent;
Expand Down Expand Up @@ -65,7 +67,7 @@ _.extend(EagerBase.prototype, {
pendingDeferred.push(this.eagerFetch(relationName, handled[relationName], _.extend({}, options, {
isEager: true,
withRelated: subRelated[relationName],
_beforeFn: withRelated[relationName] || noop
_beforeFn: withRelated[relationName] || function() {}
})));
}

Expand All @@ -82,7 +84,7 @@ _.extend(EagerBase.prototype, {
for (let i = 0, l = withRelated.length; i < l; i++) {
const related = withRelated[i];
if (_.isString(related)) {
obj[related] = noop
obj[related] = function() {}
} else {
_.extend(obj, related);
}
Expand All @@ -94,8 +96,9 @@ _.extend(EagerBase.prototype, {
// which is used to correcly pair additional nested relations.
pushModels: function(relationName, handled, response) {
const models = this.parent;
const { relatedData } = handled;
const related = map(response, row => relatedData.createModel(row));
const relatedData = handled.relatedData;
const related = _.map(response, row => relatedData.createModel(row));

return relatedData.eagerPair(relationName, related, models);
}

Expand Down
37 changes: 20 additions & 17 deletions src/base/events.js → lib/base/events.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Events
// ---------------

import Promise from './promise';
import events from 'events'
import { each, flatMap, once as _once } from 'lodash';

const { EventEmitter } = events;
'use strict';

const Promise = require('./promise');
const events = require('events');
const _ = require('lodash');
const EventEmitter = events.EventEmitter;
const eventNames = text => text.split(/\s+/);

/**
Expand All @@ -15,8 +15,7 @@ const eventNames = text => text.split(/\s+/);
* Base Event class inherited by {@link Model} and {@link Collection}. It's not
* meant to be used directly, and is only displayed here for completeness.
*/
export default class Events extends EventEmitter {

class Events extends EventEmitter {
/**
* @method Events#on
* @description
Expand All @@ -31,7 +30,7 @@ export default class Events extends EventEmitter {
* That callback to invoke whenever the event is fired.
*/
on(nameOrNames, handler) {
each(eventNames(nameOrNames), (name) => {
eventNames(nameOrNames).forEach((name) => {
super.on(name, handler)
});
return this;
Expand All @@ -52,7 +51,7 @@ export default class Events extends EventEmitter {
return this.removeAllListeners();
}

each(eventNames(nameOrNames), name => this.removeAllListeners(name));
eventNames(nameOrNames).forEach(name => this.removeAllListeners(name));
return this;
}

Expand All @@ -69,9 +68,9 @@ export default class Events extends EventEmitter {
* @param {...mixed} [args]
* Extra arguments to pass to the event listener callback function.
*/
trigger(nameOrNames, ...args) {
each(eventNames(nameOrNames), (name) => {
this.emit(name, ...args)
trigger(nameOrNames) {
eventNames(nameOrNames).forEach((name) => {
this.emit.apply(this, [name].concat(Array.from(arguments)))
});
return this;
}
Expand All @@ -95,10 +94,12 @@ export default class Events extends EventEmitter {
* @returns Promise<mixed[]>
* A promise resolving the the resolved return values of any triggered handlers.
*/
triggerThen(nameOrNames, ...args) {
triggerThen(nameOrNames) {
const names = eventNames(nameOrNames);
const listeners = flatMap(names, name => this.listeners(name));
return Promise.mapSeries(listeners, listener => listener.apply(this, args));
const listeners = _.flatMap(names, name => this.listeners(name));
const args = Array.from(arguments);

return Promise.mapSeries(listeners, listener => listener.apply(this, args.slice(1)));
}

/**
Expand All @@ -117,11 +118,13 @@ export default class Events extends EventEmitter {
* That callback to invoke only once when the event is fired.
*/
once(name, callback) {
const wrapped = _once((...args) => {
const wrapped = _.once(() => {
this.off(name, wrapped);
return callback.apply(this, args);
return callback.apply(this, Array.from(arguments));
});
wrapped._callback = callback;
return this.on(name, wrapped);
}
}

module.exports = Events;

0 comments on commit 2ebd1af

Please sign in to comment.