How to Truncate / Trim Text By Sentence in JavaScript (not word or character)

Many times for showing a preview of longer text, I prefer not just to show a certain amount of characters (although it looks better visually) but to break it via sentence. Below is a small JavaScript function (I use it in NodeJs a bunch) that I’ve used to do this. No, it’s not the most complicated thing ever, but I wanted to share in case it helps anyone.

String.prototype.truncateBySent = function(sentCount = 3, moreText = "") {
  //match ".","!","?" - english ending sentence punctuation
  var sentences = this.match(/[^\.!\?]+[\.!\?]+/g);
  if (sentences) {
    console.log(sentences.length);
    if (sentences.length >= sentCount && sentences.length > sentCount) {
      //has enough sentences
      return sentences.slice(0, sentCount).join(" ") + moreText;
    }
  }
  //return full text if nothing else
  return this;
};

Easy to use, just do something like this:

someText.truncateBySent(2);

Sometimes you also want to include something at the end to indicate there is more text, like a link or ellipsis. You can do this:

someText.truncateBySent(2,' <a href="#">View More</a>');

It will take a line of text like this:

Sample sentence one? Another sentence two. Another three. Is there four? What about five? And six! Finally seven.

And return this:

Sample sentence one? Another sentence two. View More

See a working sample here: https://codepen.io/chrisbitting/pen/oNvzKNz

Or grab it on GitHub: https://github.com/cbitting/jsTruncateBySentence

How to Truncate / Trim Text By Sentence in JavaScript (not word or character)

Cordova or NPM Update Errors – MODULE_NOT_FOUND / EACCES

If you’re taking a break and updating node, npm, cordova or other various packages – you may run into some permission issues causing errors like MODULE_NOT_FOUND or EACCES. Below are some errors I experienced (in OSX 10.11).

These errors were fixed pretty easily by running the below:

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This command was originally found at:
https://docs.npmjs.com/getting-started/fixing-npm-permissions

npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "update" "-g" "cordova"
npm ERR! node v5.7.0
npm ERR! npm v3.6.0
npm ERR! code MODULE_NOT_FOUND
npm ERR! Cannot find module 'github-url-from-git'

OR:

npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/.staging'

Hope this info helps out someone having the same issues.

Cordova or NPM Update Errors – MODULE_NOT_FOUND / EACCES

Upgrading / Updating your Node.js & NPM to Latest Version on Windows

If you’re running Node on Windows (who isn’t? [don’t answer that]), updating Node.js & NPM to the latest version is a little different than on Linux or OSX. In some ways, it’s easier than you might think.

To update these, the easiest way I have found is to download the latest version of Node.js (NPM is included) from the Node site here. Choose 32bit or 64bit (depending on your setup) and install.

Now if you run “node -v” or “npm -v” you’ll see you have the latest and greatest.

node-update

Upgrading / Updating your Node.js & NPM to Latest Version on Windows

Local web server for testing / development using Node.js and http-server

localhost8080If you’re developing html / javascript applications and want to test locally, many times you will go beyond what local file access (file:///C:/…) in browsers will allow (like XMLHttpRequests, json calls, cross domain access and Access-Control-Allow-Origin restrictions).

A simple solution instead of deploying your code to apache or IIS is to install a local http server. http-server for Node.js is a fast, easy install and app that will allow you to use any directory as a http://localhost.

Installing this simple http server only takes a few steps:

  1. Install node.js if you don’t already have installed (from http://nodejs.org)
  2. In a command prompt / terminal, now run:
    npm install http-server -g
    

    (this installs http-server globally so you can access from any folder or directory)

  3. Now using command prompt or terminal, browser to a folder with some html you want to serve as http. (ie: c:\someproject\).
  4. Run:
    http-server
    
  5. Open your browser and visit http://localhost:8080.

 

You can change port 8080 (the default) to anything using “-p”, so http-server -p 8088 would change your local site to http://localhost:8088

Run http-server –help to see the other options available for running.

Local web server for testing / development using Node.js and http-server