Created
July 6, 2017 14:24
-
-
Save b4dnewz/86c3c0da54c4d9a6df0704dda5ca2a9c to your computer and use it in GitHub Desktop.
Angular filter to limit string based on words count.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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