Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

match.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. var valueToString = require("@sinonjs/commons").valueToString;
  3. var indexOf = require("@sinonjs/commons").prototypes.string.indexOf;
  4. var forEach = require("@sinonjs/commons").prototypes.array.forEach;
  5. var type = require("type-detect");
  6. var engineCanCompareMaps = typeof Array.from === "function";
  7. var deepEqual = require("./deep-equal").use(match); // eslint-disable-line no-use-before-define
  8. var isArrayType = require("./is-array-type");
  9. var isSubset = require("./is-subset");
  10. var createMatcher = require("./create-matcher");
  11. /**
  12. * Returns true when `array` contains all of `subset` as defined by the `compare`
  13. * argument
  14. *
  15. * @param {Array} array An array to search for a subset
  16. * @param {Array} subset The subset to find in the array
  17. * @param {Function} compare A comparison function
  18. * @returns {boolean} [description]
  19. * @private
  20. */
  21. function arrayContains(array, subset, compare) {
  22. if (subset.length === 0) {
  23. return true;
  24. }
  25. var i, l, j, k;
  26. for (i = 0, l = array.length; i < l; ++i) {
  27. if (compare(array[i], subset[0])) {
  28. for (j = 0, k = subset.length; j < k; ++j) {
  29. if (i + j >= l) {
  30. return false;
  31. }
  32. if (!compare(array[i + j], subset[j])) {
  33. return false;
  34. }
  35. }
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. /* eslint-disable complexity */
  42. /**
  43. * Matches an object with a matcher (or value)
  44. *
  45. * @alias module:samsam.match
  46. * @param {object} object The object candidate to match
  47. * @param {object} matcherOrValue A matcher or value to match against
  48. * @returns {boolean} true when `object` matches `matcherOrValue`
  49. */
  50. function match(object, matcherOrValue) {
  51. if (matcherOrValue && typeof matcherOrValue.test === "function") {
  52. return matcherOrValue.test(object);
  53. }
  54. switch (type(matcherOrValue)) {
  55. case "bigint":
  56. case "boolean":
  57. case "number":
  58. case "symbol":
  59. return matcherOrValue === object;
  60. case "function":
  61. return matcherOrValue(object) === true;
  62. case "string":
  63. var notNull = typeof object === "string" || Boolean(object);
  64. return (
  65. notNull &&
  66. indexOf(
  67. valueToString(object).toLowerCase(),
  68. matcherOrValue.toLowerCase()
  69. ) >= 0
  70. );
  71. case "null":
  72. return object === null;
  73. case "undefined":
  74. return typeof object === "undefined";
  75. case "Date":
  76. /* istanbul ignore else */
  77. if (type(object) === "Date") {
  78. return object.getTime() === matcherOrValue.getTime();
  79. }
  80. /* istanbul ignore next: this is basically the rest of the function, which is covered */
  81. break;
  82. case "Array":
  83. case "Int8Array":
  84. case "Uint8Array":
  85. case "Uint8ClampedArray":
  86. case "Int16Array":
  87. case "Uint16Array":
  88. case "Int32Array":
  89. case "Uint32Array":
  90. case "Float32Array":
  91. case "Float64Array":
  92. return (
  93. isArrayType(matcherOrValue) &&
  94. arrayContains(object, matcherOrValue, match)
  95. );
  96. case "Map":
  97. /* istanbul ignore next: this is covered by a test, that is only run in IE, but we collect coverage information in node*/
  98. if (!engineCanCompareMaps) {
  99. throw new Error(
  100. "The JavaScript engine does not support Array.from and cannot reliably do value comparison of Map instances"
  101. );
  102. }
  103. return (
  104. type(object) === "Map" &&
  105. arrayContains(
  106. Array.from(object),
  107. Array.from(matcherOrValue),
  108. match
  109. )
  110. );
  111. default:
  112. break;
  113. }
  114. switch (type(object)) {
  115. case "null":
  116. return false;
  117. case "Set":
  118. return isSubset(matcherOrValue, object, match);
  119. default:
  120. break;
  121. }
  122. /* istanbul ignore else */
  123. if (matcherOrValue && typeof matcherOrValue === "object") {
  124. if (matcherOrValue === object) {
  125. return true;
  126. }
  127. if (typeof object !== "object") {
  128. return false;
  129. }
  130. var prop;
  131. // eslint-disable-next-line guard-for-in
  132. for (prop in matcherOrValue) {
  133. var value = object[prop];
  134. if (
  135. typeof value === "undefined" &&
  136. typeof object.getAttribute === "function"
  137. ) {
  138. value = object.getAttribute(prop);
  139. }
  140. if (
  141. matcherOrValue[prop] === null ||
  142. typeof matcherOrValue[prop] === "undefined"
  143. ) {
  144. if (value !== matcherOrValue[prop]) {
  145. return false;
  146. }
  147. } else if (
  148. typeof value === "undefined" ||
  149. !deepEqual(value, matcherOrValue[prop])
  150. ) {
  151. return false;
  152. }
  153. }
  154. return true;
  155. }
  156. /* istanbul ignore next */
  157. throw new Error("Matcher was an unknown or unsupported type");
  158. }
  159. /* eslint-enable complexity */
  160. forEach(Object.keys(createMatcher), function (key) {
  161. match[key] = createMatcher[key];
  162. });
  163. module.exports = match;