underscore.js 1000 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. const _ = (module.exports = require('underscore'));
  2. const uuid = require('uuid');
  3. const underscoreDeepExtend = require('underscore-deep-extend');
  4. _.mixin({
  5. /**
  6. * Get a deeply nested object property.
  7. *
  8. * @param {Object} obj The object.
  9. * @param {String} path The path within the object to fetch.
  10. * @param {*} fallbackValue The value to return if given path not found.
  11. *
  12. * @return {*} Returns value if found; otherwise the fallbackVAlue.
  13. */
  14. get(obj, path, fallbackValue) {
  15. if (this.isUndefined(obj) || obj === null || typeof path !== 'string') {
  16. return fallbackValue;
  17. }
  18. const fields = path.split('.');
  19. let result = obj;
  20. for (let i = 0; i < fields.length; ++i) {
  21. if (!this.isObject(result) && !this.isArray(result)) {
  22. return fallbackValue;
  23. }
  24. result = result[fields[i]];
  25. }
  26. return result || fallbackValue;
  27. },
  28. deepExtend: underscoreDeepExtend(_),
  29. uuid() {
  30. return uuid.v4();
  31. }
  32. });
  33. module.exports = _;