-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mespapiers): Add a function to get the default selected version
of SelectPaperVersion component state
- Loading branch information
Showing
3 changed files
with
61 additions
and
9 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
16 changes: 16 additions & 0 deletions
16
packages/cozy-mespapiers-lib/src/helpers/makeReferenceRulesByOcrAttributes.js
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Returns reference rules from ocrAttributes with their side | ||
* @param {object} ocrAttributes | ||
* @returns {object[]} reference rules | ||
*/ | ||
export const makeReferenceRulesByOcrAttributes = ocrAttributes => { | ||
return Object.keys(ocrAttributes) | ||
.filter(attr => /(front|back)/.test(attr)) | ||
.flatMap( | ||
side => | ||
ocrAttributes[side].referenceRules?.map(ref => ({ | ||
...ref, | ||
side | ||
})) || [] | ||
) | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/cozy-mespapiers-lib/src/helpers/makeReferenceRulesByOcrAttributes.spec.js
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { makeReferenceRulesByOcrAttributes } from './makeReferenceRulesByOcrAttributes' | ||
|
||
describe('getReferenceRulesByOcrAttributes', () => { | ||
it('should return reference rules with side attribute', () => { | ||
const ocrAttributes = { | ||
front: { | ||
referenceRules: [{ regex: 'rule1' }, { regex: 'rule2' }] | ||
}, | ||
back: { | ||
referenceRules: [{ regex: 'rule3' }, { regex: 'rule4' }] | ||
} | ||
} | ||
|
||
const expected = [ | ||
{ regex: 'rule1', side: 'front' }, | ||
{ regex: 'rule2', side: 'front' }, | ||
{ regex: 'rule3', side: 'back' }, | ||
{ regex: 'rule4', side: 'back' } | ||
] | ||
|
||
expect(makeReferenceRulesByOcrAttributes(ocrAttributes)).toEqual(expected) | ||
}) | ||
|
||
it('should return an empty array if no front or back attributes', () => { | ||
const ocrAttributes = { | ||
other: { | ||
referenceRules: [{ regex: 'rule1' }, { regex: 'rule2' }] | ||
} | ||
} | ||
|
||
expect(makeReferenceRulesByOcrAttributes(ocrAttributes)).toEqual([]) | ||
}) | ||
|
||
it('should return an empty array if referenceRules is not found', () => { | ||
const ocrAttributes = { | ||
front: { | ||
otherAttribute: 'other value' | ||
} | ||
} | ||
|
||
expect(makeReferenceRulesByOcrAttributes(ocrAttributes)).toEqual([]) | ||
}) | ||
}) |