Skip to content

Commit

Permalink
Added 'functional style' for the param path fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrzej Szukiewicz committed Jan 30, 2018
1 parent c248c85 commit 1d1ee6c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import { concat, arrayOfArrays, getContextPath } from './utils'
import { validateItem } from './validation'

const defaultEnabler = [() => true],
FGP = ['FORX-GET-PATH'],
FGP_KEY = '__FORX_GET_PATH__',
// eslint-disable-next-line no-param-reassign
setFGPflag = (a) => { a[FGP_KEY] = FGP; return a },
hasFGPflag = a => a[FGP_KEY] === FGP,
getPathTo = v => setFGPflag(() => v),
normalizeParams = (params) => {
let result = params
if (isFunction(result)) return normalizeParams(result())
Expand Down Expand Up @@ -58,12 +64,14 @@ const defaultEnabler = [() => true],
return { ...rule, value, test, params, enabled }
},
queryPath = (value, context) => (p) => {
if (isFunction(p)) return p()
if (isFunction(p) && !hasFGPflag(p)) return p()
const
contextPath = getContextPath(p, context.indexes)

if (last(contextPath) === '@') {
return slice(0, contextPath.length - 1, contextPath)
} else if (hasFGPflag(p)) {
return contextPath
}
return traverse(contextPath, value)
},
Expand Down Expand Up @@ -110,7 +118,7 @@ const defaultEnabler = [() => true],
resPath =
(rule.to && getContextPath(rule.to, context.indexes)) || context.goal
if (!isEnabled(ruleParams, rule.enabled)) return []
// eslint-disable-next-line
// eslint-disable-next-line one-var
const errors = map(r => r.value, validateItem([ruleParams, rule.test]))
if (!isEmpty(errors)) res = set(resPath, errors, res)
return null
Expand All @@ -127,4 +135,4 @@ const defaultEnabler = [() => true],
}

export default runRule
export { runRule, normalizeRule, makeConfig, runRaw, run }
export { runRule, normalizeRule, makeConfig, runRaw, run, getPathTo }
22 changes: 17 additions & 5 deletions test/rule.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { map, cloneDeep } from 'lodash/fp'
import { runRule, normalizeRule, run, makeConfig } from '../src/rule'
import { runRule, normalizeRule, run, makeConfig, getPathTo } from '../src/rule'
import { config, value } from './rule/test-config'

const runNormalizedRule = (rule, val) => runRule(normalizeRule(rule), val)
Expand Down Expand Up @@ -116,18 +116,29 @@ describe('rule', () => {
const
successMap = {
'L.1.1': ['team', '0', 'address', '0'],
'L.2.1': ['team', '1', 'address', '0']
'L.2.1': ['team', '1', 'address', '0'],
'L.2.2': ['team', '1', 'address', '1']
},
rule = {
// -1
value: 'team.address.line1',
params: [
'team.{team}.address.{address}.line1',
'team.{team}.address.{address}.@'
'team.{team}.address.{address}.@',
getPathTo('team.{team}.address.{address}'),
getPathTo(['team', '{team}', 'address', '{address}']),
getPathTo('team.{team}.address'),
'team.{team}.address.@'
// pathTo('team.{team}.address.{address}')
],
test: [
[(v, p1, p2) => {
[(v, ...params) => {
const [p1, p2, p3, p4, p5, p6] = params
expect(successMap[p1]).toEqual(p2)
expect(successMap[p1]).toEqual(p3)
expect(successMap[p1]).toEqual(p4)
expect(successMap[p1].slice(0, 3)).toEqual(p5)
expect(successMap[p1].slice(0, 3)).toEqual(p6)
return true
}, 'ERR']
]
Expand All @@ -141,7 +152,8 @@ describe('rule', () => {
},
{
address: [
{ line1: 'L.2.1' }
{ line1: 'L.2.1' },
{ line1: 'L.2.2' }
]
}
]
Expand Down

0 comments on commit 1d1ee6c

Please sign in to comment.