Prototype source code analysis-enumerable
Keywords: Prototype
Js Code
- Var $ break = new Object (); // The break Object can be used in comparison with java exception.
- Var $ continue = new Object (); // The Object indicating the continue can be used in comparison with the exception of java
- Var Enumerable = {
- Each: function (iterator) {// execute the _ each function
- Var index = 0;
- Try {
- This. _ each (function (value ){
- Try {
- Iterator (value, index ++ );
- } Catch (e ){
- If (E! = $ Continue) Throw E;
- }
- });
- } Catch (e ){
- If (E! = $ Break) Throw E;
- }
- },
- // Boolean returns the summary result after function traversal. If no false value is returned for each execution, true is returned.
- ALL: function (iterator ){
- Var result = true;
- This. each (function (value, index ){
- Result = result &&!! (Iterator | Prototype. K) (value, index );
- If (! Result) throw $ break;
- });
- Return result;
- },
- // Boolean returns the result of the function traversal. If the result is not false, true is returned.
- Any: function (iterator ){
- Var result = true;
- This. each (function (value, index ){
- If (result = !! (Iterator | prototype. K) (value, index ))
- Throw $ break;
- });
- Return result;
- },
- // Result array puts the result after function traversal into an array and returns
- Collect: function (iterator ){
- VaR Results = [];
- This. Each (function (value, index ){
- Results. Push (iterator (value, index ));
- });
- Return results;
- },
- // Return the value of the non-false parameter in the any function.
- Detect: function (iterator ){
- Var result;
- This. each (function (value, index ){
- If (iterator (value, index )){
- Result = value;
- Throw $ break;
- }
- });
- Return result;
- },
- // Filter the collect function. Only the value of the function parameter that executes the non-false function enters the result array.
- Findall: function (iterator ){
- VaR Results = [];
- This. Each (function (value, index ){
- If (iterator (value, index ))
- Results. Push (value );
- });
- Return results;
- },
- // Filter the collect function. Only when the execution value conforms to pattern can the execution result be entered into the result array.
- Grep: function (pattern, iterator ){
- Var results = [];
- This. each (function (value, index ){
- Var stringValue = value. toString ();
- If (stringValue. match (pattern ))
- Results. push (iterator | Prototype. K) (value, index ));
- })
- Return results;
- },
- // Whether boolean is a member of the value Array
- Include: function (object ){
- Var found = false;
- This. each (function (value ){
- If (value = object ){
- Found = true;
- Throw $ break;
- }
- });
- Return found;
- },
- // Calculates the sum of inject injection results.
- Inject: function (memo, iterator ){
- This. each (function (value, index ){
- Memo = iterator (memo, value, index );
- });
- Return memo;
- },
- // Pass the function + required parameters for traversal and execution and return the execution result set
- Invoke: function (method ){
- Var args = $ A (arguments). slice (1 );
- Return this. collect (function (value ){
- Return value [method]. apply (value, args );
- });
- },
- // Obtain the maximum return value after function traversal.
- Max: function (iterator ){
- Var result;
- This. each (function (value, index ){
- Value = (iterator | Prototype. K) (value, index );
- If (value> = (result | value ))
- Result = value;
- });
- Return result;
- },
- // Obtain the minimum returned value after function traversal.
- Min: function (iterator ){
- VaR result;
- This. Each (function (value, index ){
- Value = (iterator | prototype. K) (value, index );
- If (value <= (result | value ))
- Result = value;
- });
- Return result;
- },
- // Return the execution result according to true and non-true as a two-dimensional array
- Partition: function (iterator ){
- VaR Trues = [], falses = [];
- This. each (function (value, index ){
- (Iterator | Prototype. K) (value, index )?
- Trues: falses). push (value );
- });
- Return [trues, falses];
- },
- // Retrieve the attribute value of each element from the traversal array and put it into the result array.
- Pluck: function (property ){
- Var results = [];
- This. each (function (value, index ){
- Results. push (value [property]);
- });
- Return results;
- },
- // Returns the combination of non-real elements in the iterator result of the traversal execution function.
- Reject: function (iterator ){
- VaR Results = [];
- This. Each (function (value, index ){
- If (! Iterator (value, index ))
- Results. Push (value );
- });
- Return results;
- },
- // Return the sorting result set after the sorting function is executed.
- SortBy: function (iterator ){
- Return this. collect (function (value, index ){
- Return {value: value, criteria: iterator (value, index )};
- }). Sort (function (left, right ){
- Var a = left. criteria, B = right. criteria;
- Return a <B? -1: a> B? 1: 0;
- }). Pluck ('value ');
- },
- // You can traverse the set and convert it into a js array.
- ToArray: function (){
- Return this. collect (Prototype. K );
- },
- // Zip Compression
- Zip: function (){
- Var iterator = Prototype. K, args = $ A (arguments );
- If (typeof args. last () = 'function ')
- Iterator = args. pop ();
- Var collections = [this]. concat (args). map ($ );
- Return this. map (function (value, index ){
- Iterator (value = collections. pluck (index ));
- Return value;
- });
- },
- // View similar tostring
- Inspect: function (){
- Return '# this. toArray (). inspect () +'> ';
- }
- }
- Object. extend (Enumerable ,{
- Map: Enumerable. collect,
- Find: Enumerable. detect,
- Select: Enumerable. findAll,
- Member: Enumerable. include,
- Entries: Enumerable. toArray
- });