Giter VIP home page Giter VIP logo

truncator's Introduction

Truncator

npm version Build Status

Layout specific text truncator considering line length, content height or character length.

Installation

You can install truncator from npm.

$ npm install truncator --save

If you are using module loader such as Rollup or Webpack:

import { truncate } from 'truncator';

Also, you can use it from unpkg:

<!-- Normal build -->
<script src="https://unpkg.com/truncator"></script>
<!-- Minified build -->
<script src="https://unpkg.com/truncator/dist/truncator.min.js"></script>
var truncate = Truncator.truncate;

Usage

Just call truncate function then the specified text will be truncated.

var truncator = truncate(el, text, options);
  • el: HTMLElement that will be input text.
  • text: Truncate target string.
  • options: Truncate options object.
    • line, height or count
    • ellipsis: Ellipsis symbol. null indicates no symbol will be added. default: '...'

The returned object has the following methods:

  • recalc(): Retry to truncate the initially given el and text on the current state. It is useful if you want to adapt resizing the container element.
  • restoreText(): Restore the original text on el.
// Re-truncate the text
truncator.recalc();

// Restore the original text
truncator.restoreText();

Example

var el = document.getElementById('wrapper');
var truncator = truncate(el, 'Target text', { line: 3, ellipsis: null });

window.addEventListener('resize', function () {
  truncator.recalc();
});

License

MIT

truncator's People

Contributors

ktsn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

nvminhtu

truncator's Issues

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.