UNPKG

@newdash/newdash

Version:

javascript/typescript utility library

45 lines (44 loc) 1.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startsWith = void 0; const baseClamp_1 = __importDefault(require("./.internal/baseClamp")); const baseToString_1 = __importDefault(require("./.internal/baseToString")); const toInteger_1 = __importDefault(require("./toInteger")); const toString_1 = __importDefault(require("./toString")); /** * Checks if `string` starts with the given target string. * * @since 5.2.0 * @category String * @param string The string to inspect. * @param target The string to search for. * @param position The position to search from. * @returns Returns `true` if `string` starts with `target`, * else `false`. * @see [[endsWith]], [[includes]] * @example * * ```js * startsWith('abc', 'a') * // => true * * startsWith('abc', 'b') * // => false * * startsWith('abc', 'b', 1) * // => true * ``` */ function startsWith(string = "", target, position = 0) { string = (0, toString_1.default)(string); position = position == null ? 0 : (0, baseClamp_1.default)((0, toInteger_1.default)(position), 0, string.length); target = (0, baseToString_1.default)(target); return string.slice(position, position + target.length) == target; } exports.startsWith = startsWith; exports.default = startsWith;