Skip to content

Commit

Permalink
Making non poly-fill methods override and asserting this in the tests
Browse files Browse the repository at this point in the history
Former-commit-id: 3cc967c13a377b3cceb625d45595a99d92bac920 [formerly c1bc14ebf021cac4d46ed22e898b0d1b315b9a98]
Former-commit-id: a59d2e2cfbf7e1a39d44ea0cc302554d11dcd4e0
  • Loading branch information
andrewplummer committed Aug 17, 2013
1 parent c138ae5 commit bc189b4
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 37 deletions.
20 changes: 13 additions & 7 deletions lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
array[AlphanumericSortEquivalents] = equivalents;
}

extend(array, false, false, {
extend(array, false, true, {

/***
*
Expand Down Expand Up @@ -468,7 +468,11 @@
checkCallback(f);
index = arrayFind(this, f, 0, false, true, context);
return isUndefined(index) ? -1 : index;
},
}

});

extend(array, true, true, {

/***
* @method findAll(<f>, [index] = 0, [loop] = false)
Expand Down Expand Up @@ -1158,8 +1162,10 @@

});


// Aliases
extend(array, true, false, {

extend(array, true, true, {

/***
* @method all()
Expand Down Expand Up @@ -1228,7 +1234,7 @@
***/

function buildEnumerableMethods(names, mapping) {
extendSimilar(object, false, false, names, function(methods, name) {
extendSimilar(object, false, true, names, function(methods, name) {
methods[name] = function(obj, arg1, arg2) {
var result, coerced = keysWithObjectCoercion(obj), matcher;
if(!mapping) {
Expand Down Expand Up @@ -1256,11 +1262,11 @@
buildObjectInstanceMethods(names, Hash);
}

function exportSortAlgorithms() {
function exportSortAlgorithm() {
array[AlphanumericSort] = collateStrings;
}

extend(object, false, false, {
extend(object, false, true, {

'map': function(obj, map) {
var result = {}, key, value;
Expand Down Expand Up @@ -1311,5 +1317,5 @@
buildEnumerableMethods(EnumerableFindingMethods);
buildEnumerableMethods(EnumerableMappingMethods, true);
buildObjectInstanceMethods(EnumerableOtherMethods, Hash);
exportSortAlgorithms();
exportSortAlgorithm();

2 changes: 1 addition & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
function initializeClass(klass) {
if(klass['SugarMethods']) return;
defineProperty(klass, 'SugarMethods', {});
extend(klass, false, false, {
extend(klass, false, true, {
'extend': function(methods, override, instance) {
extend(klass, instance !== false, override, methods);
},
Expand Down
20 changes: 10 additions & 10 deletions lib/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@
***/

function buildDateMethods() {
extendSimilar(date, true, false, DateUnits, function(methods, u, i) {
extendSimilar(date, true, true, DateUnits, function(methods, u, i) {
var name = u.name, caps = simpleCapitalize(name), multiplier = u.multiplier(), since, until;
u.addMethod = 'add' + caps + 's';
// "since/until now" only count "past" an integer, i.e. "2 days ago" is
Expand Down Expand Up @@ -1527,7 +1527,7 @@
}

function buildFormatShortcuts() {
extendSimilar(date, true, false, 'short,long,full', function(methods, name) {
extendSimilar(date, true, true, 'short,long,full', function(methods, name) {
methods[name] = function(localeCode) {
return formatDate(this, name, false, localeCode);
}
Expand Down Expand Up @@ -1599,7 +1599,7 @@
var special = 'today,yesterday,tomorrow,weekday,weekend,future,past'.split(',');
var weekdays = English['weekdays'].slice(0,7);
var months = English['months'].slice(0,12);
extendSimilar(date, true, false, special.concat(weekdays).concat(months), function(methods, name) {
extendSimilar(date, true, true, special.concat(weekdays).concat(months), function(methods, name) {
methods['is'+ simpleCapitalize(name)] = function(utc) {
return this.is(name, 0, utc);
};
Expand Down Expand Up @@ -1627,16 +1627,16 @@
}

function setDateProperties() {
date.extend({
extend(date, false , true, {
'RFC1123': '{Dow}, {dd} {Mon} {yyyy} {HH}:{mm}:{ss} {tz}',
'RFC1036': '{Weekday}, {dd}-{Mon}-{yy} {HH}:{mm}:{ss} {tz}',
'ISO8601_DATE': '{yyyy}-{MM}-{dd}',
'ISO8601_DATETIME': '{yyyy}-{MM}-{dd}T{HH}:{mm}:{ss}.{fff}{isotz}'
}, false, false);
});
}


date.extend({
extend(date, false, true, {

/***
* @method Date.create(<d>, [locale] = currentLocale)
Expand Down Expand Up @@ -1751,9 +1751,9 @@
addDateInputFormat(getLocalization(localeCode), format, match);
}

}, false, false);
});

date.extend({
extend(date, true, true, {

/***
* @method set(<set>, [reset] = false)
Expand Down Expand Up @@ -2162,7 +2162,7 @@


// Instance aliases
date.extend({
extend(date, true, true, {

/***
* @method iso()
Expand Down Expand Up @@ -2363,7 +2363,7 @@
number.extend(methods);
}

number.extend({
extend(number, true, true, {

/***
* @method duration([locale] = currentLocale)
Expand Down
2 changes: 1 addition & 1 deletion lib/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}, ms));
}

extend(Function, true, false, {
extend(Function, true, true, {

/***
* @method lazy([ms] = 1, [immediate] = false, [limit] = Infinity)
Expand Down
2 changes: 1 addition & 1 deletion lib/inflections.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
Inflector.uncountable('equipment,information,rice,money,species,series,fish,sheep,jeans'.split(','));


extend(string, true, false, {
extend(string, true, true, {

/***
* @method pluralize()
Expand Down
2 changes: 1 addition & 1 deletion lib/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
'vy': 'ꝡ'
};

extend(string, true, false, {
extend(string, true, true, {
/***
* @method normalize()
* @returns String
Expand Down
8 changes: 4 additions & 4 deletions lib/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}


extend(number, false, false, {
extend(number, false, true, {

/***
* @method Number.random([n1], [n2])
Expand All @@ -54,7 +54,7 @@

});

extend(number, true, false, {
extend(number, true, true, {

/***
* @method log(<base> = Math.E)
Expand Down Expand Up @@ -383,12 +383,12 @@
return precision ? withPrecision(this, precision, fn) : fn(this);
}
}
extend(number, true, false, {
extend(number, true, true, {
'ceil': createRoundingFunction(ceil),
'round': createRoundingFunction(round),
'floor': createRoundingFunction(floor)
});
extendSimilar(number, true, false, 'abs,pow,sin,asin,cos,acos,tan,atan,exp,pow,sqrt', function(methods, name) {
extendSimilar(number, true, true, 'abs,pow,sin,asin,cos,acos,tan,atan,exp,pow,sqrt', function(methods, name) {
methods[name] = function(a, b) {
return math[name](this, a, b);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
*
***/
function buildTypeMethods() {
extendSimilar(object, false, false, ClassNames, function(methods, name) {
extendSimilar(object, false, true, ClassNames, function(methods, name) {
var method = 'is' + name;
ObjectTypeMethods.push(method);
methods[method] = typeChecks[name];
Expand Down Expand Up @@ -195,7 +195,7 @@

});

extend(object, false, false, {
extend(object, false, true, {

'isObject': function(obj) {
return isPlainObject(obj);
Expand Down
10 changes: 5 additions & 5 deletions lib/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
return this.isValid() ? this.start + ".." + this.end : 'Invalid Range';
};

extend(Range, true, false, {
extend(Range, true, true, {

/***
* @method isValid()
Expand Down Expand Up @@ -261,7 +261,7 @@

});

extend(Range, true, false, {
extend(Range, true, true, {

/***
* @method step()
Expand Down Expand Up @@ -294,7 +294,7 @@
* @extra If either [start] or [end] are null, they will default to the current date.
***/
[number, string, date].forEach(function(klass) {
extend(klass, false, false, {
extend(klass, false, true, {

'range': function(start, end) {
if(klass.create) {
Expand All @@ -313,7 +313,7 @@
*
***/

extend(number, true, false, {
extend(number, true, true, {

/***
* @method upto(<num>, [fn], [step] = 1)
Expand Down Expand Up @@ -357,7 +357,7 @@

});

extend(number, true, false, {
extend(number, true, true, {

/***
* @method downto(<num>, [fn], [step] = 1)
Expand Down
4 changes: 2 additions & 2 deletions lib/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
***/

extend(regexp, false, false, {
extend(regexp, false, true, {

/***
* @method RegExp.escape(<str> = '')
Expand All @@ -31,7 +31,7 @@

});

extend(regexp, true, false, {
extend(regexp, true, true, {

/***
* @method getFlags()
Expand Down
4 changes: 2 additions & 2 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
});


extend(string, true, false, {
extend(string, true, true, {

/***
* @method escapeRegExp()
Expand Down Expand Up @@ -889,7 +889,7 @@

// Aliases

extend(string, true, false, {
extend(string, true, true, {

/***
* @method insert()
Expand Down
3 changes: 2 additions & 1 deletion test/default.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Sugar Full Package</title>
<title>Sugar Default Package</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="stylesheets/custom.css" type="text/css" />
<link rel="stylesheet" href="stylesheets/tooltip.css" type="text/css" />
<!-- setup -->
<script type="text/javascript" src="javascripts/setup.js"></script>
<script type="text/javascript" src="javascripts/overrides.js"></script>
<script type="text/javascript" src="../performance/javascripts/benchmark.js"></script>
<!-- jquery -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
Expand Down
1 change: 1 addition & 0 deletions test/full.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="stylesheets/tooltip.css" type="text/css" />
<!-- setup -->
<script type="text/javascript" src="javascripts/setup.js"></script>
<script type="text/javascript" src="javascripts/overrides.js"></script>
<script type="text/javascript" src="../performance/javascripts/benchmark.js"></script>
<!-- jquery -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
Expand Down
59 changes: 59 additions & 0 deletions test/javascripts/overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

var overridingFunction = function() {
return 'Something totally unexpected!';
}

Object.map = overridingFunction;
Object.each = overridingFunction;
Object.any = overridingFunction;
Object.sum = overridingFunction;
Object.isObject = overridingFunction;
Object.isNumber = overridingFunction;
Object.extend = overridingFunction;

Array.create = overridingFunction;
Array.prototype.findAll = overridingFunction;
Array.prototype.all = overridingFunction;
Array.prototype.add = overridingFunction;
Array.prototype.groupBy = overridingFunction;

String.extend = overridingFunction;
String.range = overridingFunction;
String.prototype.each = overridingFunction;
String.prototype.to = overridingFunction;
String.prototype.pad = overridingFunction;
String.prototype.insert = overridingFunction;
String.prototype.pluralize = overridingFunction;
String.prototype.hankaku = overridingFunction;

Number.random = overridingFunction;
Number.range = overridingFunction;
Number.prototype.log = overridingFunction;
Number.prototype.ceil = overridingFunction;
Number.prototype.round = overridingFunction;
Number.prototype.floor = overridingFunction;
Number.prototype.abs = overridingFunction;
Number.prototype.upto = overridingFunction;
Number.prototype.downto = overridingFunction;
Number.prototype.secondsBefore = overridingFunction;
Number.prototype.duration = overridingFunction;

RegExp.escape = overridingFunction;
RegExp.prototype.getFlags = overridingFunction;
RegExp.prototype.setFlags = overridingFunction;

Function.prototype.lazy = overridingFunction;
Function.prototype.throttle = overridingFunction;
Function.prototype.debounce = overridingFunction;


Date['ISO8601_DATE'] = 'none!';
Date.create = overridingFunction;
Date.range = overridingFunction;
Date.prototype.set = overridingFunction;
Date.prototype.iso = overridingFunction;
Date.prototype.endOfDay = overridingFunction;
Date.prototype.long = overridingFunction;
Date.prototype.isToday = overridingFunction;
Date.prototype.addMinutes = overridingFunction;

0 comments on commit bc189b4

Please sign in to comment.