Skip to content

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
samiahmedsiddiqui committed Nov 1, 2019
1 parent bdd16af commit 397e56b
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.coverage
.DS_Store
node_modules
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- 8
- 9
- 10
- 11
- 12
- node
os:
- linux
- osx
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Sami Ahmed Siddiqui <sami.siddiqui@yasglobal.com> (https://www.yasglobal.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
91 changes: 90 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,91 @@
# trim-characters
Trims text to a certain number of characters.

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Dependency Status][dependency-image]][dependency-url]

Trims text to a certain number of characters. You can define either to break word to maintain the number of characters limit or characters limit may decreases to avoid breaking words.

## Install

Via `npm`
```
npm install trim-characters
```

Via Yarn
```
yarn add trim-characters
```

## Usage

Import Package in `Node.js`.

```
var trimCharacters = require('trim-characters');
```

Import Package in `React`.

```
import trimCharacters from 'trim-characters';
```

## Example

### Trims text to a certain number of characters in `Node.js`.

```
var trimCharacters = require('trim-characters');
var text = '<h1>What is Geostatistics?</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt quam ut ligula ullamcorper interdum. Nulla malesuada purus tristique justo tristique, id posuere purus tristique. Pellentesque non magna ut libero elementum interdum vel vitae ante. Sed porta auctor urna eget venenatis. Mauris nec convallis metus. Sed at dui elit. Donec rhoncus justo neque, finibus commodo dui posuere ut. Maecenas in mi enim. Quisque maximus enim nunc.</p> <p>Donec eu ultricies ipsum. Fusce eget pellentesque urna. Vestibulum lacinia laoreet mi nec posuere. Duis vel elit elementum, scelerisque eros a, sodales eros. Praesent hendrerit neque velit, nec pretium ipsum finibus facilisis. Proin ultricies sem in sapien consectetur dictum.</p>';
var trimmedChar1 = trimCharacters(text, 70, false, '...');
var trimmedChar2 = trimCharacters(text, 70, true, '...');
console.log('Trim Characters with breaking words: ', trimmedChar1);
console.log('Trim Characters without breaking words: ', trimmedChar2);
```

### Trims text to a certain number of characters in `React`.

```
import trimCharacters from 'trim-characters';
var text = '<h1>What is Geostatistics?</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt quam ut ligula ullamcorper interdum. Nulla malesuada purus tristique justo tristique, id posuere purus tristique. Pellentesque non magna ut libero elementum interdum vel vitae ante. Sed porta auctor urna eget venenatis. Mauris nec convallis metus. Sed at dui elit. Donec rhoncus justo neque, finibus commodo dui posuere ut. Maecenas in mi enim. Quisque maximus enim nunc.</p> <p>Donec eu ultricies ipsum. Fusce eget pellentesque urna. Vestibulum lacinia laoreet mi nec posuere. Duis vel elit elementum, scelerisque eros a, sodales eros. Praesent hendrerit neque velit, nec pretium ipsum finibus facilisis. Proin ultricies sem in sapien consectetur dictum.</p>';
var trimmedChar1 = trimCharacters(text, 70, false, '...');
var trimmedChar2 = trimCharacters(text, 70, true, '...');
console.log('Trim Characters with breaking words: ', trimmedChar1);
console.log('Trim Characters without breaking words: ', trimmedChar2);
```

## Parameters

| Attributes | Type | Required | Default | Description |
| ----------- | ----- | ----------- | -------- |-------------- |
| text | String | Yes | | Text to trim. |
| charLength | Integer | No | `155` | Number of characters. |
| breakWord | Boolean | No | `false` | Either to break words or not. If it is set to `true` then `charLength` may decreases to **AVOID** breaking words. |
| more | String | No | &hellip; | What to append if `text` needs to be trimmed. |

## Return

Trimmed text.

## Tested

This package is tested with the `Node.js` and `React` Application.

[npm-image]: https://img.shields.io/npm/v/trim-characters.svg
[npm-url]: https://www.npmjs.com/package/trim-characters
[downloads-image]: https://img.shields.io/npm/dm/trim-characters.svg

[travis-image]: https://img.shields.io/travis/com/samiahmedsiddiqui/trim-characters.svg?label=travis-ci
[travis-url]: https://travis-ci.com/samiahmedsiddiqui/trim-characters

[appveyor-url]: https://ci.appveyor.com/project/samiahmedsiddiqui/trim-characters
[appveyor-image]: https://img.shields.io/appveyor/ci/samiahmedsiddiqui/trim-characters.svg?label=appveyor

[dependency-image]: https://img.shields.io/david/samiahmedsiddiqui/trim-characters.svg
[dependency-url]: https://david-dm.org/samiahmedsiddiqui/trim-characters
22 changes: 22 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
environment:
matrix:
# node.js
- nodejs_version: "8"
- nodejs_version: "9"
- nodejs_version: "10"
- nodejs_version: "11"
- nodejs_version: "12"
- nodejs_version: ""

install:
- ps: Install-Product node $env:nodejs_version
- npm install

test_script:
- node --version
- npm --version
- cmd: npm test

build: off

version: "{build}"
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var phpTrim = require('trim-php');

function trimCharacters(text, charLength, breakWord, more) {
var sep = ' ';

if (!charLength) {
charLength = 155;
}

if (!more) {
more = '&hellip;';
}

// StripTags
text = text.replace(/<(script|style)[^>]*?>.*?<\/\\1>/si, '');
text = text.replace(/<[^>]*>/g, '');
text = phpTrim.trimPhp(text);
text = text.replace(/[\n\r\t]+/, ' ');

if (text.length > charLength) {
if (breakWord) {
text = text.substr(0, text.lastIndexOf(sep, charLength));
} else {
text = text.substring(0, charLength);
}

text = text + more;
}

return text;
}

module.exports = trimCharacters;
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "trim-characters",
"version": "1.0.0",
"description": "Trims text to a certain number of characters.",
"main": "index.js",
"scripts": {
"test": "node test/all.js"
},
"repository": {
"type": "git",
"url": "https://github.com/samiahmedsiddiqui/trim-characters.git"
},
"keywords": [
"react",
"trim",
"characters",
"break-word",
"without-break-word"
],
"author": "Sami Ahmed Siddiqui <sami.siddiqui@yasglobal.com> (https://www.yasglobal.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/samiahmedsiddiqui/trim-characters/issues"
},
"homepage": "https://github.com/samiahmedsiddiqui/trim-characters#readme",
"dependencies": {
"trim-php": "^1.0.0"
},
"runkitExampleFilename": "test/runkit.js"
}
22 changes: 22 additions & 0 deletions test/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var trimCharacters = require('../index');

var text = '<h1>What is Geostatistics?</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt quam ut ligula ullamcorper interdum. Nulla malesuada purus tristique justo tristique, id posuere purus tristique. Pellentesque non magna ut libero elementum interdum vel vitae ante. Sed porta auctor urna eget venenatis. Mauris nec convallis metus. Sed at dui elit. Donec rhoncus justo neque, finibus commodo dui posuere ut. Maecenas in mi enim. Quisque maximus enim nunc.</p> <p>Donec eu ultricies ipsum. Fusce eget pellentesque urna. Vestibulum lacinia laoreet mi nec posuere. Duis vel elit elementum, scelerisque eros a, sodales eros. Praesent hendrerit neque velit, nec pretium ipsum finibus facilisis. Proin ultricies sem in sapien consectetur dictum.</p>';

var trimmedChar1 = trimCharacters(text, 70, false, '...');
var trimmedChar2 = trimCharacters(text, 70, true, '...');

console.log('-------------------------------');
console.log('Original Text is:');
console.log('-------------------------------');
console.log(text);
console.log('\n');
console.log('-------------------------------');
console.log('Trim Characters with breaking words:');
console.log('-------------------------------');
console.log(trimmedChar1);
console.log('\n');
console.log('-------------------------------');
console.log('Trim Characters without breaking words:');
console.log('-------------------------------');
console.log(trimmedChar2);
console.log('\n');
15 changes: 15 additions & 0 deletions test/runkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var trimCharacters = require('trim-characters');

var text = '<h1>What is Geostatistics?</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt quam ut ligula ullamcorper interdum. Nulla malesuada purus tristique justo tristique, id posuere purus tristique. Pellentesque non magna ut libero elementum interdum vel vitae ante. Sed porta auctor urna eget venenatis. Mauris nec convallis metus. Sed at dui elit. Donec rhoncus justo neque, finibus commodo dui posuere ut. Maecenas in mi enim. Quisque maximus enim nunc.</p> <p>Donec eu ultricies ipsum. Fusce eget pellentesque urna. Vestibulum lacinia laoreet mi nec posuere. Duis vel elit elementum, scelerisque eros a, sodales eros. Praesent hendrerit neque velit, nec pretium ipsum finibus facilisis. Proin ultricies sem in sapien consectetur dictum.</p>';

var trimmedChar1 = trimCharacters(text, 70, false, '...');
var trimmedChar2 = trimCharacters(text, 70, true, '...');

console.log('Original Text is:');
console.log(text);
console.log('Trim Characters with breaking words:');
console.log(trimmedChar1);
console.log('Trim Characters without breaking words:');
console.log(trimmedChar2);

process.exit();

0 comments on commit 397e56b

Please sign in to comment.