isKey.js 848 B

12345678910111213141516171819202122232425262728
  1. var isArray = require('../lang/isArray'),
  2. toObject = require('./toObject');
  3. /** Used to match property names within property paths. */
  4. var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\n\\]|\\.)*?\1)\]/,
  5. reIsPlainProp = /^\w*$/;
  6. /**
  7. * Checks if `value` is a property name and not a property path.
  8. *
  9. * @private
  10. * @param {*} value The value to check.
  11. * @param {Object} [object] The object to query keys on.
  12. * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
  13. */
  14. function isKey(value, object) {
  15. var type = typeof value;
  16. if ((type == 'string' && reIsPlainProp.test(value)) || type == 'number') {
  17. return true;
  18. }
  19. if (isArray(value)) {
  20. return false;
  21. }
  22. var result = !reIsDeepProp.test(value);
  23. return result || (object != null && value in toObject(object));
  24. }
  25. module.exports = isKey;