UNPKG

@trade-quorum/tq-helpers

Version:

Various common helpers for Trade Quorum API/App

39 lines (30 loc) 1.15 kB
const _ = require('lodash'); exports.keysValuesToString = (keys, values) => { if (!keys || !values || keys.length !== values.length) throw new Error( 'Keys and values should be provided and have the same size' ); let string = ''; for (let i = 0; i < keys.length; i++) { if (i !== 0) string += ', '; string += `${keys[i]} = ${values[i]}`; } return string; }; exports.findWhere = (objects, query) => _.find(objects, _.matches(query)); exports.findIndexWhere = (objects, query) => _.findIndex(objects, _.matches(query)); exports.hasWhere = (objects, query) => !!exports.findWhere(objects, query); exports.filterWhere = (objects, query) => _.filter(objects, _.matches(query)); exports.findWhereOrError = (objects, query, objectType) => { const object = exports.findWhere(objects, query); if (!object) throw new Error(`${objectType} ${JSON.stringify(query)} not found`); return object; }; exports.uniq = (objects) => _.uniq(objects.map(JSON.stringify)).map(JSON.parse); exports.cloneAndReplace = (array, index, value) => { const newArray = [...array]; newArray[index] = value; return newArray; };