11'use strict' ;
22
3- var cheerio = require ( 'cheerio' ) , // used to convert raw html into jQuery style object to we can use css selectors
3+ var isUrl = require ( 'is-url' ) , // module use to verify the sourceUrl is an actual URL
4+ cheerio = require ( 'cheerio' ) , // used to convert raw html into jQuery style object to we can use css selectors
45 request = require ( 'request-promise' ) , // promise based request object
56 NodeCache = require ( "node-cache" ) , // auto expiring in memory caching collection
67 cache = new NodeCache ( { stdTTL : 300 } ) ; // cache source HTML for 5 minutes, after which we fetch a new version
@@ -77,7 +78,7 @@ function resolveTemplate(sourceUrl){
7778 return new Promise ( function ( resolve , reject ) {
7879
7980 // if its a url and we have the contents in cache
80- if ( sourceUrl . isUrl ( ) && cache . get ( sourceUrl ) ) {
81+ if ( isUrl ( sourceUrl ) && cache . get ( sourceUrl ) ) {
8182
8283 // get source html from cache
8384 var html = cache . get ( sourceUrl ) ;
@@ -88,7 +89,7 @@ function resolveTemplate(sourceUrl){
8889 // return source as a jquery style object
8990 resolve ( $ ) ;
9091 }
91- else if ( sourceUrl . isUrl ( ) ) {
92+ else if ( isUrl ( sourceUrl ) ) {
9293
9394 // request the source url
9495 return request ( { uri : sourceUrl } ) . then ( function ( html ) {
@@ -116,9 +117,4 @@ function resolveTemplate(sourceUrl){
116117 resolve ( sourceUrl ) ;
117118 }
118119 } ) ;
119- }
120-
121- // helper function to allow us to determine if the template is a url
122- String . prototype . isUrl = function ( ) {
123- return / ^ h t t p [ s ] ? : \/ \/ ( [ \w - ] + \. ) + [ \w - ] + ( [ \w - . / ? % & = ] * ) ? $ / i. test ( this ) ;
124- }
120+ } ;
0 commit comments