jayson
Advanced tools
Comparing version 3.7.0 to 4.0.0
'use strict'; | ||
const events = require('events'); | ||
const isPlainObject = require('lodash/isPlainObject'); | ||
const utils = require('../utils'); | ||
@@ -21,3 +20,3 @@ | ||
const Client = function(server, options) { | ||
if(arguments.length === 1 && isPlainObject(server)) { | ||
if(arguments.length === 1 && utils.isPlainObject(server)) { | ||
options = server; | ||
@@ -24,0 +23,0 @@ server = null; |
'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'); | ||
@@ -30,3 +21,3 @@ | ||
// only got passed options | ||
if(isPlainObject(handler)) { | ||
if(utils.isPlainObject(handler)) { | ||
options = handler; | ||
@@ -71,4 +62,4 @@ handler = null; | ||
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); | ||
@@ -79,20 +70,21 @@ switch(true) { | ||
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))}; | ||
@@ -99,0 +91,0 @@ // give params as is |
'use strict'; | ||
const events = require('events'); | ||
const isFunction = require('lodash/isFunction'); | ||
const jayson = require('../'); | ||
@@ -126,3 +125,3 @@ const utils = require('../utils'); | ||
const isMethod = definition instanceof Method; | ||
const isDefinitionFunction = isFunction(definition); | ||
const isDefinitionFunction = typeof definition === 'function'; | ||
@@ -366,3 +365,3 @@ // a valid method is either a function or a client (relayed method) | ||
if(!isFunction(router)) { | ||
if(typeof router !== 'function') { | ||
router = function(method) { | ||
@@ -381,3 +380,3 @@ return this.getMethod(method); | ||
// got a regular function, make it an instance of jayson.Method | ||
if(isFunction(resolved)) { | ||
if(typeof resolved === 'function') { | ||
return new jayson.Method(resolved); | ||
@@ -384,0 +383,0 @@ } |
'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'); | ||
@@ -61,8 +56,10 @@ const JSONstringify = require('json-stringify-safe'); | ||
* 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}; | ||
}, {}); | ||
}; | ||
@@ -74,8 +71,8 @@ | ||
* @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); | ||
@@ -87,3 +84,3 @@ const result = JSONStream.parse(); | ||
// apply reviver walk function to prevent stringify/parse again | ||
if(isFunction(options.reviver)) { | ||
if(typeof options.reviver === 'function') { | ||
data = Utils.walk({'': data}, '', options.reviver); | ||
@@ -103,2 +100,67 @@ } | ||
/** | ||
* 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 | ||
@@ -111,3 +173,3 @@ * @param {Stream} stream Stream instance | ||
callback = once(callback); | ||
callback = Utils.once(callback); | ||
let data = ''; | ||
@@ -261,3 +323,3 @@ | ||
if(isFunction(options.reviver)) { | ||
if(typeof options.reviver === 'function') { | ||
reviver = options.reviver; | ||
@@ -267,3 +329,3 @@ } | ||
try { | ||
obj = JSON.parse.apply(JSON, compact([str, reviver])); | ||
obj = JSON.parse.apply(JSON, [str, reviver].filter(v => v)); | ||
} catch(err) { | ||
@@ -287,3 +349,3 @@ return callback(err); | ||
if(isFunction(options.replacer)) { | ||
if(typeof options.replacer === 'function') { | ||
replacer = options.replacer; | ||
@@ -293,3 +355,3 @@ } | ||
try { | ||
str = JSONstringify.apply(JSON, compact([obj, replacer])); | ||
str = JSONstringify.apply(JSON, [obj, replacer].filter(v => v)); | ||
} catch(err) { | ||
@@ -296,0 +358,0 @@ return callback(err); |
{ | ||
"name": "jayson", | ||
"version": "3.7.0", | ||
"version": "4.0.0", | ||
"description": "JSON-RPC 1.0/2.0 compliant server and client", | ||
@@ -62,3 +62,2 @@ "license": "MIT", | ||
"JSONStream": "^1.3.5", | ||
"lodash": "^4.17.20", | ||
"uuid": "^8.3.2", | ||
@@ -69,3 +68,2 @@ "ws": "^7.4.5" | ||
"@types/express-serve-static-core": "^4.17.30", | ||
"@types/lodash": "^4.14.182", | ||
"body-parser": "^1.19.0", | ||
@@ -72,0 +70,0 @@ "connect": "^3.7.0", |
'use strict'; | ||
const jayson = require('../../'); | ||
const isFunction = require('lodash/isFunction'); | ||
@@ -46,3 +45,3 @@ /** | ||
wasPromised = promise && isFunction(promise.then); | ||
wasPromised = promise && typeof promise.then === 'function'; | ||
@@ -49,0 +48,0 @@ // if the handler returned a promise, call the callback when it resolves |
@@ -117,2 +117,4 @@ # Jayson | ||
- *4.0.0* | ||
- Remove `lodash` dependency which should halve bundle size. There might be minor incompatibilities if you pass funky object or array types to jayson methods. | ||
- *3.6.4* | ||
@@ -703,3 +705,2 @@ - Websocket client and server support | ||
```javascript | ||
const _ = require('lodash'); | ||
const jayson = require('jayson'); | ||
@@ -721,10 +722,11 @@ | ||
// this reduction produces an object like this: {'foo.bar': [Function], 'math.add': [Function]} | ||
const map = _.reduce(methods, collapse('', '.'), {}); | ||
const map = Object.keys(methods).reduce(collapse('', '.', methods), {}); | ||
const server = new jayson.Server(map); | ||
function collapse(stem, sep) { | ||
return function(map, value, key) { | ||
function collapse(stem, sep, obj) { | ||
return function(map, key) { | ||
const prop = stem ? stem + sep + key : key; | ||
if(_.isFunction(value)) map[prop] = value; | ||
else if(_.isObject(value)) map = _.reduce(value, collapse(prop, sep), map); | ||
const value = obj[key]; | ||
if(typeof value === 'function') map[prop] = value; | ||
else if(typeof value === 'object' && value !== null) map = Object.keys(value).reduce(collapse(prop, sep, value), map); | ||
return map; | ||
@@ -757,3 +759,2 @@ } | ||
const jayson = require('jayson'); | ||
const _ = require('lodash'); | ||
@@ -784,3 +785,3 @@ const methods = { | ||
handler: function(args, done) { | ||
const result = _.isArray(args); | ||
const result = Array.isArray(args); | ||
done(null, result); | ||
@@ -804,5 +805,3 @@ }, | ||
function sum(list) { | ||
return _.reduce(list, function(sum, val) { | ||
return sum + val; | ||
}, 0); | ||
return Object.keys(list).reduce(function(sum, key) { return sum + list[key]; }, 0); | ||
} | ||
@@ -947,3 +946,2 @@ ``` | ||
```javascript | ||
const _ = require('lodash'); | ||
const jayson = require('jayson'); | ||
@@ -1159,3 +1157,2 @@ const jsonParser = require('body-parser').json; | ||
const jayson = require('jayson/promise'); | ||
const _ = require('lodash'); | ||
@@ -1165,3 +1162,3 @@ const server = new jayson.Server({ | ||
add: async function(args) { | ||
const sum = _.reduce(args, function(sum, value) { return sum + value; }, 0); | ||
const sum = Object.keys(args).reduce(function(sum, key) { return sum + args[key]; }, 0); | ||
return sum; | ||
@@ -1296,3 +1293,2 @@ }, | ||
```javascript | ||
const _ = require('lodash'); | ||
const jayson = require('jayson'); | ||
@@ -1306,3 +1302,4 @@ const jsonParser = require('body-parser').json; | ||
add: function(numbers, callback) { | ||
callback(null, _.reduce(numbers, (sum, val) => sum + val, 0)); | ||
const sum = Object.keys(numbers).reduce(function(sum, key) { return sum + numbers[key]; }, 0); | ||
callback(null, sum); | ||
} | ||
@@ -1309,0 +1306,0 @@ }); |
161377
12
16
2910
1383
- Removedlodash@^4.17.20
- Removedlodash@4.17.21(transitive)