rolling-ts-utils
v1.0.8
Published
A small library which exposes some helpful generic utility types
Downloads
7
Maintainers
Readme
Rolling TS Utils
A small library which exposes some helpful generic utility types. This is an early version and currently contains only a few types, but will be gradually expanded and improved.
Installation
npm install --save-dev rolling-ts-utils
Basic Usage
import type { ReplaceStringPart } from "rolling-ts-utils";
const str = "This is a cool {noun}." as const;
let sentence: ReplaceStringPart<typeof str, "dog">;
// ^? typeof sentence = "This is a cool dog."
Available Types
ReplaceStringPart<OriginalString, NewString>
OriginalString
: The string to be replacedNewString
: The string to replace the first occurrence of{string}
, wherestring
is any string- If no
{string}
is found, the original string is returned
const str = "This is a cool {noun}." as const;
let sentence: ReplaceStringPart<typeof str, "dog">;
// ^? typeof sentence = "This is a cool dog."
ReplaceStringPart<OriginalString, NewString, Match>
OriginalString
: The string to be replacedNewString
: The string to be inserted at the first occurance ofMatch
Match
: The substring to be replaced- If no match is found, the original string is returned
const str = "This is a cool {noun}." as const;
let sentence: ReplaceStringPart<typeof str, "cat", "{noun}">;
// ^? typeof sentence = "This is a cool cat."
ReplaceStringPartGlobal<OriginalString, NewString, Match>
OriginalString
: The string to be replacedNewString
: The string to be inserted at each occurance ofMatch
Match
: The substring to be replaced- If no match is found, the original string is returned
const str = "This is {word} {word} {thing}." as const;
let sentence: ReplaceStringPartGlobal<typeof str, "dog", "{word}">;
// ^? typeof sentence = "This is dog dog {thing}."
ReplaceOrderedStringParts<OriginalString, NewStrings, Index>
OriginalString
: The string to be replacedNewStrings
: An array of strings to be inserted in orderIndex
: The index of the substring to be replaced (this should normally be left empty)- If no
{string}
is found, the original string is returned
This will break if the NewStrings
array contains more than 1000 elements
const str = "This is {article} {adjective} {noun}." as const;
let sentence: ReplaceOrderedStringParts<
typeof str,
["an", "amazing", "rabbit"]
>;
// ^? typeof sentence = "This is an amazing rabbit."
ReplaceMultipleStringParts<OriginalString, Keys, Values, Index>
OriginalString
: The string to be replacedKeys
: An array of substrings to be replacedValues
: An array of strings to be inserted in place of the key at the same position in theKeys
arrayIndex
: The index of the substring to be replaced (this should normally be left empty)- If no match is found, the key-value pair has no impact
This will break if the Keys
array contains more than 1000 elements
const str = "This is {article} {adjective} {noun}." as const;
let sentence: ReplaceMultipleStringParts<
typeof str,
["{article}", "{adjective}", "{noun}"],
["an", "interesting", "duck"]
>;
// ^? typeof sentence = "This is an interesting duck."
ReplaceAllStringParts<OriginalString, NewString>
OriginalString
: The string to be replacedNewString
: The string to be inserted in place of ALL occurrences of{string}
, wherestring
is any string
const str = "This is {article} {adjective} {noun}." as const;
let sentence: ReplaceAllStringParts<typeof str, "dog">;
// ^? typeof sentence = "This is dog dog dog."
Inc<OriginalNum>
OriginalNum
: A number to be incremented
This will break if OriginalNum
is greater than 999
type OriginalNum = 5;
type IncrementedNum = Inc<OriginalNum>;
// ^? type IncrementedNum = 6