sole.js 485 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var _require = require('../helpers/is'),
  3. isFunction = _require.isFunction;
  4. module.exports = function sole(key, operator, value) {
  5. var collection;
  6. if (isFunction(key)) {
  7. collection = this.filter(key);
  8. } else {
  9. collection = this.where(key, operator, value);
  10. }
  11. if (collection.isEmpty()) {
  12. throw new Error('Item not found.');
  13. }
  14. if (collection.count() > 1) {
  15. throw new Error('Multiple items found.');
  16. }
  17. return collection.first();
  18. };