maandag 11 mei 2009

stringQuery plugin

Quite often I find myself in the position that I need to check some simple thing on strings. Does it start/end with a certain string? Does it contain a certain string. SO i decided to code up this little plugin for jQuery. It's not much, but it just makes things a little easier.

$.stringQuery.startsWith(haystack, needle)

This function checks if string haystack starts with string needle.
$.stringQuery.startsWith('foobar', 'foo') === true;
$.stringQuery.startsWith('foobar', 'af') === false;

$.stringQuery.endsWith(haystack, needle)

This function checks if string haystack ends with string needle.
$.stringQuery.endsWith('foobar', 'bar') === true;
$.stringQuery.endsWith('foobar', 'bara') === false;

$.stringQuery.contains(haystack, needle)

This function checks if string haystack contains string needle.
$.stringQuery.contains('foobar', 'bar') === true;
$.stringQuery.contains('foobar', 'ob') === true;
$.stringQuery.contains('foobar', 'hai') === false;


This is just something I find myself testing a lot and I thought it would be nice to make the checks a little easier.

Here is the source code (Note how the License is about two times as big as the actual code)

/*
Copyright (c) 2009, Pim Jager
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The name Pim Jager may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Pim Jager ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Pim Jager BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function($){
    $.stringQuery = {};
    $.stringQuery.startsWith = function(haystack, needle){
        //checks if haystack starts with needle i.e. startsWith('foobar','foo') //true
        return haystack.indexOf(needle) === 0;
    }
    $.stringQuery.endsWith = function(haystack, needle){
        //checks if haystack ends with needle i.e. endsWith('foobar','bar') //true
        return haystack.substr(haystack.length - needle.length) === needle;
    }
    $.stringQuery.contains = function(haystack, needle){
        //checks if haystack contains needle i.e. contains('foobar','ob'); //true
        return haystack.indexOf(needle) !== -1;
    }
})(jQuery);

Geen opmerkingen:

Een reactie posten