or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-endswith

Checks if string ends with the given target string with optional position parameter

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.endswith@4.2.x

To install, run

npx @tessl/cli install tessl/npm-lodash-endswith@4.2.0

index.mddocs/

Lodash endsWith

Lodash endsWith is a string utility function that checks if a string ends with a given target string. It provides enhanced functionality beyond the native JavaScript String.prototype.endsWith() method by supporting a position parameter that allows checking if the string ends with the target at a specific position rather than just at the end.

Package Information

  • Package Name: lodash
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash

Core Imports

// Full lodash library
import _ from "lodash";
// Use as: _.endsWith(string, target, position)
// Modular import (recommended)
import endsWith from "lodash/endsWith";
// Use as: endsWith(string, target, position)

For CommonJS:

// Full library
const _ = require("lodash");

// Modular import
const endsWith = require("lodash/endsWith");

Basic Usage

import endsWith from "lodash/endsWith";

// Basic string ending check
endsWith("hello world", "world");
// => true

endsWith("hello world", "hello");
// => false

// Check with position parameter
endsWith("hello world", "hello", 5);
// => true (checks if "hello" ends at position 5)

// Edge cases
endsWith("test", "");
// => true (empty string always matches)

endsWith("", "test");
// => false

endsWith("abc", "c", 3);
// => true (position >= string length)

Capabilities

String Ending Detection

Checks if a string ends with the given target string, with optional position parameter for enhanced control.

/**
 * Checks if `string` ends with the given target string.
 * @param {string} [string=''] - The string to search
 * @param {string} [target] - The string to search for
 * @param {number} [position=string.length] - The position to search from
 * @returns {boolean} Returns `true` if `string` ends with `target`, else `false`
 */
function endsWith(string, target, position): boolean;

Parameters:

  • string (string, optional): The string to search. null and undefined values are converted to empty string '', other values converted to string using coercion.
  • target (string, optional): The string to search for. Gets converted to string using string coercion (target + '').
  • position (number, optional): The position to search from. Defaults to string.length if undefined. Gets clamped to range [0, string.length].

Returns:

  • boolean: Returns true if string ends with target at the specified position, else false.

Behavior:

  • Performs string conversion on both string and target parameters
  • Position parameter allows checking if string ends with target at a specific position rather than just at the end
  • Position values are clamped to valid range: negative positions become 0, positions beyond string length become string length
  • Empty target string always returns true regardless of position
  • Uses efficient indexOf method for string matching
  • Supports decimal position values (truncated to integer)

Usage Examples:

import endsWith from "lodash/endsWith";

// Standard usage
endsWith("JavaScript", "Script");
// => true

endsWith("JavaScript", "Java");
// => false

// Position-based checking
endsWith("JavaScript", "Java", 4);
// => true (checks if "Java" ends at position 4)

endsWith("JavaScript", "Script", 6);
// => false ("Script" doesn't end at position 6)

// Edge cases
endsWith("test", "", 2);
// => true (empty target always matches)

endsWith("hello", "lo", 5);
// => true (position 5 equals string length)

endsWith("hello", "lo", 10);
// => true (position beyond length treated as string length)

// Type coercion examples
endsWith("123", 3);
// => true (number 3 converted to string "3")

endsWith(null, "");
// => true (null converted to empty string "")

endsWith(undefined, "");
// => true (undefined converted to empty string "")

Error Handling

The endsWith function handles all parameter types gracefully through type coercion:

  • Falsy string values are converted to their string representations
  • Non-string target values are converted using string coercion
  • Invalid position values are handled by clamping to valid range
  • No exceptions are thrown for normal usage patterns

Browser and Environment Support

  • Node.js: All versions
  • Browsers: All modern browsers (IE 9+)
  • Module Systems: CommonJS, AMD, UMD, ES modules
  • Global: Available as _.endsWith when lodash is loaded globally