Comparing version 0.0.9 to 1.0.0
124
index.js
@@ -0,1 +1,57 @@ | ||
const ELLIPSIS_CHARACTER = '\u2026' | ||
function lineClamp (rootElement, lineCount, options) { | ||
rootElement.style.cssText += | ||
'overflow:hidden;overflow-wrap:break-word;word-wrap:break-word' | ||
const maximumHeight = | ||
(lineCount || 1) * | ||
parseInt(window.getComputedStyle(rootElement).lineHeight, 10) | ||
// Exit if text does not overflow `rootElement`. | ||
if (rootElement.scrollHeight <= maximumHeight) { | ||
return false | ||
} | ||
return truncateElementNode( | ||
rootElement, | ||
rootElement, | ||
maximumHeight, | ||
(options && options.ellipsis) || ELLIPSIS_CHARACTER | ||
) | ||
} | ||
function truncateElementNode ( | ||
element, | ||
rootElement, | ||
maximumHeight, | ||
ellipsisCharacter | ||
) { | ||
const childNodes = element.childNodes | ||
let i = childNodes.length - 1 | ||
while (i > -1) { | ||
const childNode = childNodes[i--] | ||
if ( | ||
(childNode.nodeType === 1 && | ||
truncateElementNode( | ||
childNode, | ||
rootElement, | ||
maximumHeight, | ||
ellipsisCharacter | ||
)) || | ||
(childNode.nodeType === 3 && | ||
truncateTextNode( | ||
childNode, | ||
rootElement, | ||
maximumHeight, | ||
ellipsisCharacter | ||
)) | ||
) { | ||
return true | ||
} | ||
element.removeChild(childNode) | ||
} | ||
return false | ||
} | ||
function truncateTextNode ( | ||
@@ -7,4 +63,4 @@ textNode, | ||
) { | ||
var lastIndexOfWhitespace | ||
var textContent = textNode.textContent | ||
let lastIndexOfWhitespace | ||
let textContent = textNode.textContent | ||
while (textContent.length > 1) { | ||
@@ -30,3 +86,4 @@ lastIndexOfWhitespace = textContent.lastIndexOf(' ') | ||
var TRAILING_WHITESPACE_AND_PUNCTUATION_REGEX = /[ .,;!?'‘’“”\-–—]+$/ | ||
const TRAILING_WHITESPACE_AND_PUNCTUATION_REGEX = /[ .,;!?'‘’“”\-–—]+$/ | ||
function truncateTextNodeByCharacter ( | ||
@@ -38,4 +95,4 @@ textNode, | ||
) { | ||
var textContent = textNode.textContent | ||
var length = textContent.length | ||
let textContent = textNode.textContent | ||
let length = textContent.length | ||
while (length > 1) { | ||
@@ -55,57 +112,2 @@ // Trim off one trailing character and any trailing punctuation and whitespace. | ||
function truncateElementNode ( | ||
element, | ||
rootElement, | ||
maximumHeight, | ||
ellipsisCharacter | ||
) { | ||
var childNodes = element.childNodes | ||
var i = childNodes.length - 1 | ||
while (i > -1) { | ||
var childNode = childNodes[i--] | ||
var nodeType = childNode.nodeType | ||
if ( | ||
(nodeType === 1 && | ||
truncateElementNode( | ||
childNode, | ||
rootElement, | ||
maximumHeight, | ||
ellipsisCharacter | ||
)) || | ||
(nodeType === 3 && | ||
truncateTextNode( | ||
childNode, | ||
rootElement, | ||
maximumHeight, | ||
ellipsisCharacter | ||
)) | ||
) { | ||
return true | ||
} | ||
element.removeChild(childNode) | ||
} | ||
return false | ||
} | ||
var ELLIPSIS_CHARACTER = '\u2026' | ||
module.exports = function (rootElement, lineCount, options) { | ||
rootElement.style.cssText += | ||
'overflow:hidden;overflow-wrap:break-word;word-wrap:break-word' | ||
var maximumHeight = | ||
(lineCount || 1) * | ||
parseInt(window.getComputedStyle(rootElement).lineHeight, 10) | ||
// Exit if text does not overflow `rootElement`. | ||
if (rootElement.scrollHeight <= maximumHeight) { | ||
return false | ||
} | ||
return truncateElementNode( | ||
rootElement, | ||
rootElement, | ||
maximumHeight, | ||
(options && options.ellipsis) || ELLIPSIS_CHARACTER | ||
) | ||
} | ||
module.exports = lineClamp |
{ | ||
"name": "line-clamp", | ||
"version": "0.0.9", | ||
"description": "Line clamp a DOM element in vanilla JavaScript.", | ||
"version": "1.0.0", | ||
"description": "Line clamp a DOM element in vanilla JavaScript", | ||
"author": "Lim Yuan Qing", | ||
@@ -12,17 +12,33 @@ "license": "MIT", | ||
"devDependencies": { | ||
"concurrently": "^4.1.0", | ||
"ecstatic": "^3.2.0", | ||
"concurrently": "^4.1.1", | ||
"gzip-size-cli": "^3.0.0", | ||
"opn-cli": "^4.0.0", | ||
"prettier-standard": "^8.0.0", | ||
"standard": "^12.0.1", | ||
"uglify-js": "^3.3.12", | ||
"watchify": "^3.11.0" | ||
"husky": "^3.0.0", | ||
"lint-staged": "^9.2.0", | ||
"open-cli": "^5.0.0", | ||
"prettier-standard": "^9.1.1", | ||
"sirv-cli": "^0.4.4", | ||
"standard": "^13.0.2", | ||
"terser": "^4.1.2", | ||
"watchify": "^3.11.1" | ||
}, | ||
"scripts": { | ||
"start": "concurrently \"watchify index.js --standalone lineClamp --outfile example/bundle.js\" \"ecstatic example --port 8080\" \"opn 'http://0.0.0.0:8080/'\"", | ||
"start": "concurrently --raw \"watchify index.js --standalone lineClamp --outfile example/bundle.js\" \"sirv start example --port 8080 --quiet\" \"open 'http://0.0.0.0:8080/'\"", | ||
"fix": "prettier-standard index.js", | ||
"lint": "standard index.js", | ||
"weight": "uglifyjs index.js --compress --mangle --toplevel | gzip-size" | ||
"weight": "terser index.js --compress --mangle --toplevel | gzip-size" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.js": [ | ||
"standard", | ||
"git add" | ||
] | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
@@ -29,0 +45,0 @@ "clamp", |
@@ -1,4 +0,4 @@ | ||
# line-clamp [![npm Version](http://img.shields.io/npm/v/line-clamp.svg?style=flat)](https://www.npmjs.com/package/line-clamp) [![Build Status](https://img.shields.io/travis/yuanqing/line-clamp.svg?branch=master&style=flat)](https://travis-ci.org/yuanqing/line-clamp) | ||
# line-clamp [![npm Version](https://badgen.net/npm/v/line-clamp)](https://www.npmjs.org/package/line-clamp) [![Build Status](https://badgen.net/travis/yuanqing/line-clamp?label=build)](https://travis-ci.org/yuanqing/line-clamp) [![Bundle Size](https://badgen.net/bundlephobia/minzip/line-clamp)](https://bundlephobia.com/result?p=line-clamp) | ||
> Line clamp a DOM element in vanilla JavaScript. | ||
> Line clamp a DOM element in vanilla JavaScript | ||
@@ -8,3 +8,2 @@ - Truncates in pure JavaScript; does *not* rely on [`-webkit-line-clamp`](https://css-tricks.com/line-clampin/) | ||
- Supports appending a custom string instead of an ellipsis (`…`) | ||
- 477 bytes gzipped | ||
@@ -61,3 +60,3 @@ ## Usage | ||
`options` is an optional object literal. | ||
`options` is an optional configuration object. | ||
@@ -70,4 +69,2 @@ - Set `options.ellipsis` to change the string to be appended to the truncated text. Defaults to `…`. | ||
Install via [yarn](https://yarnpkg.com): | ||
```sh | ||
@@ -77,8 +74,2 @@ $ yarn add line-clamp | ||
Or [npm](https://npmjs.com): | ||
```sh | ||
$ npm install --save line-clamp | ||
``` | ||
## Prior art | ||
@@ -85,0 +76,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
6951
10
4
80