services/sync/tps/extensions/mozmill/resource/stdlib/withs.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tps/extensions/mozmill/resource/stdlib/withs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/*
     1.5 +    Copyright (c) 2006 Lawrence Oluyede <l.oluyede@gmail.com>
     1.6 +
     1.7 +    Permission is hereby granted, free of charge, to any person obtaining a copy
     1.8 +    of this software and associated documentation files (the "Software"), to deal
     1.9 +    in the Software without restriction, including without limitation the rights
    1.10 +    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    1.11 +    copies of the Software, and to permit persons to whom the Software is
    1.12 +    furnished to do so, subject to the following conditions:
    1.13 +
    1.14 +    The above copyright notice and this permission notice shall be included in all
    1.15 +    copies or substantial portions of the Software.
    1.16 +
    1.17 +    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.18 +    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.19 +    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    1.20 +    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.21 +    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    1.22 +    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    1.23 +    SOFTWARE.
    1.24 +*/
    1.25 +
    1.26 +/*
    1.27 + startsWith(str, prefix[, start[, end]]) -> bool
    1.28 +
    1.29 + Return true if str ends with the specified prefix, false otherwise.
    1.30 + With optional start, test str beginning at that position.
    1.31 + With optional end, stop comparing str at that position.
    1.32 + prefix can also be an array of strings to try.
    1.33 +*/
    1.34 +
    1.35 +var EXPORTED_SYMBOLS = ['startsWith', 'endsWith'];
    1.36 +
    1.37 +function startsWith(str, prefix, start, end) {
    1.38 +    if (arguments.length < 2) {
    1.39 +        throw new TypeError('startsWith() requires at least 2 arguments');
    1.40 +    }
    1.41 +
    1.42 +    // check if start and end are null/undefined or a 'number'
    1.43 +    if ((start == null) || (isNaN(new Number(start)))) {
    1.44 +        start = 0;
    1.45 +    }
    1.46 +    if ((end == null) || (isNaN(new Number(end)))) {
    1.47 +        end = Number.MAX_VALUE;
    1.48 +    }
    1.49 +
    1.50 +    // if it's an array
    1.51 +    if (typeof prefix == "object") {
    1.52 +        for (var i = 0, j = prefix.length; i < j; i++) {
    1.53 +            var res = _stringTailMatch(str, prefix[i], start, end, true);
    1.54 +            if (res) {
    1.55 +                return true;
    1.56 +            }
    1.57 +        }
    1.58 +        return false;
    1.59 +    }
    1.60 +
    1.61 +    return _stringTailMatch(str, prefix, start, end, true);
    1.62 +}
    1.63 +
    1.64 +/*
    1.65 + endsWith(str, suffix[, start[, end]]) -> bool
    1.66 +
    1.67 + Return true if str ends with the specified suffix, false otherwise.
    1.68 + With optional start, test str beginning at that position.
    1.69 + With optional end, stop comparing str at that position.
    1.70 + suffix can also be an array of strings to try.
    1.71 +*/
    1.72 +function endsWith(str, suffix, start, end) {
    1.73 +    if (arguments.length < 2) {
    1.74 +        throw new TypeError('endsWith() requires at least 2 arguments');
    1.75 +    }
    1.76 +
    1.77 +    // check if start and end are null/undefined or a 'number'
    1.78 +    if ((start == null) || (isNaN(new Number(start)))) {
    1.79 +        start = 0;
    1.80 +    }
    1.81 +    if ((end == null) || (isNaN(new Number(end)))) {
    1.82 +        end = Number.MAX_VALUE;
    1.83 +    }
    1.84 +
    1.85 +    // if it's an array
    1.86 +    if (typeof suffix == "object") {
    1.87 +        for (var i = 0, j = suffix.length; i < j; i++) {
    1.88 +            var res = _stringTailMatch(str, suffix[i], start, end, false);
    1.89 +            if (res) {
    1.90 +                return true;
    1.91 +            }
    1.92 +        }
    1.93 +        return false;
    1.94 +    }
    1.95 +
    1.96 +    return _stringTailMatch(str, suffix, start, end, false);
    1.97 +}
    1.98 +
    1.99 +/*
   1.100 + Matches the end (direction == false) or start (direction == true) of str
   1.101 + against substr, using the start and end arguments. Returns false
   1.102 + if not found and true if found.
   1.103 +*/
   1.104 +function _stringTailMatch(str, substr, start, end, fromStart) {
   1.105 +    var len = str.length;
   1.106 +    var slen = substr.length;
   1.107 +
   1.108 +    var indices = _adjustIndices(start, end, len);
   1.109 +    start = indices[0]; end = indices[1]; len = indices[2];
   1.110 +
   1.111 +    if (fromStart) {
   1.112 +        if (start + slen > len) {
   1.113 +            return false;
   1.114 +        }
   1.115 +    } else {
   1.116 +        if (end - start < slen || start > len) {
   1.117 +            return false;
   1.118 +        }
   1.119 +        if (end - slen > start) {
   1.120 +            start = end - slen;
   1.121 +        }
   1.122 +    }
   1.123 +
   1.124 +    if (end - start >= slen) {
   1.125 +        return str.substr(start, slen) == substr;
   1.126 +    }
   1.127 +    return false;
   1.128 +}
   1.129 +
   1.130 +function _adjustIndices(start, end, len)
   1.131 +{
   1.132 +	if (end > len) {
   1.133 +	    end = len;
   1.134 +	} else if (end < 0) {
   1.135 +	    end += len;
   1.136 +	}
   1.137 +
   1.138 +    if (end < 0) {
   1.139 +        end = 0;
   1.140 +    }
   1.141 +	if (start < 0) {
   1.142 +	    start += len;
   1.143 +	}
   1.144 +	if (start < 0) {
   1.145 +		start = 0;
   1.146 +	}
   1.147 +	
   1.148 +	return [start, end, len];
   1.149 +}

mercurial