Skip to content

Commit

Permalink
remove lodash calls from lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tedeh committed Aug 12, 2022
1 parent 8d788a2 commit dcf354e
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 49 deletions.
3 changes: 1 addition & 2 deletions lib/client/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const events = require('events');
const isPlainObject = require('lodash/isPlainObject');
const utils = require('../utils');

/**
Expand All @@ -18,7 +17,7 @@ const utils = require('../utils');
* @return {Client}
*/
const Client = function(server, options) {
if(arguments.length === 1 && isPlainObject(server)) {
if(arguments.length === 1 && utils.isPlainObject(server)) {
options = server;
server = null;
}
Expand Down
34 changes: 13 additions & 21 deletions lib/method.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
'use strict';

const isArray = require('lodash/isArray');
const isPlainObject = require('lodash/isPlainObject');
const isObject = require('lodash/isObject');
const extend = require('lodash/extend');
const keys = require('lodash/keys');
const reduce = require('lodash/reduce');
const pick = require('lodash/pick');
const toArray = require('lodash/toArray');
const toPlainObject = require('lodash/toPlainObject');
const utils = require('./utils');

/**
Expand All @@ -27,7 +18,7 @@ const Method = function(handler, options) {
}

// only got passed options
if(isPlainObject(handler)) {
if(utils.isPlainObject(handler)) {
options = handler;
handler = null;
}
Expand Down Expand Up @@ -67,31 +58,32 @@ Method.prototype.setHandler = function(handler) {
Method.prototype._getHandlerParams = function(params) {
const options = this.options;

const isObjectParams = !isArray(params) && isObject(params) && params;
const isArrayParams = isArray(params);
const isObjectParams = !Array.isArray(params) && utils.isPlainObject(params) && params;
const isArrayParams = Array.isArray(params);

switch(true) {

// handler always gets an array
case options.params === Array:
return isArrayParams ? params : toArray(params);
return isArrayParams ? params : utils.toArray(params);

// handler always gets an object
case options.params === Object:
return isObjectParams ? params : toPlainObject(params);
return isObjectParams ? params : utils.toPlainObject(params);

// handler gets a list of defined properties that should always be set
case isArray(options.params): {
const undefinedParams = reduce(options.params, function(undefinedParams, key) {
undefinedParams[key] = undefined;
return undefinedParams;
case Array.isArray(options.params): {
const undefinedParams = Object.keys(options.params).reduce(function (out, index) {
const key = options.params[index];
out[key] = undefined;
return out;
}, {});
return extend(undefinedParams, pick(params, keys(params)));
return {...undefinedParams, ...utils.pick(params, Object.keys(params))};
}

// handler gets a map of defined properties and their default values
case isPlainObject(options.params):
return extend({}, options.params, pick(params, keys(params)));
case utils.isPlainObject(options.params):
return {...options.params, ...utils.pick(params, Object.keys(params))};

// give params as is
default:
Expand Down
7 changes: 3 additions & 4 deletions lib/server/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const events = require('events');
const isFunction = require('lodash/isFunction');
const jayson = require('../');
const utils = require('../utils');

Expand Down Expand Up @@ -123,7 +122,7 @@ Server.prototype.method = function(name, definition) {

const isRelay = definition instanceof jayson.Client;
const isMethod = definition instanceof Method;
const isDefinitionFunction = isFunction(definition);
const isDefinitionFunction = typeof definition === 'function';

// a valid method is either a function or a client (relayed method)
if(!isRelay && !isMethod && !isDefinitionFunction) {
Expand Down Expand Up @@ -362,7 +361,7 @@ Server.prototype._resolveRouter = function(method, params) {

let router = this.options.router;

if(!isFunction(router)) {
if(typeof router !== 'function') {
router = function(method) {
return this.getMethod(method);
};
Expand All @@ -376,7 +375,7 @@ Server.prototype._resolveRouter = function(method, params) {
}

// got a regular function, make it an instance of jayson.Method
if(isFunction(resolved)) {
if(typeof resolved === 'function') {
return new jayson.Method(resolved);
}

Expand Down
96 changes: 79 additions & 17 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
'use strict';

const compact = require('lodash/compact');
const extend = require('lodash/extend');
const isFunction = require('lodash/isFunction');
const once = require('lodash/once');
const partial = require('lodash/partial');
const JSONStream = require('JSONStream');
const JSONstringify = require('json-stringify-safe');
const uuid = require('uuid').v4;
Expand Down Expand Up @@ -58,31 +53,33 @@ Utils.generateId = function() {

/**
* Merges properties of object b into object a
* @param {...Object} Objects to be merged
* @param {...Object} args Objects to be merged
* @return {Object}
* @private
*/
Utils.merge = function() {
return extend.apply(null, arguments);
Utils.merge = function(...args) {
return args.reduce(function (out, obj) {
return {...out, ...obj};
}, {});
};

/**
* Parses an incoming stream for requests using JSONStream
* @param {Stream} stream
* @param {Object} options
* @param {Function} onRequest - Called once for stream errors and an unlimited amount of times for valid requests
* @param {Function} onRequest Called once for stream errors and an unlimited amount of times for valid requests
*/
Utils.parseStream = function(stream, options, onRequest) {

const onError = once(onRequest);
const onSuccess = partial(onRequest, null);
const onError = Utils.once(onRequest);
const onSuccess = (...args) => onRequest(null, ...args);

const result = JSONStream.parse();

result.on('data', function(data) {

// apply reviver walk function to prevent stringify/parse again
if(isFunction(options.reviver)) {
if(typeof options.reviver === 'function') {
data = Utils.walk({'': data}, '', options.reviver);
}

Expand All @@ -96,6 +93,71 @@ Utils.parseStream = function(stream, options, onRequest) {

};

/**
* Returns a function that can only be called once
* @param {Function} fn
* @return {Function}
*/
Utils.once = function (fn) {
let called = false;
let lastRetval;
return function (...args) {
if (called) return lastRetval;
called = true;
lastRetval = fn.call(this, ...args);
};
};

/**
* Returns true if obj is a plain object (not null)
* @param {*} obj
* @return {Boolean}
*/
Utils.isPlainObject = function (obj) {
return typeof obj === 'object' && obj !== null;
};

/**
* Converts an object to an array
* @param {*} obj
* @return {Array}
*/
Utils.toArray = function (obj) {
if (Array.isArray(obj)) return obj;
if (Utils.isPlainObject(obj)) return Object.keys(obj).map(function (key) {
return obj[key];
});
if (!obj) return [];
return Array.prototype.slice.call(obj);
};

/**
* Converts an object to a plain object
* @param {*} obj
* @return {Object}
*/
Utils.toPlainObject = function (value) {
value = Object(value);
const result = {};
for (const key in value) {
result[key] = value[key];
}
return result;
};

/**
* Picks keys from obj
* @param {Object} obj
* @param {[]String} keys
* @return {Object}
*/
Utils.pick = function (obj, keys) {
return keys.reduce(function (out, key) {
out[key] = obj[key];
return out;
}, {});
};

/**
* Helper to parse a stream and interpret it as JSON
* @param {Stream} stream Stream instance
Expand All @@ -104,7 +166,7 @@ Utils.parseStream = function(stream, options, onRequest) {
*/
Utils.parseBody = function(stream, options, callback) {

callback = once(callback);
callback = Utils.once(callback);
let data = '';

stream.setEncoding('utf8');
Expand Down Expand Up @@ -253,12 +315,12 @@ Utils.JSON.parse = function(str, options, callback) {
let obj = null;
options = options || {};

if(isFunction(options.reviver)) {
if(typeof options.reviver === 'function') {
reviver = options.reviver;
}

try {
obj = JSON.parse.apply(JSON, compact([str, reviver]));
obj = JSON.parse.apply(JSON, [str, reviver].filter(v => v));
} catch(err) {
return callback(err);
}
Expand All @@ -277,12 +339,12 @@ Utils.JSON.stringify = function(obj, options, callback) {
let str = null;
options = options || {};

if(isFunction(options.replacer)) {
if(typeof options.replacer === 'function') {
replacer = options.replacer;
}

try {
str = JSONstringify.apply(JSON, compact([obj, replacer]));
str = JSONstringify.apply(JSON, [obj, replacer].filter(v => v));
} catch(err) {
return callback(err);
}
Expand Down
3 changes: 1 addition & 2 deletions promise/lib/method.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const jayson = require('../../');
const isFunction = require('lodash/isFunction');

/**
* Constructor for a Jayson Promise Method
Expand Down Expand Up @@ -43,7 +42,7 @@ PromiseMethod.prototype.execute = function(server, requestParams, context, outer
outerCallback.apply(null, arguments);
});

wasPromised = promise && isFunction(promise.then);
wasPromised = promise && typeof promise.then === 'function';

// if the handler returned a promise, call the callback when it resolves
if(wasPromised) {
Expand Down
1 change: 0 additions & 1 deletion test/examples.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const last = require('lodash/last');
const { spawn } = require('child_process');
const path = require('path');
const should = require('should');
Expand Down
4 changes: 2 additions & 2 deletions test/promise.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const reduce = require('lodash/reduce');
const forEach = require('lodash/forEach');
const should = require('should');
const fetch = require('node-fetch');
const jayson = require('./../promise');
Expand Down Expand Up @@ -187,7 +186,8 @@ describe('jayson/promise', function() {

};

forEach(suites, function(suite, name) {
Object.keys(suites).forEach(function(name) {
const suite = suites[name];

describe(name, function() {

Expand Down

0 comments on commit dcf354e

Please sign in to comment.