Ticket #9611: alternate_enumerable_syntax_with_tests.diff
| File alternate_enumerable_syntax_with_tests.diff, 10.6 kB (added by jcoglan, 10 months ago) |
|---|
-
src/array.js
old new 110 110 if (value !== undefined) results.push(value); 111 111 }); 112 112 return '[' + results.join(', ') + ']'; 113 }, 114 115 toFunction: function() { 116 var method = this[0], args = this.slice(1); 117 if (!method) return Prototype.K; 118 return function(o) { 119 var fn = o[method]; 120 return (typeof fn == 'function') ? fn.apply(o, args) : undefined; 121 }; 113 122 } 114 123 }); 115 124 -
src/base.js
old new 196 196 197 197 Function.prototype.defer = Function.prototype.delay.curry(0.01); 198 198 199 Function.from = function(iterator) { 200 if (typeof iterator == 'function') return iterator; 201 if (iterator.toFunction) return iterator.toFunction(); 202 if (typeof iterator == 'object') return $H(iterator).toFunction(); 203 return Prototype.K; 204 }; 205 199 206 Date.prototype.toJSON = function() { 200 207 return '"' + this.getFullYear() + '-' + 201 208 (this.getMonth() + 1).toPaddedString(2) + '-' + -
src/enumerable.js
old new 23 23 }, 24 24 25 25 all: function(iterator, context) { 26 if (iterator) iterator = Function.from(iterator); 26 27 iterator = iterator ? iterator.bind(context) : Prototype.K; 27 28 var result = true; 28 29 this.each(function(value, index) { … … 33 34 }, 34 35 35 36 any: function(iterator, context) { 37 if (iterator) iterator = Function.from(iterator); 36 38 iterator = iterator ? iterator.bind(context) : Prototype.K; 37 39 var result = false; 38 40 this.each(function(value, index) { … … 43 45 }, 44 46 45 47 collect: function(iterator, context) { 48 if (iterator) iterator = Function.from(iterator); 46 49 iterator = iterator ? iterator.bind(context) : Prototype.K; 47 50 var results = []; 48 51 this.each(function(value, index) { … … 52 55 }, 53 56 54 57 detect: function(iterator, context) { 58 if (iterator) iterator = Function.from(iterator); 55 59 iterator = iterator.bind(context); 56 60 var result; 57 61 this.each(function(value, index) { … … 64 68 }, 65 69 66 70 findAll: function(iterator, context) { 71 if (iterator) iterator = Function.from(iterator); 67 72 iterator = iterator.bind(context); 68 73 var results = []; 69 74 this.each(function(value, index) { … … 125 130 }, 126 131 127 132 max: function(iterator, context) { 133 if (iterator) iterator = Function.from(iterator); 128 134 iterator = iterator ? iterator.bind(context) : Prototype.K; 129 135 var result; 130 136 this.each(function(value, index) { … … 136 142 }, 137 143 138 144 min: function(iterator, context) { 145 if (iterator) iterator = Function.from(iterator); 139 146 iterator = iterator ? iterator.bind(context) : Prototype.K; 140 147 var result; 141 148 this.each(function(value, index) { … … 147 154 }, 148 155 149 156 partition: function(iterator, context) { 157 if (iterator) iterator = Function.from(iterator); 150 158 iterator = iterator ? iterator.bind(context) : Prototype.K; 151 159 var trues = [], falses = []; 152 160 this.each(function(value, index) { … … 165 173 }, 166 174 167 175 reject: function(iterator, context) { 176 if (iterator) iterator = Function.from(iterator); 168 177 iterator = iterator.bind(context); 169 178 var results = []; 170 179 this.each(function(value, index) { … … 175 184 }, 176 185 177 186 sortBy: function(iterator, context) { 187 if (iterator) iterator = Function.from(iterator); 178 188 iterator = iterator.bind(context); 179 189 return this.map(function(value, index) { 180 190 return {value: value, criteria: iterator(value, index)}; -
src/hash.js
old new 104 104 105 105 toJSON: function() { 106 106 return Hash.toJSON(this); 107 }, 108 109 toFunction: function() { 110 var hash = this, keys = this.keys(); 111 if (keys.length === 0) return Prototype.K; 112 return function(o) { 113 var result = true, key, fn, args, op; 114 for (var i = 0, n = keys.length; i < n; i++) { 115 key = keys[i]; 116 fn = o[key]; args = hash[key]; 117 if (typeof fn == 'function' && !(args instanceof Array)) args = [args]; 118 result = result && ((typeof fn == 'function') ? fn.apply(o, args) : fn == args); 119 } 120 return result; 121 }; 107 122 } 108 123 }); 109 124 -
src/string.js
old new 112 112 toArray: function() { 113 113 return this.split(''); 114 114 }, 115 115 116 toFunction: function() { 117 var properties = this.split('.'); 118 if (!properties[0]) return Prototype.K; 119 return function(o) { 120 var object, member = o; 121 for (var i = 0, n = properties.length; i < n; i++) { 122 object = member; 123 member = object[properties[i]]; 124 if (typeof member == 'function') member = member.apply(object); 125 } 126 return member; 127 }; 128 }, 129 116 130 succ: function() { 117 131 return this.slice(0, this.length - 1) + 118 132 String.fromCharCode(this.charCodeAt(this.length - 1) + 1); -
test/unit/enumerable.html
old new 32 32 </tbody> 33 33 </table> 34 34 35 <div style="display: block;" class="finder"></div> 36 <div style="display: none;" class="finder"></div> 37 <div style="display: inline;" class="finder"></div> 38 35 39 <!-- Tests follow --> 36 40 <script type="text/javascript" language="javascript" charset="utf-8"> 37 41 // <![CDATA[ … … 45 49 46 50 Nicknames: $w('sam- noradio htonl Ulysses'), 47 51 52 Radios: [ 53 {checked: true}, {checked: false}, {checked: 1}, {checked: true}, 54 {checked: ''}, {checked: 17}, {checked: null}, {checked: 'foo'} 55 ], 56 57 Records: [ 58 {valid: function() { return this.name == 'Bob'; }, name: 'Mike'}, 59 {valid: function() { return this.name == 'Bob'; }, name: 'Bob'} 60 ], 61 62 Values: [{value: 12}, {value: -5}, {value: 9}, {value: 6}], 63 64 Nested: [{value: {integer: 12}}, {value: {integer: -5}}, {value: {integer: 9}}, {value: {integer: 6}}], 65 48 66 Basic: [1, 2, 3], 49 67 50 68 Primes: [ … … 122 140 assert(!Fixtures.Basic.any(function(value) { 123 141 return value > 5; 124 142 })); 143 assert(Fixtures.Radios.any('checked')); 144 assert(Fixtures.Records.any('valid')); 125 145 }}, 126 146 127 147 testAll: function() {with(this) { … … 137 157 assert(!Fixtures.Basic.all(function(value) { 138 158 return value > 1; 139 159 })); 160 assert([{checked: true}, {checked: 12}, {checked: 'foo'}].all('checked')); 161 assert(!Fixtures.Radios.all('checked')); 162 assert(!Fixtures.Records.all('valid')); 140 163 }}, 141 164 142 165 testCollect: function() {with(this) { … … 146 169 }).join(", ")); 147 170 148 171 assertEqual(26, Fixtures.Primes.map().length); 172 173 assertEqual(Fixtures.Nicknames.join(', '), 174 Fixtures.People.collect('nickname').join(', ')); 175 176 assertEqual(Fixtures.Values.pluck('value').join(', '), 177 Fixtures.Nested.collect('value.integer').join(', ')); 178 179 var set = Fixtures.Values.collect(function(v) { 180 return { 181 value: function() { return {quantity: this.k}; }, 182 k: v.value 183 }; 184 }); 185 assertEqual(Fixtures.Values.pluck('value').join(', '), 186 set.collect('value.quantity').join(', ')); 187 188 assertEqual('plums, apples, oranges', 189 ['apples', 'oranges', 'plums'].sortBy({replace: [/^./, '']}).join(', ')); 149 190 }}, 150 191 151 192 testDetect: function() {with(this) { … … 153 194 Fixtures.People.detect(function(person) { 154 195 return person.nickname.match(/no/); 155 196 }).name); 197 assertEqual(true, Fixtures.Radios.detect('checked').checked); 198 assertEqual('Bob', Fixtures.Records.detect('valid').name); 156 199 }}, 157 200 158 201 testEachSlice: function() {with(this) { … … 181 224 testFindAll: function() {with(this) { 182 225 assertEqual(Fixtures.Primes.join(', '), 183 226 Fixtures.Z.findAll(prime).join(', ')); 227 assertEqual(5, Fixtures.Radios.findAll('checked').length); 228 assertEqual(2, $$('div.finder').findAll('visible').length); 229 230 assertEqual(3, $$('div').findAll(['hasClassName', 'finder']).length); 231 assertEqual(3, $$('div').findAll({hasClassName: ['finder']}).length); 232 assertEqual(2, $$('div').findAll({hasClassName: 'finder', visible: true}).length); 233 assertEqual(3, $$('div').findAll({hasClassName: ['finder'], tagName: 'DIV'}).length); 234 assertEqual(0, $$('div').findAll({hasClassName: ['finder'], tagName: 'SPAN'}).length); 184 235 }}, 185 236 186 237 testGrep: function() {with(this) { … … 257 308 assertEqual(97, Fixtures.Primes.max()); 258 309 assertEqual(2, [ -9, -8, -7, -6, -4, -3, -2, 0, -1, 2 ].max()); 259 310 assertEqual('sam-', Fixtures.Nicknames.max()); // ?s > ?U 311 assertEqual(12, Fixtures.Values.max('value')); 260 312 }}, 261 313 262 314 testMin: function() {with(this) { 263 315 assertEqual(1, Fixtures.Z.min()); 264 316 assertEqual(0, [ 1, 2, 3, 4, 5, 6, 7, 8, 0, 9 ].min()); 265 317 assertEqual('Ulysses', Fixtures.Nicknames.min()); // ?U < ?h 318 assertEqual(-5, Fixtures.Values.min('value')); 266 319 }}, 267 320 268 321 testPartition: function() {with(this) { … … 273 326 assertEqual(2, result.length); 274 327 assertEqual('sam-, htonl', result[0].join(', ')); 275 328 assertEqual('noradio, Ulysses', result[1].join(', ')); 329 330 result = Fixtures.Radios.partition('checked'); 331 assertEqual(5, result[0].length); 332 assertEqual(3, result[1].length); 276 333 }}, 277 334 278 335 testPluck: function() {with(this) { … … 284 341 assertEqual(0, 285 342 Fixtures.Nicknames.reject(Prototype.K).length); 286 343 344 assertEqual(3, 345 Fixtures.Radios.reject('checked').length); 346 347 assertEqual(1, 348 Fixtures.Records.reject('valid').length); 349 287 350 assertEqual('sam-, noradio, htonl', 288 351 Fixtures.Nicknames.reject(function(nickname) { 289 352 return nickname != nickname.toLowerCase(); … … 295 358 Fixtures.People.sortBy(function(value) { 296 359 return value.nickname.toLowerCase(); 297 360 }).pluck('nickname').join(', ')); 361 362 assertEqual('-5, 6, 9, 12', 363 Fixtures.Values.sortBy('value').pluck('value').join(', ')); 298 364 }}, 299 365 300 366 testToArray: function() {with(this) {