1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// Copyright (C) 2013 Google Inc. All rights reserved. 2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// 3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without 4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// modification, are permitted provided that the following conditions are 5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// met: 6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// 7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// * Redistributions of source code must retain the above copyright 8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// notice, this list of conditions and the following disclaimer. 9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// * Redistributions in binary form must reproduce the above 10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer 11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// in the documentation and/or other materials provided with the 12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// distribution. 13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// * Neither the name of Google Inc. nor the names of its 14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// contributors may be used to endorse or promote products derived from 15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// this software without specific prior written permission. 16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// 17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 29926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// @fileoverview Generic string utility functions. 30926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)var string = string || {}; 31926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 32926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)(function() { 33926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 34926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)string.contains = function(a, b) 35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return a.indexOf(b) != -1; 37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)string.caseInsensitiveContains = function(a, b) 40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return a.match(new RegExp(b, 'i')); 42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)string.startsWith = function(a, b) 45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return a.indexOf(b) == 0; 47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 49926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)string.endsWith = function(a, b) 50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return a.lastIndexOf(b) == a.length - b.length; 52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 53926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 54926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)string.isValidName = function(str) 55926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 5693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) return str.match(/[A-Za-z0-9\-\_,\+]/); 57926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 59926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)string.trimString = function(str) 60926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return str.replace(/^\s+|\s+$/g, ''); 62926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 63926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 64926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)string.collapseWhitespace = function(str) 65926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 66926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return str.replace(/\s+/g, ' '); 67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 68926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 69926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)})();