From: dsc Date: Tue, 7 Dec 2010 11:46:45 +0000 (-0800) Subject: Fixes map,forEach in YString. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=18968ec9f7b24021dcd77b757b187931cff2fe28;p=tanks.git Fixes map,forEach in YString. --- diff --git a/src/Y/types/collection.cjs b/src/Y/types/collection.cjs index 529a329..b8db902 100644 --- a/src/Y/types/collection.cjs +++ b/src/Y/types/collection.cjs @@ -44,17 +44,21 @@ YBase.subclass('YCollection', { return acc; }, - 'map' : function map( fn, context ){ - var o = this._o, acc = new o.constructor(); + 'map' : function map( fn ){ + var o = this._o + , cxt = arguments[1] || this + , acc = new o.constructor() + ; for ( var name in o ) - acc[name] = fn.call( context || this, o[name], name, o ); + acc[name] = fn.call(cxt, o[name], name, o); return acc; }, 'forEach' : function forEach( fn, context ){ - var o = this._o; + var o = this._o, cxt = arguments[1] || this; for ( var name in o ) - fn.call( context || this, o[name], name, o ); + fn.call(cxt, o[name], name, o); + return this; }, 'filter' : function filter( fn, context ){ diff --git a/src/Y/types/string.cjs b/src/Y/types/string.cjs index 7edcac7..1d57932 100644 --- a/src/Y/types/string.cjs +++ b/src/Y/types/string.cjs @@ -26,11 +26,15 @@ YCollection.subclass('YString', function(YString){ } core.extend(this, { - init : function init(o){ + 'init' : function init(o){ if (!o) o = ""; this._o = o; }, + 'clone' : function clone(){ + return YString(this._o); + }, + /** * As Array.slice -- replaces `howMany` elements starting at `idx` * with the concatenation of the rest of the arguments. @@ -39,7 +43,7 @@ YCollection.subclass('YString', function(YString){ * * Invalid values for `howMany` will be replaced with 0. */ - splice : function splice(idx, howMany){ + 'splice' : function splice(idx, howMany){ var s = this._o; idx = (idx < 0 ? s.length+idx : idx); @@ -59,20 +63,20 @@ YCollection.subclass('YString', function(YString){ * * Negative values for start and/or end are allowed. */ - mutate : function mutate(start, end){ + 'mutate' : function mutate(start, end){ // var args = slice.call(arguments); arguments[1] = end-start; return this.splice.apply(this, arguments); }, - strip: function strip(){ + 'strip': function strip(){ return this._o.replace(/(^\s+|\s+$)/g, ''); }, - trim: trim, - ltrim: trim, + 'trim' : trim, + 'ltrim' : trim, - rtrim: function rtrim(val){ + 'rtrim' : function rtrim(val){ var s = this._o+''; val = val+''; @@ -85,26 +89,27 @@ YCollection.subclass('YString', function(YString){ return s; }, - startsWith: function startsWith(val){ + 'startsWith' : function startsWith(val){ return this._o.indexOf(val) === 0; }, - endsWith: function endsWith(val){ + 'endsWith' : function endsWith(val){ var s = this._o; return s.lastIndexOf(val) === (s.length - val.length); }, - compare : function compare(n){ + 'compare' : function compare(n){ var m = this._o; return (m > n ? 1 : (m < n ? -1 : 0 )); }, - toString: function(){ + 'toString' : function(){ return this._o; } }); - this.set = function set(key, value, def){ + this['set'] = + function set(key, value, def){ var s = this._o, _val = (value !== undefined ? value : def); if ( isNumber(key) ) @@ -118,7 +123,8 @@ YCollection.subclass('YString', function(YString){ return this; }; - this.attr = function attr(key, value, def){ + this['attr'] = + function attr(key, value, def){ var s = this._o; // set if ( value !== undefined || def !== undefined ) @@ -136,11 +142,28 @@ YCollection.subclass('YString', function(YString){ }; this.attr.__wraps__ = del.attr; - this.reduce = function reduce(fn, acc){ + this['reduce'] = + function reduce(fn, acc){ fn = Function.toFunction(fn); for ( var s = this._o, cxt = arguments[2]||this, i = 0, L = s.length; i < L; ++i ) acc = fn.call(cxt, acc, s.charAt(i), i, s); return acc; }; + this['map'] = + function map(fn){ + fn = Function.toFunction(fn); + for ( var s = this._o, L = s.length, cxt = arguments[1]||this, acc = new Array(L), i = 0; i < L; ++i ) + acc[i] = fn.call(cxt, s.charAt(i), i, s); + return acc; + }; + + this['forEach'] = + function forEach(fn){ + fn = Function.toFunction(fn); + for ( var s = this._o, L = s.length, cxt = arguments[1]||this, i = 0; i < L; ++i ) + fn.call(cxt, s.charAt(i), i, s); + return this; + }; + }); diff --git a/src/tanks/config.cjs b/src/tanks/config.cjs index 89a2464..953ca74 100644 --- a/src/tanks/config.cjs +++ b/src/tanks/config.cjs @@ -5,7 +5,7 @@ var Y = require('Y').Y exports.defaults = { ui : { showGridCoords : false, - showCountdown : false + showCountdown : true }, pathing : { overlayAIPaths : false,