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.

index.md 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. # samsam
  2. > Same same, but different
  3. `samsam` is a collection of predicate and comparison functions useful for
  4. identifiying the type of values and to compare values with varying degrees of
  5. strictness.
  6. `samsam` is a general-purpose library. It works in browsers and Node. It will
  7. define itself as an AMD module if you want it to (i.e. if there's a `define`
  8. function available).
  9. ## Predicate functions
  10. ### `isArguments(value)`
  11. Returns `true` if `value` is an `arguments` object, `false` otherwise.
  12. ### `isNegZero(value)`
  13. Returns `true` if `value` is `-0`.
  14. ### `isElement(value)`
  15. Returns `true` if `value` is a DOM element node. Unlike
  16. Underscore.js/lodash, this function will return `false` if `value` is an
  17. _element-like_ object, i.e. a regular object with a `nodeType` property that
  18. holds the value `1`.
  19. ### `isSet(value)`
  20. Returns `true` if `value` is a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
  21. ## Comparison functions
  22. ### `identical(x, y)`
  23. Strict equality check according to EcmaScript Harmony's `egal`.
  24. **From the Harmony wiki:**
  25. > An egal function simply makes available the internal `SameValue` function
  26. > from section 9.12 of the ES5 spec. If two values are egal, then they are not
  27. > observably distinguishable.
  28. `identical` returns `true` when `===` is `true`, except for `-0` and
  29. `+0`, where it returns `false`. Additionally, it returns `true` when
  30. `NaN` is compared to itself.
  31. ### `deepEqual(actual, expectation)`
  32. Deep equal comparison. Two values are "deep equal" if:
  33. - They are identical
  34. - They are both date objects representing the same time
  35. - They are both arrays containing elements that are all deepEqual
  36. - They are objects with the same set of properties, and each property
  37. in `actual` is deepEqual to the corresponding property in `expectation`
  38. - `actual` can have [symbolic properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) that are missing from `expectation`
  39. ### Matcher
  40. Match values and objects by type or or other fuzzy criteria. `samsam` ships
  41. with these built in matchers:
  42. #### `sinon.match.any`
  43. Matches anything.
  44. #### `sinon.match.defined`
  45. Requires the value to be defined.
  46. #### `sinon.match.truthy`
  47. Requires the value to be truthy.
  48. #### `sinon.match.falsy`
  49. Requires the value to be falsy.
  50. #### `sinon.match.bool`
  51. Requires the value to be a `Boolean`
  52. #### `sinon.match.number`
  53. Requires the value to be a `Number`.
  54. #### `sinon.match.string`
  55. Requires the value to be a `String`.
  56. #### `sinon.match.object`
  57. Requires the value to be an `Object`.
  58. #### `sinon.match.func`
  59. Requires the value to be a `Function`.
  60. #### `sinon.match.array`
  61. Requires the value to be an `Array`.
  62. #### `sinon.match.array.deepEquals(arr)`
  63. Requires an `Array` to be deep equal another one.
  64. #### `sinon.match.array.startsWith(arr)`
  65. Requires an `Array` to start with the same values as another one.
  66. #### `sinon.match.array.endsWith(arr)`
  67. Requires an `Array` to end with the same values as another one.
  68. #### `sinon.match.array.contains(arr)`
  69. Requires an `Array` to contain each one of the values the given array has.
  70. #### `sinon.match.map`
  71. Requires the value to be a `Map`.
  72. #### `sinon.match.map.deepEquals(map)`
  73. Requires a `Map` to be deep equal another one.
  74. #### `sinon.match.map.contains(map)`
  75. Requires a `Map` to contain each one of the items the given map has.
  76. #### `sinon.match.set`
  77. Requires the value to be a `Set`.
  78. #### `sinon.match.set.deepEquals(set)`
  79. Requires a `Set` to be deep equal another one.
  80. #### `sinon.match.set.contains(set)`
  81. Requires a `Set` to contain each one of the items the given set has.
  82. #### `sinon.match.regexp`
  83. Requires the value to be a regular expression.
  84. #### `sinon.match.date`
  85. Requires the value to be a `Date` object.
  86. #### `sinon.match.symbol`
  87. Requires the value to be a `Symbol`.
  88. #### `sinon.match.in(array)`
  89. Requires the value to be in the `array`.
  90. #### `sinon.match.same(ref)`
  91. Requires the value to strictly equal `ref`.
  92. #### `sinon.match.typeOf(type)`
  93. Requires the value to be of the given type, where `type` can be one of
  94. `"undefined"`,
  95. `"null"`,
  96. `"boolean"`,
  97. `"number"`,
  98. `"string"`,
  99. `"object"`,
  100. `"function"`,
  101. `"array"`,
  102. `"regexp"`,
  103. `"date"` or
  104. `"symbol"`.
  105. #### `sinon.match.instanceOf(type)`
  106. Requires the value to be an instance of the given `type`.
  107. #### `sinon.match.has(property[, expectation])`
  108. Requires the value to define the given `property`.
  109. The property might be inherited via the prototype chain. If the optional expectation is given, the value of the property is deeply compared with the expectation. The expectation can be another matcher.
  110. #### `sinon.match.hasOwn(property[, expectation])`
  111. Same as `sinon.match.has` but the property must be defined by the value itself. Inherited properties are ignored.
  112. #### `sinon.match.hasNested(propertyPath[, expectation])`
  113. Requires the value to define the given `propertyPath`. Dot (`prop.prop`) and bracket (`prop[0]`) notations are supported as in [Lodash.get](https://lodash.com/docs/4.4.2#get).
  114. The propertyPath might be inherited via the prototype chain. If the optional expectation is given, the value at the propertyPath is deeply compared with the expectation. The expectation can be another matcher.
  115. ```javascript
  116. sinon.match.hasNested("a[0].b.c");
  117. // Where actual is something like
  118. var actual = { a: [{ b: { c: 3 } }] };
  119. sinon.match.hasNested("a.b.c");
  120. // Where actual is something like
  121. var actual = { a: { b: { c: 3 } } };
  122. ```
  123. #### `sinon.match.every(matcher)`
  124. Requires **every** element of an `Array`, `Set` or `Map`, or alternatively **every** value of an `Object` to match the given `matcher`.
  125. #### `sinon.match.some(matcher)`
  126. Requires **any** element of an `Array`, `Set` or `Map`, or alternatively **any** value of an `Object` to match the given `matcher`.
  127. ## Combining matchers
  128. All matchers implement `and` and `or`. This allows to logically combine mutliple matchers. The result is a new matchers that requires both (and) or one of the matchers (or) to return `true`.
  129. ```javascript
  130. var stringOrNumber = sinon.match.string.or(sinon.match.number);
  131. var bookWithPages = sinon.match.instanceOf(Book).and(sinon.match.has("pages"));
  132. ```
  133. ### `match(object, matcher)`
  134. Creates a custom matcher to perform partial equality check. Compares `object`
  135. with matcher according a wide set of rules:
  136. #### String matcher
  137. In its simplest form, `match` performs a case insensitive substring match.
  138. When the matcher is a string, `object` is converted to a string, and the
  139. function returns `true` if the matcher is a case-insensitive substring of
  140. `object` as a string.
  141. ```javascript
  142. samsam.match("Give me something", "Give"); //true
  143. samsam.match("Give me something", "sumptn"); // false
  144. samsam.match(
  145. {
  146. toString: function () {
  147. return "yeah";
  148. },
  149. },
  150. "Yeah!"
  151. ); // true
  152. ```
  153. The last example is not symmetric. When the matcher is a string, the `object`
  154. is coerced to a string - in this case using `toString`. Changing the order of
  155. the arguments would cause the matcher to be an object, in which case different
  156. rules apply (see below).
  157. #### Boolean matcher
  158. Performs a strict (i.e. `===`) match with the object. So, only `true`
  159. matches `true`, and only `false` matches `false`.
  160. #### Regular expression matcher
  161. When the matcher is a regular expression, the function will pass if
  162. `object.test(matcher)` is `true`. `match` is written in a generic way, so
  163. any object with a `test` method will be used as a matcher this way.
  164. ```javascript
  165. samsam.match("Give me something", /^[a-z\s]$/i); // true
  166. samsam.match("Give me something", /[0-9]/); // false
  167. samsam.match(
  168. {
  169. toString: function () {
  170. return "yeah!";
  171. },
  172. },
  173. /yeah/
  174. ); // true
  175. samsam.match(234, /[a-z]/); // false
  176. ```
  177. #### Number matcher
  178. When the matcher is a number, the assertion will pass if `object == matcher`.
  179. ```javascript
  180. samsam.match("123", 123); // true
  181. samsam.match("Give me something", 425); // false
  182. samsam.match(
  183. {
  184. toString: function () {
  185. return "42";
  186. },
  187. },
  188. 42
  189. ); // true
  190. samsam.match(234, 1234); // false
  191. ```
  192. #### Function matcher
  193. When the matcher is a function, it is called with `object` as its only
  194. argument. `match` returns `true` if the function returns `true`. A strict
  195. match is performed against the return value, so a boolean `true` is required,
  196. truthy is not enough.
  197. ```javascript
  198. // true
  199. samsam.match("123", function (exp) {
  200. return exp == "123";
  201. });
  202. // false
  203. samsam.match("Give me something", function () {
  204. return "ok";
  205. });
  206. // true
  207. samsam.match(
  208. {
  209. toString: function () {
  210. return "42";
  211. },
  212. },
  213. function () {
  214. return true;
  215. }
  216. );
  217. // false
  218. samsam.match(234, function () {});
  219. ```
  220. #### Object matcher
  221. As mentioned above, if an object matcher defines a `test` method, `match`
  222. will return `true` if `matcher.test(object)` returns truthy.
  223. If the matcher does not have a test method, a recursive match is performed. If
  224. all properties of `matcher` matches corresponding properties in `object`,
  225. `match` returns `true`. Note that the object matcher does not care if the
  226. number of properties in the two objects are the same - only if all properties in
  227. the matcher recursively matches ones in `object`. If supported, this object matchers
  228. include [symbolic properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)
  229. in the comparison.
  230. ```javascript
  231. // true
  232. samsam.match("123", {
  233. test: function (arg) {
  234. return arg == 123;
  235. },
  236. });
  237. // false
  238. samsam.match({}, { prop: 42 });
  239. // true
  240. samsam.match(
  241. {
  242. name: "Chris",
  243. profession: "Programmer",
  244. },
  245. {
  246. name: "Chris",
  247. }
  248. );
  249. // false
  250. samsam.match(234, { name: "Chris" });
  251. ```
  252. #### DOM elements
  253. `match` can be very helpful when comparing DOM elements, because it allows
  254. you to compare several properties with one call:
  255. ```javascript
  256. var el = document.getElementById("myEl");
  257. samsam.match(el, {
  258. tagName: "h2",
  259. className: "item",
  260. innerHTML: "Howdy",
  261. });
  262. ```