@agencebio/rosetta-cultures
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -19,8 +19,24 @@ /** | ||
/** | ||
* @deprecated since version 1.4.0 | ||
* @param {String} code | ||
* @returns {UnifiedCulture} | ||
* @returns {?UnifiedCulture} | ||
*/ | ||
export function fromCodePac(code: string): UnifiedCulture; | ||
export function fromCodePac(code: string): UnifiedCulture | null; | ||
/** | ||
* @param {String} code | ||
* @returns {?UnifiedCulture} | ||
*/ | ||
export function fromCodePacStrict(code: string): UnifiedCulture | null; | ||
/** | ||
* @param {String} code | ||
* @returns {?UnifiedCulture} | ||
*/ | ||
export function fromCodePacFirst(code: string): UnifiedCulture | null; | ||
/** | ||
* @param {String} code | ||
* @returns {UnifiedCulture[]} | ||
*/ | ||
export function fromCodePacAll(code: string): UnifiedCulture[]; | ||
/** | ||
* @param {String} code | ||
* @returns {UnifiedCulture} | ||
@@ -44,6 +60,6 @@ */ | ||
* | ||
* @param {UnifiedCulture[]} codes | ||
* @returns {function<String>:UnifiedCulture[]} | ||
* @param {UnifiedCulture[]} cultures | ||
* @returns {function(String):UnifiedCulture[]} | ||
*/ | ||
export function createCpfResolver(cultures: any): Function; | ||
export function createCpfResolver(cultures: UnifiedCulture[]): (arg0: string) => UnifiedCulture[]; | ||
export type UnifiedCulture = { | ||
@@ -50,0 +66,0 @@ code_cpf: string; |
54
index.js
@@ -28,7 +28,10 @@ /** | ||
/** | ||
* @deprecated since version 1.4.0 | ||
* @param {String} code | ||
* @returns {UnifiedCulture} | ||
* @returns {?UnifiedCulture} | ||
*/ | ||
export function fromCodePac (code) { | ||
return cpf.find(({ cultures_pac }) => cultures_pac.some(culture => culture.code === code)) | ||
console.warn("fromCodePac is deprecated, use fromCodePacFirst instead") | ||
return fromCodePacFirst(code) | ||
} | ||
@@ -38,2 +41,45 @@ | ||
* @param {String} code | ||
* @returns {?UnifiedCulture} | ||
*/ | ||
export function fromCodePacStrict (code) { | ||
let allMatchs = fromCodePacAll(code) | ||
let codes = allMatchs.map(({ code_cpf }) => code_cpf) | ||
let commonPrefix = codes.reduce((acc, code) => { | ||
let i = 0 | ||
while (code[i] === acc[i] && i < acc.length) { | ||
i++ | ||
} | ||
return code.slice(0, i) | ||
}, codes[0]).replace(/\.$/, "").split(".") | ||
if (commonPrefix.length < 2) { | ||
return null; | ||
} | ||
let commonPrefixString = commonPrefix.join(".") | ||
return cpf.find(({ code_cpf }) => code_cpf === commonPrefixString) | ||
} | ||
/** | ||
* @param {String} code | ||
* @returns {?UnifiedCulture} | ||
*/ | ||
export function fromCodePacFirst (code) { | ||
return fromCodePacAll(code)[0] || null | ||
} | ||
/** | ||
* @param {String} code | ||
* @returns {UnifiedCulture[]} | ||
*/ | ||
export function fromCodePacAll (code) { | ||
return cpf.filter( | ||
({ cultures_pac }) => cultures_pac.some(culture => culture.code === code) | ||
) | ||
} | ||
/** | ||
* @param {String} code | ||
* @returns {UnifiedCulture} | ||
@@ -75,4 +121,4 @@ */ | ||
* | ||
* @param {UnifiedCulture[]} codes | ||
* @returns {function<String>:UnifiedCulture[]} | ||
* @param {UnifiedCulture[]} cultures | ||
* @returns {function(String):UnifiedCulture[]} | ||
*/ | ||
@@ -79,0 +125,0 @@ export function createCpfResolver (cultures) { |
import { describe, it } from 'node:test' | ||
import { ok, deepEqual } from 'node:assert/strict' | ||
import { createCpfResolver, fromCodeCpf, fromCodePac, isOrganicProductionCode } from './index.js' | ||
import { | ||
createCpfResolver, | ||
fromCodeCpf, | ||
fromCodePacStrict, | ||
fromCodePacFirst, | ||
isOrganicProductionCode, | ||
fromCodePacAll | ||
} from './index.js' | ||
import cultures from './data/cpf.json' assert { type: 'json' }; | ||
@@ -61,21 +69,61 @@ /** | ||
describe('fromCodePac', () => { | ||
describe('fromCodePacFirstSelectable', () => { | ||
it('returns a matching code object', () => { | ||
const record = fromCodePac('MID') | ||
const record = fromCodePacFirst('MID') | ||
deepEqual(record.code_cpf, partialExpectation.code_cpf) | ||
ok(record.cultures_pac.find(({ code }) => code === 'MID')) | ||
ok(record.cultures_pac.find(({code}) => code === 'MID')) | ||
ok(fromCodePac('PTR')) | ||
ok(fromCodePac('MRS')) | ||
ok(fromCodePacFirst('PTR')) | ||
ok(fromCodePacFirst('MRS')) | ||
}) | ||
// we do not have a case anymore | ||
it.skip('returns a PAC code without CPF match', () => { | ||
deepEqual(fromCodePac('ZZZ'), { | ||
cultures_pac: [{ code: 'ZZZ', 'libelle': 'Culture inconnue', requires_precision: true }] | ||
it('returns null if code does not exists', () => { | ||
deepEqual(fromCodePacFirst('Z@Z'), null) | ||
}) | ||
it('returns the first match', () => { | ||
deepEqual(fromCodePacFirst('ZZZ').code_cpf, "01.1") // Cultures non permanentes | ||
deepEqual(fromCodePacFirst('AGR').code_cpf, "01.23.11") // Pomelos et pamplemousses | ||
}) | ||
}) | ||
describe('fromCodePacStrict', () => { | ||
it('returns returns the smallest full match', () => { | ||
deepEqual(fromCodePacStrict('AGR').code_cpf, "01.23.1") // Agrumes | ||
}) | ||
it('returns nothing for codes with no match', () => { | ||
deepEqual(fromCodePacStrict('ZZZ'), null) | ||
}) | ||
}) | ||
describe('fromCodePacAll', () => { | ||
it('returns all applicable cultures in all mode', () => { | ||
deepEqual( | ||
fromCodePacAll('AGR').map(({ code_cpf }) => code_cpf), | ||
["01.23.11", "01.23.12", "01.23.13", "01.23.14", "01.23.19"] | ||
) | ||
}) | ||
}) | ||
describe('data', () => { | ||
it('should not contain any duplicate code_cpf', () => { | ||
const codeCpfSet = new Set() | ||
cultures.forEach(({ code_cpf }) => { | ||
ok(!codeCpfSet.has(code_cpf)) | ||
codeCpfSet.add(code_cpf) | ||
}) | ||
}) | ||
it('returns nothing if not matching', () => { | ||
deepEqual(fromCodePac('Z@Z'), undefined) | ||
it('should not contain any duplicate code_pac which does not require precision', () => { | ||
const codePacSet = new Set() | ||
cultures.forEach(({ cultures_pac }) => { | ||
cultures_pac.forEach(({ code, requires_precision }) => { | ||
if (requires_precision) return | ||
ok(!codePacSet.has(code)) | ||
codePacSet.add(code) | ||
}) | ||
}) | ||
}) | ||
@@ -82,0 +130,0 @@ }) |
{ | ||
"name": "@agencebio/rosetta-cultures", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Traduction des codes cultures de la CPF vers la PAC vers des nomenclatures d'organismes de certification, et vice-versa.", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"require": "./dist/cjs/index.cjs" | ||
} | ||
}, | ||
"./data/*.json": "./data/*.json" | ||
}, | ||
@@ -22,0 +23,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1649759
51434