angular.module('EssayApp.directives').directive "countWords", ['$timeout', ($timeout) -> restrict: 'A' scope: false require: "ngModel" link: (scope, element, attrs, ngModel) -> PUNCTUATION = /[\u2000-\u2013\u2015-\u2018\u201A-\u206F\u2E00-\u2E7F\\!"#$%&\(\)*+,\.\/:;<=>?\[\]^_{|}~\uFF0C\u3002]+|$|^/g SPACES = /[\s]+/g fn = -> return 0 unless ngModel?.$modelValue?.length > 0 flatten = ngModel.$modelValue.replace(PUNCTUATION, ' ') flatten.split(SPACES).length - 2 init = -> scope.$watch fn, (words, old_cnt)-> scope.$eval("#{attrs.countWords}=#{words}") $timeout init, 10, false ]