(marginChar: string): (self: string) => string
(self: string, marginChar: string): stringStrips a leading margin prefix from every line using the supplied margin character.
Example (Stripping custom margins)
import { String } from "effect"
const text = " |hello\n |world"
const result = String.stripMarginWith(text, "|")
console.log(result) // "hello\nworld"export const const stripMarginWith: {
(marginChar: string): (self: string) => string
(self: string, marginChar: string): string
}
Strips a leading margin prefix from every line using the supplied margin
character.
Example (Stripping custom margins)
import { String } from "effect"
const text = " |hello\n |world"
const result = String.stripMarginWith(text, "|")
console.log(result) // "hello\nworld"
stripMarginWith: {
(marginChar: stringmarginChar: string): (self: stringself: string) => string
(self: stringself: string, marginChar: stringmarginChar: string): string
} = dual<(...args: Array<any>) => any, (self: string, marginChar: string) => string>(arity: 2, body: (self: string, marginChar: string) => string): ((...args: Array<any>) => any) & ((self: string, marginChar: string) => string) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(2, (self: stringself: string, marginChar: stringmarginChar: string): string => {
let let out: stringout = ""
for (const const line: stringline of const linesWithSeparators: (
s: string
) => LinesIterator
Returns an IterableIterator which yields each line contained within the
string as well as the trailing newline character.
Example (Iterating lines with separators)
import { String } from "effect"
const lines = String.linesWithSeparators("hello\nworld\n")
console.log(Array.from(lines)) // ["hello\n", "world\n"]
linesWithSeparators(self: stringself)) {
let let index: numberindex = 0
while (let index: numberindex < const line: stringline.String.length: numberReturns the length of a String object.
length && const line: stringline.String.charAt(pos: number): stringReturns the character at the specified index.
charAt(let index: numberindex) <= " ") {
let index: numberindex = let index: numberindex + 1
}
const const stripped: stringstripped = let index: numberindex < const line: stringline.String.length: numberReturns the length of a String object.
length && const line: stringline.String.charAt(pos: number): stringReturns the character at the specified index.
charAt(let index: numberindex) === marginChar: stringmarginChar
? const line: stringline.String.substring(start: number, end?: number): stringReturns the substring at the specified location within a String object.
substring(let index: numberindex + 1)
: const line: stringline
let out: stringout = let out: stringout + const stripped: stringstripped
}
return let out: stringout
})