Keine Beschreibung

index.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*!
  2. * make-iterator <https://github.com/jonschlinkert/make-iterator>
  3. *
  4. * Copyright (c) 2014, 2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var typeOf = require('kind-of');
  9. module.exports = function makeIterator(target, thisArg) {
  10. switch (typeOf(target)) {
  11. case 'undefined':
  12. case 'null':
  13. return noop;
  14. case 'function':
  15. // function is the first to improve perf (most common case)
  16. // also avoid using `Function#call` if not needed, which boosts
  17. // perf a lot in some cases
  18. return (typeof thisArg !== 'undefined') ? function(val, i, arr) {
  19. return target.call(thisArg, val, i, arr);
  20. } : target;
  21. case 'object':
  22. return function(val) {
  23. return deepMatches(val, target);
  24. };
  25. case 'regexp':
  26. return function(str) {
  27. return target.test(str);
  28. };
  29. case 'string':
  30. case 'number':
  31. default: {
  32. return prop(target);
  33. }
  34. }
  35. };
  36. function containsMatch(array, value) {
  37. var len = array.length;
  38. var i = -1;
  39. while (++i < len) {
  40. if (deepMatches(array[i], value)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. function matchArray(arr, value) {
  47. var len = value.length;
  48. var i = -1;
  49. while (++i < len) {
  50. if (!containsMatch(arr, value[i])) {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. function matchObject(obj, value) {
  57. for (var key in value) {
  58. if (value.hasOwnProperty(key)) {
  59. if (deepMatches(obj[key], value[key]) === false) {
  60. return false;
  61. }
  62. }
  63. }
  64. return true;
  65. }
  66. /**
  67. * Recursively compare objects
  68. */
  69. function deepMatches(val, value) {
  70. if (typeOf(val) === 'object') {
  71. if (Array.isArray(val) && Array.isArray(value)) {
  72. return matchArray(val, value);
  73. } else {
  74. return matchObject(val, value);
  75. }
  76. } else {
  77. return val === value;
  78. }
  79. }
  80. function prop(name) {
  81. return function(obj) {
  82. return obj[name];
  83. };
  84. }
  85. function noop(val) {
  86. return val;
  87. }