module namespace drunk . Filter { param input param . . arggs export interface IFilter { ( ... args ) : any ; } export interface IFilterDef { name : string ; param ? : Parser . IGetter ; } param value param filterDefs param viewModel param . . args export function pipeFor ( value , filterDefs , filterMap , isInterpolate , ... args ) { if ( ! filterDefs ) { return isInterpolate ? getInterpolateValue ( value ) : value ; } if ( isInterpolate ) { value = value . map ( ( item , i ) => { if ( ! filterDefs [ i ] ) { return item ; } return Filter . pipeFor ( item , filterDefs [ i ] , filterMap , false , ... args ) ; } ) ; return getInterpolateValue ( value ) ; } let name ; let param ; let method ; let viewModel = args [ 0 ] ; args = args . slice ( 1 ) ; filterDefs . forEach ( def => { name = def . name ; method = filterMap [ name ] ; if ( typeof method !== 'function' ) { throw new Error ( `未找到filter的定义: ${ name }` ) ; } param = def . param ? def . param . apply ( viewModel , args ) : [ ] ; value = method ( ... [ value ] . concat ( param ) ) ; } ) ; return value ; } /**
     * 判断插值表达式的值个数,如果只有一个,则返回该值,如果有多个,则返回所有值的字符串相加
     */ function getInterpolateValue ( values ) { if ( values . length === 1 ) { return values [ 0 ] ; } return values . map ( item => { if ( item == null ) { return '' ; } return typeof item === 'object' ? JSON . stringify ( item ) : item ; } ) . join ( '' ) ; } let reg = { escape : /[<>& "']/gm , unescape : /&.+?;/g , striptags : /(<([^>]+)>)/ig , format : /(yy|M|d|h|m|s)(\1)*/g } ; let escapeChars = { '&' : "&amp;" , ' ' : "&nbsp;" , '"' : "&quot;" , "'" : "&#x27;" , "<" : "&lt;" , ">" : "&gt;" } ; let unescapeChars = { '&amp;' : "&" , "&nbsp;" : " " , "&quot;" : '"' , "&#x27;" : "'" , "&lt;" : "<" , "&gt;" : ">" } ; /**
     * filter方法表
     */ export var filters = { param input escape ( input ) { return input . replace ( reg . escape , function ( x ) { return escapeChars [ x ] ; } ) ; } , param input unescape ( input ) { return input . replace ( reg . unescape , function ( a ) { return unescapeChars [ a ] ; } ) ; } , param input param length param tail truncate ( input , length , tail ? ) { if ( input . length <= length ) { return input ; } return input . slice ( 0 , length ) + ( tail != null ? tail : "..." ) ; } , param input addslashes ( input ) { return input . replace ( /[\\"']/g , '\\$&' ) . replace ( /\u0000/g , '\\0' ) ; } , param input stripslashes ( input ) { return input . replace ( /\\'/g , "'" ) . replace ( /\\"/g , '"' ) . replace ( /\\0/g , '\0' ) . replace ( /\\\\/g , '\\' ) ; } , param input size ( input ) { if ( input == null ) { return 0 ; } if ( input . length != null ) { return input . length ; } if ( typeof input === 'object' ) { let length = 0 ; for ( let k in input ) { if ( input . hasOwnProperty ( k ) ) { length += 1 ; } } return length ; } } , param input param ident json ( input , ident ? ) { return JSON . stringify ( input , null , ident || 4 ) ; } , param input striptags ( input ) { return input . replace ( reg . striptags , "" ) ; } , param input param defaultValue default ( input , defaltValue ) { return input == null ? defaltValue : input ; } , param input param format date ( input , format ) { return formatDate ( input , format ) ; } , param input log < T > ( input ) { console . log ( "[Filter.log]: " , input ) ; return input ; } } ; function formatDate ( time , format ) { if ( ! time ) { return '' ; } if ( typeof time === 'string' ) { time = time . replace ( /-/g , "/" ) ; } let t = new Date ( time ) ; let y = String ( t . getFullYear ( ) ) ; let M = t . getMonth ( ) + 1 ; let d = t . getDate ( ) ; let H = t . getHours ( ) ; let m = t . getMinutes ( ) ; let s = t . getSeconds ( ) ; return format . replace ( reg . format , function ( x ) { switch ( x ) { case "yyyy" : return y ; case "yy" : return y . slice ( 2 ) ; case "MM" : return padded ( M ) ; case "M" : return M ; case "dd" : return padded ( d ) ; case "d" : return d ; case "h" : return H ; case "hh" : return padded ( H ) ; case "mm" : return padded ( m ) ; case "m" : return m ; case "s" : return s ; case "ss" : return padded ( s ) ; } } ) ; } function padded ( n ) { return n < 10 ? '0' + n : n ; } }