From: Tim Wood Date: Fri, 8 Apr 2011 22:21:25 +0000 (-0700) Subject: stripped a few more bytes off by caching the date.prototype to _DateProto X-Git-Tag: 0.3.2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7cfbd6400824550fe1b59ff6d4396454af4b748;p=thirdparty%2Fmoment.git stripped a few more bytes off by caching the date.prototype to _DateProto --- diff --git a/README.markdown b/README.markdown index adcc94b62..703613eb7 100644 --- a/README.markdown +++ b/README.markdown @@ -21,8 +21,8 @@ In addition to the date creation and manipulation functions, there are a few fun gzipped - 3.58 kb - 1.6 kb + 3.51 kb + 1.59 kb diff --git a/lib/underscore.date.js b/lib/underscore.date.js index c077254c4..8e8ef82a4 100644 --- a/lib/underscore.date.js +++ b/lib/underscore.date.js @@ -122,8 +122,9 @@ return this; } + var _DateProto = _Date.prototype; - _Date.prototype.format = function(inputString) { + _DateProto.format = function(inputString) { // shortcuts to this and getting time functions // done to save bytes in minification var date = this.date, @@ -241,15 +242,15 @@ return inputString.replace(charactersToReplace, replaceFunction); }; - _Date.prototype.add = function(input) { + _DateProto.add = function(input) { return dateAddRemove(this, input, 1); }; - _Date.prototype.subtract = function(input) { + _DateProto.subtract = function(input) { return dateAddRemove(this, input, -1); }; - _Date.prototype.customize = function(input) { + _DateProto.customize = function(input) { var inputOrdinal = input.ordinal; _.extend(wordsWeekdays, input.weekdays); _.extend(wordsWeekdaysShort, input.weekdaysShort); @@ -284,7 +285,7 @@ substituteTimeAgo('yy', ~~ years); } - _Date.prototype.from = function(time, withoutSuffix, asMilliseconds) { + _DateProto.from = function(time, withoutSuffix, asMilliseconds) { var difference = msApart(this.date, time), string = difference < 0 ? wordsTimeAgo.past : wordsTimeAgo.future; return asMilliseconds ? difference : @@ -292,30 +293,25 @@ string.replace(/%s/i, relativeTime(difference)); }; - _Date.prototype.fromNow = function(withoutSuffix, asMilliseconds) { + _DateProto.fromNow = function(withoutSuffix, asMilliseconds) { return this.from(_.now(), withoutSuffix, asMilliseconds); }; - _Date.prototype.isLeapYear = function() { + _DateProto.isLeapYear = function() { return _.isLeapYear(this.date.getFullYear()); }; - // underscore mixins - _d = { + // integrate with underscore.js + _.mixin({ date : function(input) { return new _Date(input); }, now : function(asTimestamp) { - return asTimestamp ? new Date().getTime() : _d.date(); + return asTimestamp ? new Date().getTime() : _.date(); }, isLeapYear : function(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } - }; - - // integrate with underscore.js - if (_ && _.mixin) { - _.mixin(_d); - } + }); }(Date, _)); diff --git a/lib/underscore.date.min.js b/lib/underscore.date.min.js index e9e4c673d..885e2fe63 100644 --- a/lib/underscore.date.min.js +++ b/lib/underscore.date.min.js @@ -5,4 +5,4 @@ // // Version 0.3.2 -(function(a,b,c){function u(a){var b=Math.abs(a)/1e3,c=b/60,d=c/60,e=d/24,f=e/365;return b<45&&r("s",~~b)||b<90&&r("m")||c<45&&r("mm",~~c)||c<90&&r("h")||d<24&&r("hh",~~d)||d<48&&r("d")||e<25&&r("dd",~~e)||e<45&&r("M")||e<350&&r("MM",~~((e+15)/30))||f<2&&r("y")||r("yy",~~f)}function t(a,b){return q(a)-q(b)}function s(a){this.date=p(a);return this}function r(a,b){return k[a].replace(/%d/i,b||1)}function q(a){return isNaN(a)?p(a).getTime():a}function p(d){return d===c?new a:d instanceof s?d.date:b.isDate(d)?d:b.isArray(d)&&d.length>2?o(d):new a(d)}function o(b){return new a(b[0],b[1]||0,b[2]||1,b[3]||0,b[4]||0,b[5]||0,b[6]||0)}function n(b,c,d){var e=b.date,f=(c.ms||0)+(c.s||0)*1e3+(c.m||0)*6e4+(c.h||0)*36e5+(c.d||0)*864e5+(c.w||0)*6048e5,g=(c.M||0)+(c.y||0)*12,h;f&&e.setMilliseconds(e.getMilliseconds()+f*d),g&&(h=e.getDate(),e.setDate(1),e.setMonth(e.getMonth()+g*d),e.setDate(Math.min((new a(e.getFullYear(),e.getMonth()+1,0)).getDate(),h)));return b}function m(a,b){var c=a+"";while(c.length11?"pm":"am";case"A":return n>11?"PM":"AM";case"H":return n;case"HH":return m(n,2);case"h":return n%12||12;case"hh":return m(n%12||12,2);case"m":return o;case"mm":return m(o,2);case"s":return p;case"ss":return m(p,2);case"z":return t("zz").replace(s,"");case"zz":c=q.indexOf("(");if(c>-1)return q.slice(c+1,q.indexOf(")"));return q.slice(q.indexOf(":")).replace(s,"");default:return b.replace("\\","")}}var c=this.date,d=c.getMonth(),e=c.getDate(),f=c.getFullYear(),k=c.getDay(),n=c.getHours(),o=c.getMinutes(),p=c.getSeconds(),q=c.toString(),r=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?)/g,s=/[^A-Z]/g;return b.replace(r,t)},s.prototype.add=function(a){return n(this,a,1)},s.prototype.subtract=function(a){return n(this,a,-1)},s.prototype.customize=function(a){var c=a.ordinal;b.extend(i,a.weekdays),b.extend(j,a.weekdaysShort),b.extend(g,a.months),b.extend(h,a.monthsShort),b.extend(k,a.relativeTime),c&&b.isFunction(c)&&(l=c)},s.prototype.from=function(a,b,c){var d=t(this.date,a),e=d<0?k.past:k.future;return c?d:b?u(d):e.replace(/%s/i,u(d))},s.prototype.fromNow=function(a,c){return this.from(b.now(),a,c)},s.prototype.isLeapYear=function(){return b.isLeapYear(this.date.getFullYear())},f={date:function(a){return new s(a)},now:function(b){return b?(new a).getTime():f.date()},isLeapYear:function(a){return a%4===0&&a%100!==0||a%400===0}},b&&b.mixin&&b.mixin(f)})(Date,_) +(function(a,b,c){function v(a){var b=Math.abs(a)/1e3,c=b/60,d=c/60,e=d/24,f=e/365;return b<45&&r("s",~~b)||b<90&&r("m")||c<45&&r("mm",~~c)||c<90&&r("h")||d<24&&r("hh",~~d)||d<48&&r("d")||e<25&&r("dd",~~e)||e<45&&r("M")||e<350&&r("MM",~~((e+15)/30))||f<2&&r("y")||r("yy",~~f)}function u(a,b){return q(a)-q(b)}function s(a){this.date=p(a);return this}function r(a,b){return k[a].replace(/%d/i,b||1)}function q(a){return isNaN(a)?p(a).getTime():a}function p(d){return d===c?new a:d instanceof s?d.date:b.isDate(d)?d:b.isArray(d)&&d.length>2?o(d):new a(d)}function o(b){return new a(b[0],b[1]||0,b[2]||1,b[3]||0,b[4]||0,b[5]||0,b[6]||0)}function n(b,c,d){var e=b.date,f=(c.ms||0)+(c.s||0)*1e3+(c.m||0)*6e4+(c.h||0)*36e5+(c.d||0)*864e5+(c.w||0)*6048e5,g=(c.M||0)+(c.y||0)*12,h;f&&e.setMilliseconds(e.getMilliseconds()+f*d),g&&(h=e.getDate(),e.setDate(1),e.setMonth(e.getMonth()+g*d),e.setDate(Math.min((new a(e.getFullYear(),e.getMonth()+1,0)).getDate(),h)));return b}function m(a,b){var c=a+"";while(c.length11?"pm":"am";case"A":return n>11?"PM":"AM";case"H":return n;case"HH":return m(n,2);case"h":return n%12||12;case"hh":return m(n%12||12,2);case"m":return o;case"mm":return m(o,2);case"s":return p;case"ss":return m(p,2);case"z":return t("zz").replace(s,"");case"zz":c=q.indexOf("(");if(c>-1)return q.slice(c+1,q.indexOf(")"));return q.slice(q.indexOf(":")).replace(s,"");default:return b.replace("\\","")}}var c=this.date,d=c.getMonth(),e=c.getDate(),f=c.getFullYear(),k=c.getDay(),n=c.getHours(),o=c.getMinutes(),p=c.getSeconds(),q=c.toString(),r=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?)/g,s=/[^A-Z]/g;return b.replace(r,t)},t.add=function(a){return n(this,a,1)},t.subtract=function(a){return n(this,a,-1)},t.customize=function(a){var c=a.ordinal;b.extend(i,a.weekdays),b.extend(j,a.weekdaysShort),b.extend(g,a.months),b.extend(h,a.monthsShort),b.extend(k,a.relativeTime),c&&b.isFunction(c)&&(l=c)},t.from=function(a,b,c){var d=u(this.date,a),e=d<0?k.past:k.future;return c?d:b?v(d):e.replace(/%s/i,v(d))},t.fromNow=function(a,c){return this.from(b.now(),a,c)},t.isLeapYear=function(){return b.isLeapYear(this.date.getFullYear())},b.mixin({date:function(a){return new s(a)},now:function(c){return c?(new a).getTime():b.date()},isLeapYear:function(a){return a%4===0&&a%100!==0||a%400===0}})})(Date,_)