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

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 /*
michael@0 2 Copyright (c) 2006 Lawrence Oluyede <l.oluyede@gmail.com>
michael@0 3
michael@0 4 Permission is hereby granted, free of charge, to any person obtaining a copy
michael@0 5 of this software and associated documentation files (the "Software"), to deal
michael@0 6 in the Software without restriction, including without limitation the rights
michael@0 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
michael@0 8 copies of the Software, and to permit persons to whom the Software is
michael@0 9 furnished to do so, subject to the following conditions:
michael@0 10
michael@0 11 The above copyright notice and this permission notice shall be included in all
michael@0 12 copies or substantial portions of the Software.
michael@0 13
michael@0 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
michael@0 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
michael@0 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
michael@0 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
michael@0 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
michael@0 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
michael@0 20 SOFTWARE.
michael@0 21 */
michael@0 22
michael@0 23 /*
michael@0 24 startsWith(str, prefix[, start[, end]]) -> bool
michael@0 25
michael@0 26 Return true if str ends with the specified prefix, false otherwise.
michael@0 27 With optional start, test str beginning at that position.
michael@0 28 With optional end, stop comparing str at that position.
michael@0 29 prefix can also be an array of strings to try.
michael@0 30 */
michael@0 31
michael@0 32 var EXPORTED_SYMBOLS = ['startsWith', 'endsWith'];
michael@0 33
michael@0 34 function startsWith(str, prefix, start, end) {
michael@0 35 if (arguments.length < 2) {
michael@0 36 throw new TypeError('startsWith() requires at least 2 arguments');
michael@0 37 }
michael@0 38
michael@0 39 // check if start and end are null/undefined or a 'number'
michael@0 40 if ((start == null) || (isNaN(new Number(start)))) {
michael@0 41 start = 0;
michael@0 42 }
michael@0 43 if ((end == null) || (isNaN(new Number(end)))) {
michael@0 44 end = Number.MAX_VALUE;
michael@0 45 }
michael@0 46
michael@0 47 // if it's an array
michael@0 48 if (typeof prefix == "object") {
michael@0 49 for (var i = 0, j = prefix.length; i < j; i++) {
michael@0 50 var res = _stringTailMatch(str, prefix[i], start, end, true);
michael@0 51 if (res) {
michael@0 52 return true;
michael@0 53 }
michael@0 54 }
michael@0 55 return false;
michael@0 56 }
michael@0 57
michael@0 58 return _stringTailMatch(str, prefix, start, end, true);
michael@0 59 }
michael@0 60
michael@0 61 /*
michael@0 62 endsWith(str, suffix[, start[, end]]) -> bool
michael@0 63
michael@0 64 Return true if str ends with the specified suffix, false otherwise.
michael@0 65 With optional start, test str beginning at that position.
michael@0 66 With optional end, stop comparing str at that position.
michael@0 67 suffix can also be an array of strings to try.
michael@0 68 */
michael@0 69 function endsWith(str, suffix, start, end) {
michael@0 70 if (arguments.length < 2) {
michael@0 71 throw new TypeError('endsWith() requires at least 2 arguments');
michael@0 72 }
michael@0 73
michael@0 74 // check if start and end are null/undefined or a 'number'
michael@0 75 if ((start == null) || (isNaN(new Number(start)))) {
michael@0 76 start = 0;
michael@0 77 }
michael@0 78 if ((end == null) || (isNaN(new Number(end)))) {
michael@0 79 end = Number.MAX_VALUE;
michael@0 80 }
michael@0 81
michael@0 82 // if it's an array
michael@0 83 if (typeof suffix == "object") {
michael@0 84 for (var i = 0, j = suffix.length; i < j; i++) {
michael@0 85 var res = _stringTailMatch(str, suffix[i], start, end, false);
michael@0 86 if (res) {
michael@0 87 return true;
michael@0 88 }
michael@0 89 }
michael@0 90 return false;
michael@0 91 }
michael@0 92
michael@0 93 return _stringTailMatch(str, suffix, start, end, false);
michael@0 94 }
michael@0 95
michael@0 96 /*
michael@0 97 Matches the end (direction == false) or start (direction == true) of str
michael@0 98 against substr, using the start and end arguments. Returns false
michael@0 99 if not found and true if found.
michael@0 100 */
michael@0 101 function _stringTailMatch(str, substr, start, end, fromStart) {
michael@0 102 var len = str.length;
michael@0 103 var slen = substr.length;
michael@0 104
michael@0 105 var indices = _adjustIndices(start, end, len);
michael@0 106 start = indices[0]; end = indices[1]; len = indices[2];
michael@0 107
michael@0 108 if (fromStart) {
michael@0 109 if (start + slen > len) {
michael@0 110 return false;
michael@0 111 }
michael@0 112 } else {
michael@0 113 if (end - start < slen || start > len) {
michael@0 114 return false;
michael@0 115 }
michael@0 116 if (end - slen > start) {
michael@0 117 start = end - slen;
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 if (end - start >= slen) {
michael@0 122 return str.substr(start, slen) == substr;
michael@0 123 }
michael@0 124 return false;
michael@0 125 }
michael@0 126
michael@0 127 function _adjustIndices(start, end, len)
michael@0 128 {
michael@0 129 if (end > len) {
michael@0 130 end = len;
michael@0 131 } else if (end < 0) {
michael@0 132 end += len;
michael@0 133 }
michael@0 134
michael@0 135 if (end < 0) {
michael@0 136 end = 0;
michael@0 137 }
michael@0 138 if (start < 0) {
michael@0 139 start += len;
michael@0 140 }
michael@0 141 if (start < 0) {
michael@0 142 start = 0;
michael@0 143 }
michael@0 144
michael@0 145 return [start, end, len];
michael@0 146 }

mercurial