Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 977 B

il y a 3 ans
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*!
  2. * shallow-clone <https://github.com/jonschlinkert/shallow-clone>
  3. *
  4. * Copyright (c) 2015, Jon Schlinkert.
  5. * Licensed under the MIT License.
  6. */
  7. 'use strict';
  8. var utils = require('./utils');
  9. /**
  10. * Shallow copy an object, array or primitive.
  11. *
  12. * @param {any} `val`
  13. * @return {any}
  14. */
  15. function clone(val) {
  16. var type = utils.typeOf(val);
  17. if (clone.hasOwnProperty(type)) {
  18. return clone[type](val);
  19. }
  20. return val;
  21. }
  22. clone.array = function cloneArray(arr) {
  23. return arr.slice();
  24. };
  25. clone.date = function cloneDate(date) {
  26. return new Date(+date);
  27. };
  28. clone.object = function cloneObject(obj) {
  29. if (utils.isObject(obj)) {
  30. return utils.mixin({}, obj);
  31. } else {
  32. return obj;
  33. }
  34. };
  35. clone.regexp = function cloneRegExp(re) {
  36. var flags = '';
  37. flags += re.multiline ? 'm' : '';
  38. flags += re.global ? 'g' : '';
  39. flags += re.ignorecase ? 'i' : '';
  40. return new RegExp(re.source, flags);
  41. };
  42. /**
  43. * Expose `clone`
  44. */
  45. module.exports = clone;