Skip to content

Instantly share code, notes, and snippets.

@b4dnewz
Created July 6, 2017 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b4dnewz/86c3c0da54c4d9a6df0704dda5ca2a9c to your computer and use it in GitHub Desktop.
Save b4dnewz/86c3c0da54c4d9a6df0704dda5ca2a9c to your computer and use it in GitHub Desktop.
Angular filter to limit string based on words count.
'use strict'
###*
# @ngdoc filter
# @name app.filter:limitWords
# @description Angular filter to limit string based on words count.
# # limitWords
###
angular.module 'app'
.filter 'limitWords', ->
(input, words, end) ->
# exit if words limit is not number or input is not string
if isNaN(words) || angular.isUndefined(input)
return input
# get words from string
wordsArray = input.match(/\S+/g)
wordsCount = wordsArray.length
# if more than limit
if wordsCount > words
input = wordsArray.slice(0, words)
.join(' ') + (end || '...')
# return filtered input
input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment