Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from regulaforensics/develop
Browse files Browse the repository at this point in the history
New recipes, fixes
  • Loading branch information
aliaksandr-drozd authored Jan 26, 2024
2 parents 6b17f36 + cb6204f commit ea754dd
Show file tree
Hide file tree
Showing 46 changed files with 864 additions and 111 deletions.
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.9",
"version": "0.0.11",
"author": "Regula Forensics, Inc.",
"name": "@regulaforensics/document-reader-recipes",
"description": "Document Reader Recipes",
Expand All @@ -13,11 +13,11 @@
"class-validator": "^0.14.1",
"reflect-metadata": "^0.2.1",
"thenby": "^1.3.4",
"@regulaforensics/document-reader-typings": "^0.0.5"
"@regulaforensics/document-reader-typings": "^0.0.7"
},
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/node": "^20.11.6",
"@types/node": "^20.11.7",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3",
Expand Down
24 changes: 24 additions & 0 deletions src/decorators/default.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Transform, TransformFnParams } from 'class-transformer'


export function Default(defaultValue: any) {
return Transform(({ value }: TransformFnParams) => {
if (value !== null && value !== undefined) {
return value
}

if (typeof defaultValue === 'function') {
return defaultValue()
}

if (Array.isArray(defaultValue)) {
return [...defaultValue]
}

if (typeof defaultValue === 'object') {
return (defaultValue === null) ? null : { ...defaultValue }
}

return defaultValue
})
}
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './default.decorator'
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { ProcessResponse } from '@regulaforensics/document-reader-typings'

import rawDocReaderResponse from '@/test-data/7.json'
import { RDocumentBarcode } from './models'
import { getDocumentBarcodes } from './get-document-barcodes.recipe'


describe('getDocumentBarcodes', () => {
const docReaderResponse = ProcessResponse.fromPlain(rawDocReaderResponse)
const result = getDocumentBarcodes(docReaderResponse)
const isValid = RDocumentBarcode.isAllValid(result)

test('should return non-empty array of images', () => {
const result = getDocumentBarcodes(docReaderResponse)

expect(result.length).toBeGreaterThan(0)
})

test('should return valid array of images', () => {
expect(isValid).toBe(true)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
import { RDocumentBarcode, RDocumentBarcodeField, RDocumentBarcodeModuleData } from './models'


/**
* Get information about barcodes from the document
* @param {ProcessResponse} input - ProcessResponse from DocReader
* @returns {RDocumentBarcode[]} - array of RDocumentBarcode
*/
export const getDocumentBarcodes = (input: ProcessResponse): RDocumentBarcode[] => {
const result: RDocumentBarcode[] = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,67 @@ import { eBarCodeType, eBarCodeResultCodes } from '@regulaforensics/document-rea
import { iRDocumentBarcodeModuleData, RDocumentBarcodeModuleData } from './document-barcode-module-data.model'


/**
* Used for storing document barcode field.
*/
export interface iRDocumentBarcodeField {
/**
* Field index
* @type {number}
*/
fieldIndex: number

/**
* Barcode type
* @type {eBarCodeType}
*/
type: eBarCodeType

/**
* Barcode reading result code
* @type {eBarCodeResultCodes}
*/
resultCode: eBarCodeResultCodes

/**
* Array of barcode modules data
* @type {iRDocumentBarcodeModuleData[]}
*/
modulesData: iRDocumentBarcodeModuleData[]
}

/**
* Used for storing document barcode field.
*/
export class RDocumentBarcodeField implements iRDocumentBarcodeField {
/**
* Field index
* @type {number}
*/
@IsDefined()
@IsNumber()
fieldIndex: number

/**
* Barcode type
* @type {eBarCodeType}
*/
@IsDefined()
@IsEnum(eBarCodeType)
type: eBarCodeType

/**
* Barcode reading result code
* @type {eBarCodeResultCodes}
*/
@IsDefined()
@IsEnum(eBarCodeResultCodes)
resultCode: eBarCodeResultCodes

/**
* Array of barcode modules data
* @type {RDocumentBarcodeModuleData[]}
*/
@IsDefined()
@Type(() => RDocumentBarcodeModuleData)
@ValidateNested({ each: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,53 @@ import { IsDefined, IsEnum, IsNumber, IsString } from 'class-validator'
import { eBarCodeModuleType } from '@regulaforensics/document-reader-typings'


/**
* Used for storing document barcode module data.
*/
export interface iRDocumentBarcodeModuleData {
/**
* Number of significant elements of data
* @type {number}
*/
length: number

/**
* Module type
* @type {eBarCodeModuleType}
*/
type: eBarCodeModuleType

/**
* Read module data
* @type {string}
*/
data: string
}

/**
* Used for storing document barcode module data.
*/
export class RDocumentBarcodeModuleData implements iRDocumentBarcodeModuleData {
/**
* Number of significant elements of data
* @type {number}
*/
@IsDefined()
@IsNumber()
length: number

/**
* Module type
* @type {eBarCodeModuleType}
*/
@IsDefined()
@IsEnum(eBarCodeModuleType)
type: eBarCodeModuleType

/**
* Read module data
* @type {string}
*/
@IsDefined()
@IsString()
data: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,75 @@
import { IsDefined, IsNumber, ValidateNested } from 'class-validator'
import { IsDefined, IsNumber, ValidateNested, validateSync } from 'class-validator'
import { plainToClass, Type } from 'class-transformer'

import { AllowPrimitives } from '@/types'
import { iRDocumentBarcodeField, RDocumentBarcodeField } from './document-barcode-field.model'


/**
* Used for storing document barcode.
*/
export interface iRDocumentBarcode {
/**
* Page index of the document where the barcode was found
* @type {number}
*/
pageIndex: number

/**
* Array of barcode fields
* @type {iRDocumentBarcodeField[]}
*/
fields: iRDocumentBarcodeField[]
}

/**
* Used for storing document barcode.
*/
export class RDocumentBarcode implements iRDocumentBarcode {
/**
* Page index of the document where the barcode was found
* @type {number}
*/
@IsDefined()
@IsNumber()
pageIndex: number

/**
* Array of barcode fields
* @type {RDocumentBarcodeField[]}
*/
@IsDefined()
@Type(() => RDocumentBarcodeField)
@ValidateNested({ each: true })
fields: RDocumentBarcodeField[]

/**
* Creates an instance of RDocumentBarcode.
* @param {AllowPrimitives<iRDocumentBarcode>} input - plain object
* @returns {RDocumentBarcode}
*/
static fromPlain = (input: AllowPrimitives<iRDocumentBarcode>): RDocumentBarcode => plainToClass(RDocumentBarcode, input)

/**
* Validates input data
* @param {RDocumentBarcode} input - input data
* @returns {boolean} - true if input data is valid
*/
static isValid = (input: RDocumentBarcode): boolean => {
const errors = validateSync(input)

if (errors.length) {
console.error('RDocumentBarcode validation errors:', errors)
return false
}

return true
}

/**
* Validates array of RDocumentBarcode
* @param {RDocumentBarcode[]} input - input data
* @returns {boolean} - true if input data is valid
*/
static isAllValid = (input: RDocumentBarcode[]): boolean => input.every(RDocumentBarcode.isValid)
}
2 changes: 0 additions & 2 deletions src/recipes/image/document-image/index.ts

This file was deleted.

This file was deleted.

Loading

0 comments on commit ea754dd

Please sign in to comment.