Skip to content

Loading…

Update String:contains for ES6, fixes #2402 #2440

Merged
merged 3 commits into from

2 participants

@arian
MooTools member

Fixes #2402

Most work done by @kentaromiura.

I rebased his commits, which contained semicolon fixes and other not so important stuff.
I added the !ES6 block and the documentation updates.

kentaromiura and others added some commits
@kentaromiura kentaromiura Fixes #2402 - String.prototype.contains is now implemented according …
…to the new ES6 standard

- Moved the old String.prototype.contains implementation in 1.4compat
- Using good old indexOf at other places
f9307e0
@arian arian Add !ES6 blocs for String:contains fab9ad4
@arian arian Update documentation for String:contains ce25f3c
@arian arian merged commit ce25f3c into mootools:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 27, 2012
  1. @kentaromiura @arian

    Fixes #2402 - String.prototype.contains is now implemented according …

    kentaromiura committed with arian
    …to the new ES6 standard
    
    - Moved the old String.prototype.contains implementation in 1.4compat
    - Using good old indexOf at other places
  2. @arian

    Add !ES6 blocs for String:contains

    arian committed
  3. @arian
View
23 Docs/Types/String.md
@@ -87,16 +87,16 @@ String method: contains {#String:contains}
-----------------------------------
Checks to see if the string passed in is contained in this string.
-If the separator parameter is passed, will check to see if the string is contained in the list of values separated by that parameter.
+If the position parameter is passed, it will only check for the string from that point.
### Syntax:
- myString.contains(string[, separator]);
+ myString.contains(string[, position]);
### Arguments:
1. string - (*string*) The string to search for.
-2. separator - (*string*, optional) The string that separates the values in this string (e.g. Element classNames are separated by a ' ').
+2. position - (*number*, optional) Position in the string to begin searching for `string`, defaults to `0`.
### Returns:
@@ -106,10 +106,18 @@ If the separator parameter is passed, will check to see if the string is contain
### Examples:
'a bc'.contains('bc'); // returns true
- 'a b c'.contains('c', ' '); // returns true
- 'a bc'.contains('b', ' '); // returns false
+ 'abc'.contains('b', 1); // returns true
+ 'abc'.contains('b', 2); // returns false
+### See Also:
+
+- [MDC String:indexOf][]
+- [MDC String:contains][]
+### Note:
+
+Since MooTools 1.5 the second parameter changed from `separator` to `position` so it conforms the ES6 specification.
+If using the 1.4 compatibility layer, this method will be overwritten to have the old behavior.
String method: trim {#String:trim}
---------------------------
@@ -128,7 +136,9 @@ Trims the leading and trailing spaces off a string.
' i like cookies '.trim(); // returns 'i like cookies'
+### See Also:
+- [MDC String:trim][]
String method: clean {#String:clean}
-----------------------------
@@ -390,6 +400,9 @@ Strips the String of its *<script>* tags and anything in between them.
[MDC String]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String
+[MDC String:contains]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/contains
+[MDC String:indexOf]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf
+[MDC String:trim]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/trim
[MDC Regexp:test]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/RegExp/test
[MDC Regular Expressions]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions
[MDC parseInt]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt
View
2 Source/Core/Core.js
@@ -264,7 +264,7 @@ var force = function(name, object, methods){
};
force('String', String, [
- 'charAt', 'charCodeAt', 'concat', 'indexOf', 'lastIndexOf', 'match', 'quote', 'replace', 'search',
+ 'charAt', 'charCodeAt', 'concat', 'contains', 'indexOf', 'lastIndexOf', 'match', 'quote', 'replace', 'search',
'slice', 'split', 'substr', 'substring', 'trim', 'toLowerCase', 'toUpperCase'
])('Array', Array, [
'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'concat', 'join', 'slice',
View
2 Source/Element/Element.js
@@ -699,7 +699,7 @@ Element.implement({
hasClass: hasClassList ? function(className) {
return this.classList.contains(className);
} : function(className){
- return this.className.clean().contains(className, ' ');
+ return (' ' + this.className.clean() + ' ').indexOf(' ' + className + ' ') > -1;
},
addClass: hasClassList ? function(className) {
View
2 Source/Fx/Fx.CSS.js
@@ -122,7 +122,7 @@ Fx.CSS = new Class({
Array.each(document.styleSheets, function(sheet, j){
var href = sheet.href;
- if (href && href.contains('://') && !href.contains(document.domain)) return;
+ if (href && href.indexOf('://') > -1 && href.indexOf(document.domain) == -1) return;
var rules = sheet.rules || sheet.cssRules;
searchStyles(rules);
});
View
4 Source/Request/Request.js
@@ -183,10 +183,10 @@ var Request = this.Request = new Class({
if (trimPosition > -1 && (trimPosition = url.indexOf('#')) > -1) url = url.substr(0, trimPosition);
if (this.options.noCache)
- url += (url.contains('?') ? '&' : '?') + String.uniqueID();
+ url += (url.indexOf('?') > -1 ? '&' : '?') + String.uniqueID();
if (data && (method == 'get' || method == 'delete')){
- url += (url.contains('?') ? '&' : '?') + data;
+ url += (url.indexOf('?') > -1 ? '&' : '?') + data;
data = null;
}
View
16 Source/Types/String.js
@@ -16,12 +16,14 @@ provides: String
String.implement({
- test: function(regex, params){
- return ((typeOf(regex) == 'regexp') ? regex : new RegExp('' + regex, params)).test(this);
+ //<!ES6>
+ contains: function(string, index){
+ return (index ? String(this).slice(index) : String(this)).indexOf(string) > -1;
},
+ //</!ES6>
- contains: function(string, separator){
- return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : String(this).indexOf(string) > -1;
+ test: function(regex, params){
+ return ((typeOf(regex) == 'regexp') ? regex : new RegExp('' + regex, params)).test(this);
},
trim: function(){
@@ -80,3 +82,9 @@ String.implement({
}
});
+
+//<1.4compat>
+String.prototype.contains = function(string, separator){
+ return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : String(this).indexOf(string) > -1;
+};
+//</1.4compat>
View
4 Specs/1.2/Native/String.js
@@ -44,7 +44,7 @@ describe("String Methods", {
},
// String.contains
-
+//<1.4compat>
'should return true if the string contains a string otherwise false': function(){
expect('i like cookies'.contains('cookies')).toBeTruthy();
expect('i,like,cookies'.contains('cookies')).toBeTruthy();
@@ -58,7 +58,7 @@ describe("String Methods", {
expect('i,like,cookies'.contains('cookies', ' ')).toBeFalsy();
expect('i,like,cookies'.contains('cookies', ',')).toBeTruthy();
},
-
+//</1.4compat>
// String.test
'should return true if the test matches the string otherwise false': function(){
Something went wrong with that request. Please try again.