Ohm-Management - Projektarbeit B-ME
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.

README.md 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. ## validate objects & filter arrays with mongodb queries
  2. [![Build Status](https://secure.travis-ci.org/crcn/sift.js.png)](https://secure.travis-ci.org/crcn/sift.js)
  3. <!-- [![Coverage Status](https://coveralls.io/repos/crcn/sift.js/badge.svg)](https://coveralls.io/r/crcn/sift.js) -->
  4. <!-- [![Join the chat at https://gitter.im/crcn/sift.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/crcn/sift.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -->
  5. **For extended documentation, checkout http://docs.mongodb.org/manual/reference/operator/query/**
  6. ## Features:
  7. - Supported operators: [$in](#in), [$nin](#nin), [$exists](#exists), [$gte](#gte), [$gt](#gt), [$lte](#lte), [$lt](#lt), [$eq](#eq), [$ne](#ne), [$mod](#mod), [$all](#all), [$and](#and), [$or](#or), [$nor](#nor), [$not](#not), [$size](#size), [$type](#type), [$regex](#regex), [$where](#where), [$elemMatch](#elemmatch)
  8. - Regexp searches
  9. - Function filtering
  10. - sub object searching
  11. - dot notation searching
  12. - Supports node.js, and web
  13. - Small (2 kb minified) library
  14. - Custom Expressions
  15. - filtering of immutable datastructures
  16. ## Node.js Examples
  17. ```javascript
  18. import sift from 'sift';
  19. //intersecting arrays
  20. var sifted = sift({ $in: ['hello','world'] }, ['hello','sifted','array!']); //['hello']
  21. //regexp filter
  22. var sifted = sift(/^j/, ['craig','john','jake']); //['john','jake']
  23. //A *sifter* is returned if the second parameter is omitted
  24. var testQuery = sift({
  25. //you can also filter against functions
  26. name: function(value) {
  27. return value.length == 5;
  28. }
  29. });
  30. //filtered: [{ name: 'craig' }]
  31. [{
  32. name: 'craig',
  33. },
  34. {
  35. name: 'john'
  36. },
  37. {
  38. name: 'jake'
  39. }].filter(testQuery);
  40. //you can test *single values* against your custom sifter
  41. testQuery({ name: 'sarah' }); //true
  42. testQuery({ name: 'tim' }); //false\
  43. ```
  44. ## Browser Examples
  45. ```html
  46. <html>
  47. <head>
  48. <script src="https://raw.github.com/crcn/sift.js/master/sift.min.js" type="text/javascript"></script>
  49. <script type="text/javascript">
  50. //regexp filter
  51. var sifted = sift(/^j/, ['craig','john','jake']); //['john','jake']
  52. </script>
  53. </head>
  54. <body>
  55. </body>
  56. </html>
  57. ```
  58. ## API
  59. ### .sift(filter[, array][, selectorFn])
  60. - `filter` - the filter to use against the target array
  61. - `array` - sifts against target array. Without this, a function is returned
  62. - `selectorFn` - selector for the values within the array.
  63. With an array:
  64. ```javascript
  65. sift({$exists:true}, ['craig',null]); //['craig']
  66. ```
  67. Without an array, a sifter is returned:
  68. ```javascript
  69. var siftExists = sift({$exists:true});
  70. siftExists('craig'); //true
  71. siftExists(null); //false
  72. ['craig',null].filter(siftExists); //['craig']
  73. ```
  74. With a selector:
  75. ```javascript
  76. var sifter = sift({$exists:true}, function(user) {
  77. return !!user.name;
  78. });
  79. sifter([
  80. {
  81. name: "Craig"
  82. },
  83. {
  84. name: null
  85. }
  86. ])
  87. ```
  88. With your sifter, you can also **test** values:
  89. ```javascript
  90. siftExists(null); //false
  91. siftExists('craig'); //true
  92. ```
  93. ## Supported Operators:
  94. See MongoDB's [advanced queries](http://www.mongodb.org/display/DOCS/Advanced+Queries) for more info.
  95. ### $in
  96. array value must be *$in* the given query:
  97. Intersecting two arrays:
  98. ```javascript
  99. //filtered: ['Brazil']
  100. sift({ $in: ['Costa Rica','Brazil'] }, ['Brazil','Haiti','Peru','Chile']);
  101. ```
  102. Here's another example. This acts more like the $or operator:
  103. ```javascript
  104. sift({ location: { $in: ['Costa Rica','Brazil'] } }, [ { name: 'Craig', location: 'Brazil' } ]);
  105. ```
  106. ### $nin
  107. Opposite of $in:
  108. ```javascript
  109. //filtered: ['Haiti','Peru','Chile']
  110. sift({ $nin: ['Costa Rica','Brazil'] }, ['Brazil','Haiti','Peru','Chile']);
  111. ```
  112. ### $exists
  113. Checks if whether a value exists:
  114. ```javascript
  115. //filtered: ['Craig','Tim']
  116. sift({ $exists: true }, ['Craig',null,'Tim']);
  117. ```
  118. You can also filter out values that don't exist
  119. ```javascript
  120. //filtered: [{ name: 'Craig', city: 'Minneapolis' }]
  121. sift({ city: { $exists: false } }, [ { name: 'Craig', city: 'Minneapolis' }, { name: 'Tim' }]);
  122. ```
  123. ### $gte
  124. Checks if a number is >= value:
  125. ```javascript
  126. //filtered: [2, 3]
  127. sift({ $gte: 2 }, [0, 1, 2, 3]);
  128. ```
  129. ### $gt
  130. Checks if a number is > value:
  131. ```javascript
  132. //filtered: [3]
  133. sift({ $gt: 2 }, [0, 1, 2, 3]);
  134. ```
  135. ### $lte
  136. Checks if a number is <= value.
  137. ```javascript
  138. //filtered: [0, 1, 2]
  139. sift({ $lte: 2 }, [0, 1, 2, 3]);
  140. ```
  141. ### $lt
  142. Checks if number is < value.
  143. ```javascript
  144. //filtered: [0, 1]
  145. sift({ $lt: 2 }, [0, 1, 2, 3]);
  146. ```
  147. ### $eq
  148. Checks if `query === value`. Note that **$eq can be omitted**. For **$eq**, and **$ne**
  149. ```javascript
  150. //filtered: [{ state: 'MN' }]
  151. sift({ state: {$eq: 'MN' }}, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]);
  152. ```
  153. Or:
  154. ```javascript
  155. //filtered: [{ state: 'MN' }]
  156. sift({ state: 'MN' }, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]);
  157. ```
  158. ### $ne
  159. Checks if `query !== value`.
  160. ```javascript
  161. //filtered: [{ state: 'CA' }, { state: 'WI'}]
  162. sift({ state: {$ne: 'MN' }}, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]);
  163. ```
  164. ### $mod
  165. Modulus:
  166. ```javascript
  167. //filtered: [300, 600]
  168. sift({ $mod: [3, 0] }, [100, 200, 300, 400, 500, 600]);
  169. ```
  170. ### $all
  171. values must match **everything** in array:
  172. ```javascript
  173. //filtered: [ { tags: ['books','programming','travel' ]} ]
  174. sift({ tags: {$all: ['books','programming'] }}, [
  175. { tags: ['books','programming','travel' ] },
  176. { tags: ['travel','cooking'] } ]);
  177. ```
  178. ### $and
  179. ability to use an array of expressions. All expressions must test true.
  180. ```javascript
  181. //filtered: [ { name: 'Craig', state: 'MN' }]
  182. sift({ $and: [ { name: 'Craig' }, { state: 'MN' } ] }, [
  183. { name: 'Craig', state: 'MN' },
  184. { name: 'Tim', state: 'MN' },
  185. { name: 'Joe', state: 'CA' } ]);
  186. ```
  187. ### $or
  188. OR array of expressions.
  189. ```javascript
  190. //filtered: [ { name: 'Craig', state: 'MN' }, { name: 'Tim', state: 'MN' }]
  191. sift({ $or: [ { name: 'Craig' }, { state: 'MN' } ] }, [
  192. { name: 'Craig', state: 'MN' },
  193. { name: 'Tim', state: 'MN' },
  194. { name: 'Joe', state: 'CA' } ]);
  195. ```
  196. ### $nor
  197. opposite of or:
  198. ```javascript
  199. //filtered: [ { name: 'Tim', state: 'MN' }, { name: 'Joe', state: 'CA' }]
  200. sift({ $nor: [ { name: 'Craig' }, { state: 'MN' } ] }, [
  201. { name: 'Craig', state: 'MN' },
  202. { name: 'Tim', state: 'MN' },
  203. { name: 'Joe', state: 'CA' } ]);
  204. ```
  205. ### $size
  206. Matches an array - must match given size:
  207. ```javascript
  208. //filtered: ['food','cooking']
  209. sift({ tags: { $size: 2 } }, [ { tags: ['food','cooking'] }, { tags: ['traveling'] }]);
  210. ```
  211. ### $type
  212. Matches a values based on the type
  213. ```javascript
  214. sift({ $type: Date }, [new Date(), 4342, 'hello world']); //returns single date
  215. sift({ $type: String }, [new Date(), 4342, 'hello world']); //returns ['hello world']
  216. ```
  217. ### $regex
  218. Matches values based on the given regular expression
  219. ```javascript
  220. sift({ $regex: /^f/i, $nin: ["frank"] }, ["frank", "fred", "sam", "frost"]); // ["fred", "frost"]
  221. sift({ $regex: "^f", $options: "i", $nin: ["frank"] }, ["frank", "fred", "sam", "frost"]); // ["fred", "frost"]
  222. ```
  223. ### $where
  224. Matches based on some javascript comparison
  225. ```javascript
  226. sift({ $where: "this.name === 'frank'" }, [{name:'frank'},{name:'joe'}]); // ["frank"]
  227. sift({
  228. $where: function() {
  229. return this.name === "frank"
  230. }
  231. }, [{name:'frank'},{name:'joe'}]); // ["frank"]
  232. ```
  233. ### $elemMatch
  234. Matches elements of array
  235. ```javascript
  236. var bills = [{
  237. month: 'july',
  238. casts: [{
  239. id: 1,
  240. value: 200
  241. },{
  242. id: 2,
  243. value: 1000
  244. }]
  245. },
  246. {
  247. month: 'august',
  248. casts: [{
  249. id: 3,
  250. value: 1000,
  251. }, {
  252. id: 4,
  253. value: 4000
  254. }]
  255. }];
  256. var result = sift({
  257. casts: {$elemMatch:{
  258. value: {$gt: 1000}
  259. }}
  260. }, bills); // {month:'august', casts:[{id:3, value: 1000},{id: 4, value: 4000}]}
  261. ```
  262. ### $not
  263. Not expression:
  264. ```javascript
  265. sift({$not:{$in:['craig','tim']}}, ['craig','tim','jake']); //['jake']
  266. sift({$not:{$size:5}}, ['craig','tim','jake']); //['tim','jake']
  267. ```
  268. ## sub object Searching
  269. ```javascript
  270. var people = [{
  271. name: 'craig',
  272. address: {
  273. city: 'Minneapolis'
  274. }
  275. },
  276. {
  277. name: 'tim',
  278. address: {
  279. city: 'St. Paul'
  280. }
  281. }];
  282. var sifted = sift({ address: { city: 'Minneapolis' }}, people); // count = 1
  283. //or
  284. var sifted = sift({'address.city': 'minneapolis'}, people);//count = 1
  285. ```
  286. ## Get index of first matching element
  287. Get the index (0-based) of first matching element in target array. Returns `-1` if no match is found.
  288. ```javascript
  289. import {indexOf as siftIndexOf} from 'sift';
  290. var people = [{
  291. name: 'craig',
  292. address: {
  293. city: 'Minneapolis'
  294. }
  295. },
  296. {
  297. name: 'tim',
  298. address: {
  299. city: 'St. Paul'
  300. }
  301. }];
  302. var index = siftIndexOf({ address: { city: 'Minneapolis' }}, people); // index = 0
  303. ```