Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
expressionEngine = new expressions.ExpressionParser((func: string): any => {
switch (func) {
case 'phrase': return ph.PhraseEvaluator
case 'phrases': return ph.PhrasesEvaluator
case 'substitutions': return SubstitutionsEvaluator
default:
return expressions.ExpressionFunctions.standardFunctions.get(func)
}
})
}
export function* generatePhrases(name?: string, locale?: string, minLen?: number, maxLen?: number): IterableIterator {
if (name) {
let words = generateWords(name, locale)
minLen = minLen || 1
maxLen = maxLen || words.length
for (let len = minLen; len <= maxLen; ++len) {
for (let start = 0; start <= words.length - len; ++start) {
yield words.slice(start, start + len).join(' ')
}
}
}
}
export let PhraseEvaluator = new expr.ExpressionEvaluator('phrase',
expr.ExpressionFunctions.apply(
args => {
let name = args[0]
let locale = args.length > 1 ? args[1] : 'en-us'
return generateWords(name, locale).join(' ')
},
(val, expr, pos) => {
let error
switch (pos) {
case 0:
if (typeof val !== 'string') error = `${expr} does not have a name string.`
break
case 1:
if (typeof val !== 'string') error = `${expr} does not have a locale string.`
}
return error
}),
(val, expr, pos) => {
let error
switch (pos) {
case 0:
if (typeof val !== 'string') error = `${expr} does not have a name string.`
break
case 1:
if (typeof val !== 'string') error = `${expr} does not have a locale string.`
}
return error
}),
expr.ReturnType.String,
e => expr.ExpressionFunctions.validateOrder(e, [expr.ReturnType.String], expr.ReturnType.String))
export let PhrasesEvaluator = new expr.ExpressionEvaluator('phrases',
expr.ExpressionFunctions.apply(
args => {
let name = args[0]
let locale = args.length > 1 ? args[1] : 'en-us'
let min = args.length > 2 ? args[2] : undefined
let max = args.length > 3 ? args[3] : undefined
return Array.from(generatePhrases(name, locale, min, max))
},
(val, expr, pos) => {
let error
switch (pos) {
case 0:
if (typeof val !== 'string') error = `${expr} does not have a name string.`
break
case 1:
if (typeof val !== 'string') error = `${expr} does not have a locale string.`
break