ends_with.js (2993B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; 24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 25 var format = require( './../../format' ); 26 27 28 // MAIN // 29 30 /** 31 * Test if a string ends with the characters of another string. 32 * 33 * @param {string} str - input string 34 * @param {string} search - search string 35 * @param {integer} [len=str.length] - substring length 36 * @throws {TypeError} first argument must be a string 37 * @throws {TypeError} second argument must be a string 38 * @throws {TypeError} third argument must be an integer 39 * @returns {boolean} boolean indicating if the input string ends with the search string 40 * 41 * @example 42 * var bool = endsWith( 'Remember the story I used to tell you when you were a boy?', 'boy?' ); 43 * // returns true 44 * 45 * @example 46 * var bool = endsWith( 'Remember the story I used to tell you when you were a boy?', 'Boy?' ); 47 * // returns false 48 * 49 * @example 50 * var bool = endsWith( 'To be, or not to be, that is the question.', 'to be' ); 51 * // returns false 52 * 53 * @example 54 * var bool = endsWith( 'To be, or not to be, that is the question.', 'to be', 19 ); 55 * // returns true 56 * 57 * @example 58 * var bool = endsWith( 'To be, or not to be, that is the question.', 'to be', -23 ); 59 * // returns true 60 */ 61 function endsWith( str, search, len ) { 62 var idx; 63 var i; 64 if ( !isString( str ) ) { 65 throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); 66 } 67 if ( !isString( search ) ) { 68 throw new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', search ) ); 69 } 70 if ( arguments.length > 2 ) { 71 if ( !isInteger( len ) ) { 72 throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', len ) ); 73 } 74 if ( len === 0 ) { 75 return ( search.length === 0 ); 76 } 77 if ( len < 0 ) { 78 idx = str.length + len; 79 } else { 80 idx = len; 81 } 82 } else { 83 idx = str.length; 84 } 85 if ( search.length === 0 ) { 86 // Based on the premise that every string can be "surrounded" by empty strings (e.g., "" + "a" + "" + "b" + "" === "ab"): 87 return true; 88 } 89 idx -= search.length; 90 if ( idx < 0 ) { 91 return false; 92 } 93 for ( i = 0; i < search.length; i++) { 94 if ( str.charCodeAt( idx + i ) !== search.charCodeAt( i ) ) { 95 return false; 96 } 97 } 98 return true; 99 } 100 101 102 // EXPORTS // 103 104 module.exports = endsWith;