You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
880 B

4 years ago
  1. var isArray = require('./isArray'),
  2. isSymbol = require('./isSymbol');
  3. /** Used to match property names within property paths. */
  4. var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\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. if (isArray(value)) {
  16. return false;
  17. }
  18. var type = typeof value;
  19. if (type == 'number' || type == 'symbol' || type == 'boolean' ||
  20. value == null || isSymbol(value)) {
  21. return true;
  22. }
  23. return reIsPlainProp.test(value) || !reIsDeepProp.test(value) ||
  24. (object != null && value in Object(object));
  25. }
  26. module.exports = isKey;