Skip to content

Commit

Permalink
Merge pull request #2161 from Inist-CNRS/2139-le-transformer-mapping-…
Browse files Browse the repository at this point in the history
…ne-fonctionne-pas-correctement

change behaviour for MAPPING statement
  • Loading branch information
touv authored Sep 20, 2024
2 parents fad9ea3 + d8a5cd1 commit bbec1fd
Show file tree
Hide file tree
Showing 37 changed files with 68 additions and 53 deletions.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ module.exports = {
rootDir: `${__dirname}/src/common`,
testMatch: ['/**/*.spec.js'],
},
{
displayName: 'transformers',
rootDir: `${__dirname}/packages/transformers`,
testMatch: ['/**/*.spec.js'],
},
{
displayName: 'ezsLodex',
rootDir: `${__dirname}/packages/ezsLodex`,
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-array.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$ARRAY', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b[0]).toEqual('un deux');
expect(output[1].b[0]).toEqual('un trois');
expect(output[2].b[0]).toEqual('un quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-boolean.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$BOOLEAN', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].c).toEqual(true);
expect(output[1].c).toEqual(true);
expect(output[2].c).toEqual(true);
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-capitalize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$CAPITALIZE', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('Un deux');
expect(output[1].b).toEqual('Un trois');
expect(output[2].b).toEqual('Un quatre');
Expand Down
4 changes: 2 additions & 2 deletions packages/transformers/src/dollar-column.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$COLUMN', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].d).toEqual('un');
expect(output[1].d).toEqual('deux');
expect(output[2].d).toEqual('trois');
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('$COLUMN', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].d).toEqual('un');
expect(output[1].d).toEqual('deux');
expect(output[2].d).toEqual('trois');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-concat-uri.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('$CONCAT_URI', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].d).toEqual('1%un');
expect(output[1].d).toEqual('2%deux');
expect(output[2].d).toEqual('3%trois');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-concat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('$CONCAT', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].d[0]).toEqual('1');
expect(output[0].d[1]).toEqual('un');
expect(output[1].d[0]).toEqual('2');
Expand Down
4 changes: 2 additions & 2 deletions packages/transformers/src/dollar-default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('$DEFAULT', () => {
test('with valid parameter', done => {
const script = `
[$DEFAULT]
field = b
field = b
alternative = un quatre
[exchange]
Expand All @@ -30,7 +30,7 @@ describe('$DEFAULT', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('un deux');
expect(output[1].b).toEqual('un trois');
expect(output[2].b).toEqual('un quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$FORMAT', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('(un:trois)');
expect(output[1].b).toEqual('(un:quatre)');
expect(output[2].b).toEqual('(un:cinq)');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$GET', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].c).toEqual('OK');
expect(output[1].c).toEqual('OK');
expect(output[2].c).toEqual('OK');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-join.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$JOIN', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('un deux trois');
expect(output[1].b).toEqual('un deux quatre');
expect(output[2].b).toEqual('un deux cinq');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-lowercase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$LOWERCASE', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('un deux');
expect(output[1].b).toEqual('un trois');
expect(output[2].b).toEqual('un quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-mapping.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$MAPPING', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual(
'INSB - Institut des sciences biologiques',
);
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-mask.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$MASK', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('deux');
expect(output[1].b).toEqual('trois');
expect(output[2].b).toBeNull();
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-number.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$NUMBER', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].a).toEqual(1);
expect(output[1].a).toEqual(2);
expect(output[2].a).toEqual(3);
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$PARSE', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('un deux');
expect(output[1].b).toEqual('un trois');
expect(output[2].b).toEqual('un quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-precomputed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import statements from '.';

ezs.use(statements);

describe('$PRECOMPUTED', () => {
describe.skip('$PRECOMPUTED', () => {
test('with valid parameter', done => {
const script = `
[$PRECOMPUTED]
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-prefix.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$PREFIX', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('>un deux');
expect(output[1].b).toEqual('>un trois');
expect(output[2].b).toEqual('>un quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-remove.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$REMOVE', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toHaveLength(2);
expect(output[0].b[1]).toEqual('trois');
expect(output[1].b).toHaveLength(2);
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-replace-regex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$REPLACE_REGEX', () => {
res.push(chunk);
})
.on('end', () => {
expect(res).toHaveLengt(4);
expect(res).toHaveLength(4);
expect(res[0].b).toEqual('1 deux');
expect(res[1].b).toEqual('1 trois');
expect(res[2].b).toEqual('1 quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-replace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$REPLACE', () => {
res.push(chunk);
})
.on('end', () => {
expect(res).toHaveLengt(4);
expect(res).toHaveLength(4);
expect(res[0].b).toEqual('1 deux');
expect(res[1].b).toEqual('1 trois');
expect(res[2].b).toEqual('1 quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$SELECT', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].c).toEqual('OK');
expect(output[1].c).toEqual('OK');
expect(output[2].c).toEqual('OK');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-shift.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$SHIFT', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toHaveLength(1);
expect(output[0].b[0]).toEqual('trois');
expect(output[1].b).toHaveLength(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-split.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$SPLIT', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b[1]).toEqual('deux');
expect(output[1].b[1]).toEqual('trois');
expect(output[2].b[1]).toEqual('quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-string.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$STRING', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].a).toEqual('1');
expect(output[1].a).toEqual('2');
expect(output[2].a).toEqual('3');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-suffix.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$SUFFIX', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('un deux!');
expect(output[1].b).toEqual('un trois!');
expect(output[2].b).toEqual('un quatre!');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-trim.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$TRIM', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('un deux');
expect(output[1].b).toEqual('un trois');
expect(output[2].b).toEqual('un quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-truncate-words.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$TRUNCATE_WORDS', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('un deux');
expect(output[1].b).toEqual('un trois');
expect(output[2].b).toEqual('un quatre');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-truncate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('$TRUNCATE', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('un');
expect(output[1].b).toEqual('un');
expect(output[2].b).toEqual('un');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-uniq.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$UNIQ', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toHaveLength(3);
expect(output[1].b).toHaveLength(3);
expect(output[2].b).toHaveLength(3);
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-uppercase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('$UPPERCASE', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('UN DEUX');
expect(output[1].b).toEqual('UN TROIS');
expect(output[2].b).toEqual('UN QUATRE');
Expand Down
2 changes: 1 addition & 1 deletion packages/transformers/src/dollar-urlencode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('$URLENCODE', () => {
res.push(chunk);
})
.on('end', () => {
expect(res).toHaveLengt(4);
expect(res).toHaveLength(4);
expect(res[0].b).toEqual('%C3%A9%20deux');
expect(res[1].b).toEqual('%C3%A9%20trois');
expect(res[2].b).toEqual('%C3%A9%20quatre');
Expand Down
6 changes: 3 additions & 3 deletions packages/transformers/src/dollar.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import from from 'from';
import ezs from '../../core/src';
import ezs from '@ezs/core';
import statements from '.';

ezs.use(statements);
Expand All @@ -15,7 +15,7 @@ describe('chaining transformer', () => {
[$SUFFIX]
field = b
field = b
with = <
with = <
[exchange]
value = omit('$origin')
Expand All @@ -36,7 +36,7 @@ describe('chaining transformer', () => {
output.push(chunk);
})
.on('end', () => {
expect(output).toHaveLengt(4);
expect(output).toHaveLength(4);
expect(output[0].b).toEqual('>un deux<');
expect(output[1].b).toEqual('>un trois<');
expect(output[2].b).toEqual('>un quatre<');
Expand Down
21 changes: 14 additions & 7 deletions packages/transformers/src/operations/MAPPING.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ import { transformerWithArg } from './transformer';

export const mapping = (value, input) => {
const template = typeof input === 'string' ? input : input.toString();
const cleantemplate = template.trim().replace(/^[{[]+/, '').replace(/[}\]]+$/, '');
const cleantemplate = template
.trim()
.replace(/^[{[]+/, '')
.replace(/[}\]]+$/, '');
const list = JSON6.parse(`{${cleantemplate}}`);
return Object
.keys(list)
.reduce((currentValue, searchValue) => currentValue
.split(searchValue)
.join(list[searchValue]), value);
const cleanedValue = String(value).trim();
const newValue = Object.keys(list)
.map((searchValue) =>
cleanedValue === searchValue ? list[searchValue] : null,
)
.filter(Boolean)
.shift();
return newValue || value;
};

const transformation = (_, args) => (value) => (transformerWithArg(mapping, 'list', value, args));
const transformation = (_, args) => (value) =>
transformerWithArg(mapping, 'list', value, args);

transformation.getMetas = () => ({
name: 'MAPPING',
Expand Down
19 changes: 11 additions & 8 deletions packages/transformers/src/operations/MAPPING.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { mapping } from './MAPPING';

describe('MAPPING', () => {
it('should return mapped value', () => {
test('should return mapped value', () => {
expect(mapping('hello', '{hello:\'bonjour\'}')).toEqual('bonjour');
});
it('should return mapped value', () => {
test('should return mapped value', () => {
expect(mapping('hello', 'hello:\'bonjour\',hi:"salut"')).toEqual('bonjour');
});
it('should return mapped value', () => {
test('should return mapped value', () => {
expect(mapping('hello', '"hello":"bonjour","hi":"salut"')).toEqual('bonjour');
});
it('should return mapped value', () => {
test('should return mapped value', () => {
expect(mapping('hello', '[hello:"bonjour",hi:"salut"]')).toEqual('bonjour');
});
it('should return mapped value', () => {
expect(mapping(' hello ', '[hello:"bonjour",hi:"salut"]')).toEqual(' bonjour ');
test('should return mapped value', () => {
expect(mapping(' hello ', '[hello:"bonjour",hi:"salut"]')).toEqual('bonjour');
});
it('should return mapped value', () => {
expect(mapping('hello world', '[hello:"",hi:"salut",world:"les gens"]')).toEqual(' les gens');
test('should return mapped value', () => {
expect(mapping('hello world', '[hello:"",world:"salut", "hello world":"bonjour"]')).toEqual('bonjour');
});
test('should not be recurcive', () => {
expect(mapping('BD', '["BD":"BGD","BG":"BGR","GR":"GRC"]')).toEqual('BGD');
});
});
Loading

0 comments on commit bbec1fd

Please sign in to comment.