object.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Merge the second object into the first one.
  3. * @param {Object} dst
  4. * @param {Object} src
  5. */
  6. export function merge(dst, src) {
  7. for (const k in src) {
  8. if (!Object.prototype.hasOwnProperty.call(src, k)) continue;
  9. if (k === "__proto__" || k === "constructor") continue;
  10. if (dst[k] instanceof Object) {
  11. merge(dst[k], src[k]);
  12. } else {
  13. dst[k] = src[k];
  14. }
  15. }
  16. }
  17. /**
  18. * @param {unknown} obj - The object to check.
  19. * @returns {boolean} True if the object is an Error, false otherwise.
  20. */
  21. export function isError(obj) {
  22. return Object.prototype.toString.call(obj) === "[object Error]";
  23. }
  24. /**
  25. * @param {unknown} val - The value to check.
  26. * @returns {boolean} True if the value is a function, false otherwise.
  27. */
  28. export function isFunction(val) {
  29. return typeof val === "function";
  30. }
  31. /**
  32. * @param {unknown} x - The value to check.
  33. * @returns {boolean} True if the value is undefined, false otherwise.
  34. */
  35. export function isUndefined(x) {
  36. return typeof x === "undefined";
  37. }
  38. /**
  39. * @param {unknown} o - The value to check.
  40. * @returns {boolean} True if the value is an Error
  41. */
  42. export function isErrorObject (o) {
  43. return o instanceof Error;
  44. }