getPath.js 834 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. /*!
  3. * Behaves like `Schema#path()`, except for it also digs into arrays without
  4. * needing to put `.0.`, so `getPath(schema, 'docArr.elProp')` works.
  5. */
  6. const numberRE = /^\d+$/;
  7. module.exports = function getPath(schema, path) {
  8. let schematype = schema.path(path);
  9. if (schematype != null) {
  10. return schematype;
  11. }
  12. const pieces = path.split('.');
  13. let cur = '';
  14. let isArray = false;
  15. for (const piece of pieces) {
  16. if (isArray && numberRE.test(piece)) {
  17. continue;
  18. }
  19. cur = cur.length === 0 ? piece : cur + '.' + piece;
  20. schematype = schema.path(cur);
  21. if (schematype != null && schematype.schema) {
  22. schema = schematype.schema;
  23. cur = '';
  24. if (!isArray && schematype.$isMongooseDocumentArray) {
  25. isArray = true;
  26. }
  27. }
  28. }
  29. return schematype;
  30. };