-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: some filters on undefined variable throws, #140
- Loading branch information
Showing
8 changed files
with
229 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
import { last } from '../../util/underscore' | ||
import { isArray, last } from '../../util/underscore' | ||
import { isTruthy } from '../../render/syntax' | ||
|
||
export default { | ||
'join': (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg), | ||
'last': <T>(v: T[]): T => last(v), | ||
'first': <T>(v: T[]): T => v[0], | ||
'map': <T1, T2>(arr: {[key: string]: T1}[], arg: string): T1[] => arr.map(v => v[arg]), | ||
'last': (v: any) => isArray(v) ? last(v) : '', | ||
'first': (v: any) => isArray(v) ? v[0] : '', | ||
'map': map, | ||
'reverse': (v: any[]) => [...v].reverse(), | ||
'sort': <T>(v: T[], arg: (lhs: T, rhs: T) => number) => v.sort(arg), | ||
'size': (v: string | any[]) => v.length, | ||
'concat': <T1, T2>(v: T1[], arg: T2[] | T2): (T1 | T2)[] => Array.prototype.concat.call(v, arg), | ||
'slice': <T>(v: T[], begin: number, length: number = 1): T[] => { | ||
begin = begin < 0 ? v.length + begin : begin | ||
return v.slice(begin, begin + length) | ||
}, | ||
'uniq': function<T> (arr: T[]): T[] { | ||
const u = {} | ||
return (arr || []).filter(val => { | ||
if (u.hasOwnProperty(String(val))) return false | ||
u[String(val)] = true | ||
return true | ||
}) | ||
}, | ||
'where': function<T> (arr: T[], property: string, value?: any): T[] { | ||
return arr.filter(obj => value === undefined ? isTruthy(obj[property]) : obj[property] === value) | ||
} | ||
'size': (v: string | any[]) => (v && v.length) || 0, | ||
'concat': concat, | ||
'slice': slice, | ||
'uniq': uniq, | ||
'where': where | ||
} | ||
|
||
function map<T1, T2> (arr: {[key: string]: T1}[], arg: string): T1[] { | ||
return arr.map(v => v[arg]) | ||
} | ||
|
||
function concat<T1, T2> (v: T1[], arg: T2[] | T2): (T1 | T2)[] { | ||
return Array.prototype.concat.call(v, arg) | ||
} | ||
|
||
function slice<T> (v: T[], begin: number, length: number = 1): T[] { | ||
begin = begin < 0 ? v.length + begin : begin | ||
return v.slice(begin, begin + length) | ||
} | ||
|
||
function where<T> (arr: T[], property: string, value?: any): T[] { | ||
return arr.filter(obj => value === undefined ? isTruthy(obj[property]) : obj[property] === value) | ||
} | ||
|
||
function uniq<T> (arr: T[]): T[] { | ||
const u = {} | ||
return (arr || []).filter(val => { | ||
if (u.hasOwnProperty(String(val))) return false | ||
u[String(val)] = true | ||
return true | ||
}) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,51 @@ | ||
/** | ||
* String related filters | ||
* | ||
* * prefer stringify() to String() since `undefined`, `null` should eval '' | ||
*/ | ||
import { stringify } from '../../util/underscore' | ||
|
||
export default { | ||
'append': (v: string, arg: string) => v + arg, | ||
'prepend': (v: string, arg: string) => arg + v, | ||
'capitalize': (str: string) => String(str).charAt(0).toUpperCase() + str.slice(1), | ||
'lstrip': (v: string) => String(v).replace(/^\s+/, ''), | ||
'downcase': (v: string) => v.toLowerCase(), | ||
'upcase': (str: string) => String(str).toUpperCase(), | ||
'remove': (v: string, arg: string) => v.split(arg).join(''), | ||
'remove_first': (v: string, l: string) => v.replace(l, ''), | ||
'replace': (v: string, pattern: string, replacement: string) => | ||
String(v).split(pattern).join(replacement), | ||
'replace_first': (v: string, arg1: string, arg2: string) => String(v).replace(arg1, arg2), | ||
'rstrip': (str: string) => String(str).replace(/\s+$/, ''), | ||
'split': (v: string, arg: string) => String(v).split(arg), | ||
'strip': (v: string) => String(v).trim(), | ||
'strip_newlines': (v: string) => String(v).replace(/\n/g, ''), | ||
'truncate': (v: string, l: number = 50, o: string = '...') => { | ||
v = String(v) | ||
if (v.length <= l) return v | ||
return v.substr(0, l - o.length) + o | ||
}, | ||
'truncatewords': (v: string, l: number = 15, o: string = '...') => { | ||
const arr = v.split(/\s+/) | ||
let ret = arr.slice(0, l).join(' ') | ||
if (arr.length >= l) ret += o | ||
return ret | ||
} | ||
'append': (v: string, arg: string) => stringify(v) + arg, | ||
'prepend': (v: string, arg: string) => arg + stringify(v), | ||
'capitalize': capitalize, | ||
'lstrip': (v: string) => stringify(v).replace(/^\s+/, ''), | ||
'downcase': (v: string) => stringify(v).toLowerCase(), | ||
'upcase': (str: string) => stringify(str).toUpperCase(), | ||
'remove': (v: string, arg: string) => stringify(v).split(arg).join(''), | ||
'remove_first': (v: string, l: string) => stringify(v).replace(l, ''), | ||
'replace': replace, | ||
'replace_first': replaceFirst, | ||
'rstrip': (str: string) => stringify(str).replace(/\s+$/, ''), | ||
'split': (v: string, arg: string) => stringify(v).split(arg), | ||
'strip': (v: string) => stringify(v).trim(), | ||
'strip_newlines': (v: string) => stringify(v).replace(/\n/g, ''), | ||
'truncate': truncate, | ||
'truncatewords': truncateWords | ||
} | ||
|
||
function capitalize (str: string) { | ||
str = stringify(str) | ||
return str.charAt(0).toUpperCase() + str.slice(1) | ||
} | ||
|
||
function replace (v: string, pattern: string, replacement: string) { | ||
return stringify(v).split(pattern).join(replacement) | ||
} | ||
|
||
function replaceFirst (v: string, arg1: string, arg2: string) { | ||
return stringify(v).replace(arg1, arg2) | ||
} | ||
|
||
function truncate (v: string, l: number = 50, o: string = '...') { | ||
v = stringify(v) | ||
if (v.length <= l) return v | ||
return v.substr(0, l - o.length) + o | ||
} | ||
|
||
function truncateWords (v: string, l: number = 15, o: string = '...') { | ||
const arr = v.split(/\s+/) | ||
let ret = arr.slice(0, l).join(' ') | ||
if (arr.length >= l) ret += o | ||
return ret | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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