hash-custom-usage-opts.js 731 B

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var flaverr = require('flaverr');
  6. /**
  7. * hashCustomUsageOpts()
  8. *
  9. * @param {Dictionary} customUsageOpts
  10. * @return {String}
  11. * @throws {Error}
  12. * @property {E_UNHASHABLE} If something in opts could not be hashed
  13. */
  14. module.exports = function hashCustomUsageOpts(customUsageOpts){
  15. return _.reduce(_.keys(customUsageOpts).sort(), function(hashSoFar, optKey){
  16. var optValue = customUsageOpts[optKey];
  17. // If custom usage opts contain a non-string, then don't try to cache.
  18. if (!_.isString(optValue)) {
  19. throw flaverr('E_UNHASHABLE');
  20. }
  21. hashSoFar += optKey+':'+JSON.stringify(optValue)+'|';
  22. return hashSoFar;
  23. }, '');
  24. };