Function lastSegment

  • Returns the last segment of a given path string.

    Example

    lastSegment('/home/user/documents/file.txt').value
    // => file.txt

    lastSegment('/home/user/documents/').value
    // => ''

    lastSegment('/home/user/documents').value
    // => 'documents'

    lastSegment('file.txt').value
    // => 'file.txt'

    lastSegment('').value
    // => ''

    const seg1 = lastSegment(null)
    seg1.isOk()
    // => false
    seg1.isErr()
    // => true
    seg1.error.message
    // => [paths.lastSegment] Expected string value as input

    const seg2 = lastSegment(12345)
    seg2.isOk()
    // => false
    seg2.isErr()
    // => true
    seg2.error.message
    // => Error: [paths.lastSegment] Expected string value as input

    Parameters

    • path: string

      The path string.

    Returns Result<string, Error>

    A Result with the last segment as a string on success, and an Error object if the provided input is not a string.

Generated using TypeDoc v0.24.7