-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat(string): add strTruncate,strCount strAt and strMatchAll helpers, shared literal regex helper, and coverage #530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * @nevware21/ts-utils | ||
| * https://github.com/nevware21/ts-utils | ||
| * | ||
| * Copyright (c) 2026 NevWare21 Solutions LLC | ||
| * Licensed under the MIT license. | ||
| */ | ||
|
|
||
| import { polyArrAt } from "../array/at"; | ||
| import { StrProto } from "../internal/constants"; | ||
| import { _throwIfNullOrUndefined } from "../internal/throwIf"; | ||
| import { _unwrapFunctionWithPoly } from "../internal/unwrapFunction"; | ||
| import { mathToInt } from "../math/to_int"; | ||
| import { asString } from "./as_string"; | ||
|
|
||
| /** | ||
| * The `strAt()` method takes an integer value and returns a new string consisting of the single | ||
| * UTF-16 code unit located at the specified offset into the string. It accepts both positive and | ||
| * negative integers: negative integers count back from the last string character. | ||
| * | ||
| * This is the equivalent of `String.prototype.at()` and falls back to {@link polyStrAt} in | ||
| * environments where the native method is unavailable. | ||
| * | ||
| * @function | ||
| * @since 0.14.0 | ||
| * @group String | ||
| * @param value - The string value to retrieve a character from. | ||
| * @param index - The zero-based index of the character to return. A negative index counts from | ||
| * the end of the string: `-1` returns the last character, `-2` the second-to-last, and so on. | ||
| * @returns A single-character string at the given index, or `undefined` if the index is out of | ||
| * range. | ||
| * @throws `TypeError` if `value` is `null` or `undefined`. | ||
| * @example | ||
| * ```ts | ||
| * strAt("hello", 0); // "h" | ||
| * strAt("hello", 1); // "e" | ||
| * strAt("hello", -1); // "o" — last character | ||
| * strAt("hello", -2); // "l" — second-to-last | ||
| * strAt("hello", 99); // undefined — out of range | ||
| * strAt("hello", -99); // undefined — out of range | ||
| * ``` | ||
| */ | ||
| export const strAt: (value: string, index: number) => string | undefined = (/*#__PURE__*/_unwrapFunctionWithPoly("at", StrProto as any, polyStrAt)); | ||
|
|
||
| /** | ||
| * Polyfill implementation of `String.prototype.at()` that returns the character at the given | ||
| * integer index, supporting negative indices which count back from the end of the string. | ||
| * | ||
| * Delegates index normalisation and bounds checking to {@link polyArrAt} by treating the string | ||
| * as an array-like object, matching native `String.prototype.at()` behaviour exactly. | ||
| * | ||
| * @since 0.14.0 | ||
| * @group String | ||
| * @group Polyfill | ||
| * @param value - The string value to retrieve a character from. | ||
| * @param index - The zero-based index of the character to return. Negative values count from the | ||
| * end: `-1` is the last character, `-2` is the second-to-last, and so on. | ||
| * @returns A single-character string at the normalised index, or `undefined` if out of range. | ||
| * @throws `TypeError` if `value` is `null` or `undefined`. | ||
| * @example | ||
| * ```ts | ||
| * polyStrAt("hello", 0); // "h" | ||
| * polyStrAt("hello", -1); // "o" | ||
| * polyStrAt("hello", -2); // "l" | ||
| * polyStrAt("hello", 99); // undefined | ||
| * polyStrAt("hello", -99); // undefined | ||
| * ``` | ||
| */ | ||
| /*#__NO_SIDE_EFFECTS__*/ | ||
| export function polyStrAt(value: string, index: number): string | undefined { | ||
| _throwIfNullOrUndefined(value); | ||
|
|
||
| // Reuse the Array.at polyfill for index normalization and bounds behavior. | ||
| return polyArrAt(asString(value), mathToInt(index)); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.