Skip to content
Myr

Updated ext and removed Collection

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent 0894cfe · Browse files · 3 files+0 added−539 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+0 added−539 removed

  • other:+0 added−539 removed

3 files+0 added−539 removed

deleted:lib/express/collection.js-538 removed
12// Express - Collection - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34/**5 * Module dependencies.6 */7 8var utils = require('express/utils')910/**11 * Throw $break in order to stop iteration.12 */1314$break = '__break__'1516var property = /^\w+$/,17 method = /^\w+\(/1819/**20 * Normalize callback _fn_. When a string is21 * passed convert the shorthand expr to a function.22 *23 * - Functions are passed through un-touched24 * - Strings with length of < 4 are considered operators between a and b25 * - Single words are considered properties on a26 * - Single functions are considered method calls on a27 * - Larger strings are considered return expressions28 *29 * @param {string, function} fn30 * @return {function}31 * @api private32 */3334function callback(fn) {35 if (fn === undefined) return36 if (fn instanceof Function) return fn37 if (fn.length < 4) return Function('a, b, c', 'return a ' + fn + ' b')38 if (property.test(fn) || method.test(fn)) fn = 'a.' + fn39 return Function('a, b, c', 'return ' + fn)40}4142// --- Collection4344Collection = Class({45 46 /**47 * Initialize collection with an array-like object.48 *49 * @param {object} arr50 * @api private51 */52 53 init: function(arr) {54 this.arr = arr || []55 },56 57 /**58 * Return the value of _index_ or null.59 *60 * @param {int} index61 * @return {mixed}62 * @api public63 */64 65 at: function(index) {66 if ('length' in this.arr)67 return this.arr[index]68 var result, i = 069 this.each(function(val){70 if (i++ == index) {71 result = val72 throw $break73 }74 })75 return result76 },77 78 /**79 * Iterate collection using callback _fn_,80 * passing both the value and index.81 *82 * @param {function} fn83 * @return {Collection}84 * @api public85 */86 87 each: function(fn) {88 try {89 if (this.arr.forEach)90 this.arr.forEach(fn)91 else92 for (var key in this.arr)93 if (this.arr.hasOwnProperty(key))94 fn(this.arr[key], key)95 }96 catch (e) {97 if (e != $break) throw e98 }99 return this100 },101 102 /**103 * Reverse a collection.104 *105 * @return {Collection}106 * @api public107 */108 109 reverse: function() {110 if (this.arr.reverse)111 return $(this.arr.reverse())112 return this113 },114 115 /**116 * Join a collection with the given _str_ or ''.117 *118 * @param {string} str119 * @return {string}120 * @api public121 */122 123 join: function(str) {124 return this.toArray().join(str || '')125 },126 127 /**128 * Sort collection with optional _fn_.129 *130 * @param {function} fn131 * @return {Collection}132 * @warn converts to array133 * @api public134 */135 136 sort: function(fn) {137 fn = callback(fn)138 return $(this.toArray().sort(fn))139 },140 141 /**142 * Iterate with _memo_ using callback _fn_.143 * The _memo_ object is passed as the first 144 * argument, and the return value of _fn_ becomes145 * the value of _memo_ providing a functional 146 * approach to reducing a collection.147 *148 * @param {mixed} memo149 * @param {function} fn150 * @return {mixed}151 * @api public152 */153 154 reduce: function(memo, fn) {155 fn = callback(fn)156 if (this.arr.reduce)157 return this.arr.reduce(fn, memo)158 this.each(function(val, key){159 memo = fn(memo, val, key)160 })161 return memo162 },163 164 /**165 * Map using callback _fn_, returning a 166 * new collection of return values.167 *168 * @param {function} fn169 * @return {Collection}170 * @api public171 */172 173 map: function(fn) {174 fn = callback(fn)175 if (this.arr.map)176 return $(this.arr.map(fn))177 return $(this.reduce([], function(array, val, key){178 array.push(fn(val, key))179 return array180 }))181 },182 183 /**184 * Return collection of values when _fn_ evaluates185 * to true.186 *187 * @param {function} fn188 * @return {Collection}189 * @api public190 */191 192 select: function(fn) {193 fn = callback(fn)194 return $(this.reduce([], function(array, val, key){195 if (fn(val, key))196 array.push(val)197 return array198 }))199 },200 201 /**202 * Return collection of values when _fn_ evaluates203 * to false.204 *205 * @param {function} fn206 * @return {Collection}207 * @api public208 */209 210 reject: function(fn) {211 fn = callback(fn)212 return $(this.reduce([], function(array, val, key){213 if (!fn(val, key))214 array.push(val)215 return array216 }))217 },218 219 /**220 * Check if all arguments passed are found within 221 * the collection using the === operator.222 *223 * @return {bool}224 * @api public225 */226 227 includes: function() {228 var self = this229 return $(arguments).all(function(arg){230 return self.any(function(val){231 return val === arg232 })233 })234 },235 236 /**237 * Return the first _n_ value(s), defaults to 1.238 *239 * @param {int} n240 * @return {mixed}241 * @api public242 */243 244 first: function(n) {245 return n ? 246 this.slice(0, n) :247 this.at(0)248 },249 250 /**251 * Return the last _n_ value(s), defaults to -1.252 *253 * @param {int} n254 * @return {mixed}255 * @api public256 */257 258 last: function(n) {259 var len = this.length()260 return n ? 261 this.slice(len - n, len) :262 this.at(--len)263 },264 265 /**266 * Drop the first _n_ values, returning the others.267 *268 * @param {int} n269 * @return {Collection}270 * @api public271 */272 273 drop: function(n) {274 return this.slice(n, this.length())275 },276 277 /**278 * Return a slice of values from _start_ to _end_.279 *280 * @param {int} start281 * @param {int} end282 * @return {Collection}283 * @api public284 */285 286 slice: function(start, end) {287 if (this.arr.slice)288 return $(this.arr.slice(start, end))289 var i = 0290 return $(this.reduce([], function(array, val){291 if (i++ >= start)292 if (i <= end)293 array.push(val)294 else295 throw $break296 return array297 }))298 },299 300 /**301 * Iterate until _fn_ evaluates to true, then302 * return the matching value.303 *304 * @param {function} fn305 * @return {mixed}306 * @api public307 */308 309 find: function(fn) {310 var result, fn = callback(fn)311 this.each(function(val, key){312 if (fn(val, key)) {313 result = val314 throw $break315 }316 })317 return result318 },319 320 /**321 * Return true if _fn_ ever evaluates to true, otherwise322 * returns false.323 *324 * @param {function} fn325 * @return {bool}326 * @api public327 */328 329 any: function(fn) {330 fn = callback(fn)331 if (this.arr.some)332 return this.arr.some(fn)333 return !! this.find(fn)334 },335 336 /**337 * Returns true if _fn_ always evaluates to true, otherwise338 * returns false.339 *340 * @param {function} fn341 * @return {bool}342 * @api public343 */344 345 all: function(fn) {346 fn = callback(fn)347 if (this.arr.every)348 return this.arr.every(fn)349 return this.reduce(true, function(state, val, key){350 if (!state) throw $break351 return !! fn(val, key)352 })353 },354 355 /**356 * Select values matching _pattern_357 *358 * @param {regexp} pattern359 * @return {Collection}360 * @api public361 */362 363 grep: function(pattern) {364 return this.select(function(val){365 return pattern.exec(val)366 })367 },368 369 /**370 * Convert a collection to a true array.371 * Works recursively372 *373 * @return {array}374 * @api public375 */376 377 toArray: function() {378 return this.reduce([], function(array, val){379 val instanceof Collection ?380 array.push(val.toArray()) :381 array.push(val)382 return array383 })384 },385 386 /**387 * Return the lowest number in the collection.388 *389 * @return {number}390 * @api public391 */392 393 min: function() {394 return this.reduce(null, function(min, val){395 return min === null ? val :396 val < min ? val :397 min398 })399 },400 401 /**402 * Return the largest number in the collection.403 *404 * @return {number}405 * @api public406 */407 408 max: function() {409 return this.reduce(null, function(max, val){410 return max === null ? val :411 val > max ? val :412 max413 })414 },415 416 /**417 * Group collection into several collections grouped by _size_.418 *419 * @param {int} size420 * @return {Collection}421 * @api public422 */423 424 chunk: function(size) {425 var len = this.arr.length, 426 chunks = [], chunk = [], i = 0 427 this.each(function(val, key){428 chunk.push(val)429 if (i++ > 0 && (i % size == 0 || i == len))430 chunks.push($(chunk)),431 chunk = []432 })433 return $(chunks)434 },435 436 /**437 * Return the length of this collection.438 *439 * @return {int}440 * @api public441 */442 443 length: function() {444 if ('length' in this.arr)445 return this.arr.length446 return this.reduce(0, function(len){447 return ++len448 })449 },450 451 /**452 * Return the sum of numeric values in this collection.453 *454 * @return {number}455 * @api public456 */457 458 sum: function() {459 return this.reduce(0, function(sum, n){460 return sum + n461 })462 },463 464 /**465 * Return the average of numeric values in this collection.466 *467 * @return {number}468 * @api public469 */470 471 avg: function() {472 return this.sum() / this.length()473 },474 475 /**476 * Merge _other_ with this collection.477 *478 * @param {mixed} other479 * @return {Collection}480 * @api public481 */482 483 merge: function(other) {484 if (this.arr instanceof Array)485 return $($(other).reduce(this.arr, function(array, val){486 array.push(val)487 return array488 }))489 else490 return $(utils.mixin(this.arr, $(other).arr))491 },492 493 /**494 * Clone the collection.495 *496 * @return {Collection}497 * @api public498 */499 500 clone: function() {501 return $(utils.clone(this.arr))502 },503 504 /**505 * Return the value of a random index.506 *507 * @return {mixed}508 * @api public509 */510 511 sample: function() {512 return this.at(Math.floor(Math.random() * this.length()))513 },514 515 /**516 * Convert collection to a string.517 *518 * @return {string}519 * @api public520 */521 522 toString: function() {523 return '[Collection ' + this.arr + ']'524 }525})526527/**528 * Create a new collection from an array-like object.529 *530 * @param {object} arr531 * @return {Collection}532 * @api public533 */534535var $ = exports.$ = function(arr) {536 if (arr instanceof Collection) return arr537 return new Collection(arr)538}
modified:lib/express/core.js-1 removed
10 unmodified lines
11 fs = require('fs')1213utils.mixin(require('sys'))14utils.mixin(require('express/exceptions'))15utils.mixin(require('express/collection'))15utils.mixin(require('express/event'))16utils.mixin(require('express/request'))17utils.mixin(require('express/plugin'))18utils.mixin(require('express/dsl'))
317 unmodified lines
modified:lib/support/extnot shown

Its text is not shown, modified.