Comparing version 2.0.0 to 2.1.0
@@ -135,6 +135,7 @@ /** | ||
*/ | ||
type Slice<T extends string, startIndex extends number = 0, endIndex extends number = Length<T>> = All<[ | ||
IsStringLiteral<T>, | ||
IsNumberLiteral<startIndex | endIndex> | ||
]> extends true ? T extends `${infer head}${infer rest}` ? startIndex extends 0 ? endIndex extends 0 ? '' : `${head}${Slice<rest, Math.Subtract<Math.GetPositiveIndex<T, startIndex>, 1>, Math.Subtract<Math.GetPositiveIndex<T, endIndex>, 1>>}` : `${Slice<rest, Math.Subtract<Math.GetPositiveIndex<T, startIndex>, 1>, Math.Subtract<Math.GetPositiveIndex<T, endIndex>, 1>>}` : '' : string; | ||
type Slice<T extends string, startIndex extends number = 0, endIndex extends number | undefined = undefined> = endIndex extends number ? _Slice<T, startIndex, endIndex> : _SliceStart<T, startIndex>; | ||
/** Slice with startIndex and endIndex */ | ||
type _Slice<T extends string, startIndex extends number, endIndex extends number, _result extends string = ''> = IsNumberLiteral<startIndex | endIndex> extends true ? T extends `${infer head}${infer rest}` ? IsStringLiteral<head> extends true ? startIndex extends 0 ? endIndex extends 0 ? _result : _Slice<rest, 0, Math.Subtract<Math.GetPositiveIndex<T, endIndex>, 1>, `${_result}${head}`> : _Slice<rest, Math.Subtract<Math.GetPositiveIndex<T, startIndex>, 1>, Math.Subtract<Math.GetPositiveIndex<T, endIndex>, 1>, _result> : startIndex | endIndex extends 0 ? _result : string : IsStringLiteral<T> extends true ? _result : startIndex | endIndex extends 0 ? _result : string : string; | ||
/** Slice with startIndex only */ | ||
type _SliceStart<T extends string, startIndex extends number, _result extends string = ''> = IsNumberLiteral<startIndex> extends true ? T extends `${infer head}${infer rest}` ? IsStringLiteral<head> extends true ? startIndex extends 0 ? T : _SliceStart<rest, Math.Subtract<Math.GetPositiveIndex<T, startIndex>, 1>, _result> : string : IsStringLiteral<T> extends true ? _result : startIndex extends 0 ? _result : string : string; | ||
/** | ||
@@ -148,5 +149,35 @@ * A strongly-typed version of `String.prototype.slice`. | ||
*/ | ||
declare function slice<T extends string, S extends number = 0, E extends number = Length<T>>(str: T, start?: S, end?: E): Slice<T, S, E>; | ||
declare function slice<T extends string, S extends number = 0, E extends number | undefined = undefined>(str: T, start?: S, end?: E): Slice<T, S, E>; | ||
/** | ||
* Checks if a string starts with another string. | ||
* T: The string to check. | ||
* S: The string to check against. | ||
* P: The position to start the search. | ||
*/ | ||
type StartsWith<T extends string, S extends string, P extends number = 0> = All<[IsStringLiteral<S>, IsNumberLiteral<P>]> extends true ? Math.IsNegative<P> extends false ? P extends 0 ? S extends `${infer SHead}${infer SRest}` ? T extends `${infer THead}${infer TRest}` ? IsStringLiteral<THead | SHead> extends true ? THead extends SHead ? StartsWith<TRest, SRest> : false : boolean : IsStringLiteral<T> extends true ? false : boolean : true : StartsWith<Slice<T, P>, S, 0> : StartsWith<T, S, 0> : boolean; | ||
/** | ||
* A strongly-typed version of `String.prototype.startsWith`. | ||
* @param text the string to search. | ||
* @param search the string to search with. | ||
* @param position the index to start search at. | ||
* @returns boolean, whether or not the text string starts with the search string. | ||
* @example startsWith('abc', 'a') // true | ||
*/ | ||
declare function startsWith<T extends string, S extends string, P extends number = 0>(text: T, search: S, position?: P): StartsWith<T, S, P>; | ||
/** | ||
* Reverses a string. | ||
* - `T` The string to reverse. | ||
*/ | ||
type Reverse<T extends string, _acc extends string = ''> = T extends `${infer Head}${infer Tail}` ? Reverse<Tail, `${Head}${_acc}`> : _acc extends '' ? T : `${T}${_acc}`; | ||
/** | ||
* A strongly-typed function to reverse a string. | ||
* @param str the string to reverse. | ||
* @returns the reversed string in both type level and runtime. | ||
* @example reverse('hello world') // 'dlrow olleh' | ||
*/ | ||
declare function reverse<T extends string>(str: T): Reverse<T, "">; | ||
/** | ||
* Checks if a string ends with another string. | ||
@@ -157,3 +188,9 @@ * T: The string to check. | ||
*/ | ||
type EndsWith<T extends string, S extends string, P extends number = Length<T>> = All<[IsStringLiteral<T | S>, IsNumberLiteral<P>]> extends true ? Math.IsNegative<P> extends false ? P extends Length<T> ? S extends Slice<T, Math.Subtract<Length<T>, Length<S>>, Length<T>> ? true : false : EndsWith<Slice<T, 0, P>, S, Length<T>> : false : boolean; | ||
type EndsWith<T extends string, S extends string, P extends number | undefined = undefined> = P extends number ? _EndsWith<T, S, P> : _EndsWithNoPosition<T, S>; | ||
type _EndsWith<T extends string, S extends string, P extends number> = All<[ | ||
IsStringLiteral<S>, | ||
IsNumberLiteral<P> | ||
]> extends true ? Math.IsNegative<P> extends false ? P extends Length<T> ? IsStringLiteral<T> extends true ? S extends Slice<T, Math.Subtract<Length<T>, Length<S>>, Length<T>> ? true : false : _EndsWithNoPosition<Slice<T, 0, P>, S> : _EndsWithNoPosition<Slice<T, 0, P>, S> : false : boolean; | ||
/** Overload of EndsWith without P */ | ||
type _EndsWithNoPosition<T extends string, S extends string> = StartsWith<Reverse<T>, Reverse<S>>; | ||
/** | ||
@@ -273,19 +310,2 @@ * A strongly-typed version of `String.prototype.endsWith`. | ||
/** | ||
* Checks if a string starts with another string. | ||
* T: The string to check. | ||
* S: The string to check against. | ||
* P: The position to start the search. | ||
*/ | ||
type StartsWith<T extends string, S extends string, P extends number = 0> = All<[IsStringLiteral<T | S>, IsNumberLiteral<P>]> extends true ? Math.IsNegative<P> extends false ? P extends 0 ? T extends `${S}${string}` ? true : false : StartsWith<Slice<T, P>, S, 0> : StartsWith<T, S, 0> : boolean; | ||
/** | ||
* A strongly-typed version of `String.prototype.startsWith`. | ||
* @param text the string to search. | ||
* @param search the string to search with. | ||
* @param position the index to start search at. | ||
* @returns boolean, whether or not the text string starts with the search string. | ||
* @example startsWith('abc', 'a') // true | ||
*/ | ||
declare function startsWith<T extends string, S extends string, P extends number = 0>(text: T, search: S, position?: P): StartsWith<T, S, P>; | ||
/** | ||
* Trims all whitespaces at the start of a string. | ||
@@ -346,15 +366,2 @@ * T: The string to trim. | ||
/** | ||
* Reverses a string. | ||
* - `T` The string to reverse. | ||
*/ | ||
type Reverse<T extends string, _acc extends string = ''> = T extends `${infer Head}${infer Tail}` ? Reverse<Tail, `${Head}${_acc}`> : _acc extends '' ? T : _acc; | ||
/** | ||
* A strongly-typed function to reverse a string. | ||
* @param str the string to reverse. | ||
* @returns the reversed string in both type level and runtime. | ||
* @example reverse('hello world') // 'dlrow olleh' | ||
*/ | ||
declare function reverse<T extends string>(str: T): Reverse<T, "">; | ||
/** | ||
* Truncate a string if it's longer than the given maximum length. | ||
@@ -805,2 +812,2 @@ * The last characters of the truncated string are replaced with the omission string which defaults to "...". | ||
export { CamelCase, CamelKeys, CharAt, Concat, ConstantCase, ConstantKeys, DeepCamelKeys, DeepConstantKeys, DeepDelimiterKeys, DeepKebabKeys, DeepPascalKeys, DeepSnakeKeys, DelimiterCase, DelimiterKeys, EndsWith, Includes, IsDigit, IsLetter, IsLower, IsSeparator, IsSpecial, IsUpper, Join, KebabCase, KebabKeys, Length, PadEnd, PadStart, PascalCase, PascalKeys, Repeat, Replace, ReplaceAll, Reverse, Slice, SnakeCase, SnakeKeys, Split, StartsWith, TitleCase, Trim, TrimEnd, TrimStart, Truncate, Words, camelCase, camelKeys, capitalize, charAt, concat, constantCase, constantKeys, deepCamelKeys, deepConstantKeys, deepDelimiterKeys, deepKebabKeys, deepPascalKeys, deepSnakeKeys, delimiterCase, delimiterKeys, endsWith, includes, join, kebabCase, kebabKeys, length, lowerCase, padEnd, padStart, pascalCase, pascalKeys, repeat, replace, replaceAll, reverse, slice, snakeCase, snakeKeys, split, startsWith, titleCase, toCamelCase, toConstantCase, toDelimiterCase, toKebabCase, toLowerCase, toPascalCase, toSnakeCase, toTitleCase, toUpperCase, trim, trimEnd, trimStart, truncate, uncapitalize, upperCase, words }; | ||
export { type CamelCase, type CamelKeys, type CharAt, type Concat, type ConstantCase, type ConstantKeys, type DeepCamelKeys, type DeepConstantKeys, type DeepDelimiterKeys, type DeepKebabKeys, type DeepPascalKeys, type DeepSnakeKeys, type DelimiterCase, type DelimiterKeys, type EndsWith, type Includes, type IsDigit, type IsLetter, type IsLower, type IsSeparator, type IsSpecial, type IsUpper, type Join, type KebabCase, type KebabKeys, type Length, type PadEnd, type PadStart, type PascalCase, type PascalKeys, type Repeat, type Replace, type ReplaceAll, type Reverse, type Slice, type SnakeCase, type SnakeKeys, type Split, type StartsWith, type TitleCase, type Trim, type TrimEnd, type TrimStart, type Truncate, type Words, camelCase, camelKeys, capitalize, charAt, concat, constantCase, constantKeys, deepCamelKeys, deepConstantKeys, deepDelimiterKeys, deepKebabKeys, deepPascalKeys, deepSnakeKeys, delimiterCase, delimiterKeys, endsWith, includes, join, kebabCase, kebabKeys, length, lowerCase, padEnd, padStart, pascalCase, pascalKeys, repeat, replace, replaceAll, reverse, slice, snakeCase, snakeKeys, split, startsWith, titleCase, toCamelCase, toConstantCase, toDelimiterCase, toKebabCase, toLowerCase, toPascalCase, toSnakeCase, toTitleCase, toUpperCase, trim, trimEnd, trimStart, truncate, uncapitalize, upperCase, words }; |
@@ -63,3 +63,3 @@ 'use strict'; | ||
// src/native/slice.ts | ||
function slice(str, start = 0, end = str.length) { | ||
function slice(str, start = 0, end = void 0) { | ||
return str.slice(start, end); | ||
@@ -149,3 +149,6 @@ } | ||
function capitalize(str) { | ||
return join([toUpperCase(charAt(str, 0)), slice(str, 1)]); | ||
return join([ | ||
toUpperCase(charAt(str, 0) ?? ""), | ||
slice(str, 1) | ||
]); | ||
} | ||
@@ -174,3 +177,6 @@ | ||
function uncapitalize(str) { | ||
return join([toLowerCase(charAt(str, 0)), slice(str, 1)]); | ||
return join([ | ||
toLowerCase(charAt(str, 0) ?? ""), | ||
slice(str, 1) | ||
]); | ||
} | ||
@@ -177,0 +183,0 @@ |
{ | ||
"name": "string-ts", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Strongly-typed string functions.", | ||
@@ -20,7 +20,7 @@ "main": "./dist/index.js", | ||
"@typescript-eslint/eslint-plugin": "latest", | ||
"@vitest/coverage-v8": "^1.0.2", | ||
"@vitest/coverage-v8": "^1.4.0", | ||
"eslint": "latest", | ||
"prettier": "latest", | ||
"tsup": "latest", | ||
"typescript": "^5.1.6", | ||
"typescript": "^5.4.2", | ||
"vitest": "latest" | ||
@@ -27,0 +27,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
124761
1344