Giter VIP home page Giter VIP logo

truncate's Introduction

truncate

truncate

Return a nicely shortened string if over a set upper limit (default 75 characters)

What is nicely shortened? Consider this line from Orwell's 1984...
0---------1---------2---------3---------4---------5---------6---------7---->
When we are omnipotent we shall have no more need of science. There will be

If the limit is set to 70, a hard truncation would result in...
When we are omnipotent we shall have no more need of science. There wi...

Truncating to the nearest space might be better...
When we are omnipotent we shall have no more need of science. There...

The best truncation would be...
When we are omnipotent we shall have no more need of science...

Therefore, the returned string will be, in priority...

1. If the string is less than the limit, just return the whole string
2. If the string has a period, return the string from zero to the first
    period from the right
3. If the string has no period, return the string from zero to the first
    space
4. If there is no space or period in the range return a hard truncation

In all cases, the string returned will have ellipsis appended unless
otherwise specified.

Parameters:
    s = string to be truncated as a String
    min_pos = minimum character index to return as Integer (returned
              string will be at least this long - default 0)
    max_pos = maximum character index to return as Integer (returned
              string will be at most this long - default 75)
    ellipsis = returned string will have an ellipsis appended to it
               before it is returned if this is set as Boolean
               (default is True)
Returns:
    Truncated String
Throws:
    ValueError exception if min_pos > max_pos, indicating improper
    configuration
Usage:
short_string = trunc(some_long_string)
or
shorter_string = trunc(some_long_string,max_pos=15,ellipsis=False)

truncate's People

Contributors

ansother avatar

Watchers

 avatar

Forkers

sherzberg

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.