From a6bb922b24f0d8e6adb883b8cc49d4f6937e1448 Mon Sep 17 00:00:00 2001 From: samiahmedsiddiqui Date: Sat, 26 Oct 2019 19:12:09 +0500 Subject: [PATCH] Bump to v1.1.0 --- README.md | 4 +-- index.js | 75 +++++----------------------------------------------- package.json | 5 +++- 3 files changed, 12 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index ac6ef91..a239feb 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,13 @@ yarn add trim-words ## Usage -Import Package in `Node.js` +Import Package in `Node.js`. ``` var trimWords = require('trim-words'); ``` -Import Package in `React` +Import Package in `React`. ``` import trimWords from 'trim-words'; diff --git a/index.js b/index.js index cca49a0..f851ff0 100755 --- a/index.js +++ b/index.js @@ -1,73 +1,6 @@ 'use strict'; -function stripTags(string, remove_breaks) { - string = string.replace(/<(script|style)[^>]*?>.*?<\/\\1>/si, ''); - - // Replace all Tags - string = string.replace(/<[^>]*>/g, ''); - if (remove_breaks) { - string = string.replace(/[\r\n\t ]+/, ' '); - } - - return trim(string); -} - -function trim(str, charlist) { - var whitespace = [ - ' ', - '\n', - '\r', - '\t', - '\f', - '\x0b', - '\xa0', - '\u2000', - '\u2001', - '\u2002', - '\u2003', - '\u2004', - '\u2005', - '\u2006', - '\u2007', - '\u2008', - '\u2009', - '\u200a', - '\u200b', - '\u2028', - '\u2029', - '\u3000' - ].join(''); - var finalString = ''; - var l = 0; - var i = 0; - str += ''; - - if (charlist) { - whitespace = (charlist + '').replace(/([[\]().?/*{}+$^:])/g, '$1'); - } - - l = str.length; - for (i = 0; i < l; i += 1) { - if (whitespace.indexOf(str.charAt(i)) === -1) { - str = str.substring(i); - break; - } - } - - l = str.length; - for (i = l - 1; i >= 0; i -= 1) { - if (whitespace.indexOf(str.charAt(i)) === -1) { - str = str.substring(0, i + 1); - break; - } - } - - if (whitespace.indexOf(str.charAt(0)) === -1) { - finalString = str; - } - - return finalString; -} +var phpTrim = require('trim-php'); function trimWords(text, numWords, more) { if (!numWords) { @@ -77,7 +10,11 @@ function trimWords(text, numWords, more) { more = '…'; } - text = stripTags(text); + // StripTags + text = text.replace(/<(script|style)[^>]*?>.*?<\/\\1>/si, ''); + text = text.replace(/<[^>]*>/g, ''); + text = phpTrim.trimPhp(text); + var wordsArray = text.split(/[\n\r\t ]+/, numWords + 1); var sep = ' '; diff --git a/package.json b/package.json index aaf9188..32a96f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "trim-words", - "version": "1.0.0", + "version": "1.1.0", "description": "Trims text to a certain number of words.", "main": "index.js", "scripts": { @@ -23,5 +23,8 @@ "url": "https://github.com/samiahmedsiddiqui/trim-words/issues" }, "homepage": "https://github.com/samiahmedsiddiqui/trim-words#readme", + "dependencies": { + "trim-php": "^1.0.0" + }, "runkitExampleFilename": "test/runkit.js" }