-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIQM-724 do not group together subfields during linking (#750)
* UIQM-724 do not group together subfields during linking * UIQM-724 added tests for MarcFieldContent * UIQM-724 renamed MarcFieldContent join to toContentString
- Loading branch information
1 parent
fc3424b
commit ba58778
Showing
10 changed files
with
320 additions
and
61 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
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
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,96 @@ | ||
// returns an array of subfields in the same order as in content string | ||
// '$a valueA1 $a value A2 $b valueB' -> [{ '$a': 'valueA1' }, { '$a': 'valueA2' }, { '$b': 'valueB' }] | ||
|
||
export const getContentSubfieldValueArr = (content = '') => { | ||
return content.split(/\$/) | ||
.filter(str => str.length > 0) | ||
.reduce((acc, str) => { | ||
if (!str) { | ||
return acc; | ||
} | ||
|
||
const key = `$${str[0]}`; | ||
const value = str.substring(2).trim(); | ||
|
||
return [...acc, { code: key, value }]; | ||
}, []); | ||
}; | ||
|
||
const reduceArr = (arr) => { | ||
return arr.reduce((acc, cur) => { | ||
const { code, value } = cur; | ||
const reducedValues = acc[code]; | ||
|
||
if (reducedValues) { | ||
return { | ||
...acc, | ||
[code]: [...reducedValues, value], | ||
}; | ||
} | ||
|
||
return { | ||
...acc, | ||
[code]: [value], | ||
}; | ||
}, {}); | ||
}; | ||
|
||
export class MarcFieldContent { | ||
constructor(content) { | ||
this.content = content || ''; | ||
|
||
this.subfieldsArr = getContentSubfieldValueArr(this.content); | ||
|
||
// Proxy allows to define generic property getters | ||
return new Proxy(this, this); | ||
} | ||
|
||
map(callback) { | ||
return this.subfieldsArr.map(callback); | ||
} | ||
|
||
reduce(...args) { | ||
return this.subfieldsArr.reduce(...args); | ||
} | ||
|
||
forEach(callback) { | ||
return this.subfieldsArr.forEach(callback); | ||
} | ||
|
||
toContentString() { | ||
return this.subfieldsArr.reduce((acc, cur) => { | ||
return `${acc} ${cur.code} ${cur.value}`; | ||
}, '').trim(); | ||
} | ||
|
||
append(code, value) { | ||
this.subfieldsArr.push({ code, value }); | ||
|
||
return this; // return this to be able to chain together method calls | ||
} | ||
|
||
prepend(code, value) { | ||
this.subfieldsArr.unshift({ code, value }); | ||
|
||
return this; | ||
} | ||
|
||
removeByCode(code) { | ||
this.subfieldsArr = this.subfieldsArr.filter(subfield => subfield.code !== code); | ||
|
||
return this; | ||
} | ||
|
||
findAllByCode(code) { | ||
return this.subfieldsArr.filter(subfield => subfield.code === code); | ||
} | ||
|
||
get(target, property) { | ||
// should be able to get array of subfields by calling marcFieldContent['$a'] | ||
if (property.match(/\$\w$/)) { | ||
return reduceArr(target.findAllByCode(property))[property]; | ||
} | ||
|
||
return target[property]; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/common/entities/MarcFieldContent/MarcFieldContent.test.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,95 @@ | ||
import { MarcFieldContent } from './MarcFieldContent'; | ||
|
||
describe('MarcFieldContent', () => { | ||
const content = '$a a1 $b b1 $b b2 $a a2'; | ||
|
||
let marcFieldContent = null; | ||
|
||
beforeEach(() => { | ||
marcFieldContent = new MarcFieldContent(content); | ||
}); | ||
|
||
describe('when calling `forEach`', () => { | ||
it('should call the callback with each subfield', () => { | ||
const cb = jest.fn(); | ||
|
||
marcFieldContent.forEach(cb); | ||
|
||
expect(cb).toHaveBeenCalledTimes(4); | ||
expect(cb.mock.calls[0][0]).toEqual({ code: '$a', value: 'a1' }); | ||
expect(cb.mock.calls[1][0]).toEqual({ code: '$b', value: 'b1' }); | ||
expect(cb.mock.calls[2][0]).toEqual({ code: '$b', value: 'b2' }); | ||
expect(cb.mock.calls[3][0]).toEqual({ code: '$a', value: 'a2' }); | ||
}); | ||
}); | ||
|
||
describe('when calling `map`', () => { | ||
it('should call the callback with each subfield', () => { | ||
const cb = jest.fn(); | ||
|
||
marcFieldContent.map(cb); | ||
|
||
expect(cb).toHaveBeenCalledTimes(4); | ||
expect(cb.mock.calls[0][0]).toEqual({ code: '$a', value: 'a1' }); | ||
expect(cb.mock.calls[1][0]).toEqual({ code: '$b', value: 'b1' }); | ||
expect(cb.mock.calls[2][0]).toEqual({ code: '$b', value: 'b2' }); | ||
expect(cb.mock.calls[3][0]).toEqual({ code: '$a', value: 'a2' }); | ||
}); | ||
}); | ||
|
||
describe('when calling `reduce`', () => { | ||
it('should call the callback with each subfield', () => { | ||
const cb = jest.fn().mockImplementation((acc, cur) => [...acc, cur]); | ||
|
||
marcFieldContent.reduce(cb, []); | ||
|
||
expect(cb).toHaveBeenCalledTimes(4); | ||
expect(cb.mock.calls[0][1]).toEqual({ code: '$a', value: 'a1' }); | ||
expect(cb.mock.calls[1][1]).toEqual({ code: '$b', value: 'b1' }); | ||
expect(cb.mock.calls[2][1]).toEqual({ code: '$b', value: 'b2' }); | ||
expect(cb.mock.calls[3][1]).toEqual({ code: '$a', value: 'a2' }); | ||
}); | ||
}); | ||
|
||
describe('when calling `toContentString`', () => { | ||
it('should transform subfields array back to a string', () => { | ||
expect(marcFieldContent.toContentString()).toEqual(content); | ||
}); | ||
}); | ||
|
||
describe('when calling `append`', () => { | ||
it('should add a new subfield to the end', () => { | ||
marcFieldContent.append('$a', 'a3'); | ||
|
||
expect(marcFieldContent.toContentString()).toEqual(`${content} $a a3`); | ||
}); | ||
}); | ||
|
||
describe('when calling `prepend`', () => { | ||
it('should add a new subfield to the beginning', () => { | ||
marcFieldContent.prepend('$a', 'a3'); | ||
|
||
expect(marcFieldContent.toContentString()).toEqual(`$a a3 ${content}`); | ||
}); | ||
}); | ||
|
||
describe('when calling `removeByCode`', () => { | ||
it('should remove all subfields by code', () => { | ||
marcFieldContent.removeByCode('$a'); | ||
|
||
expect(marcFieldContent.toContentString()).toEqual('$b b1 $b b2'); | ||
}); | ||
}); | ||
|
||
describe('when calling `findAllByCode`', () => { | ||
it('should return all subfields by code', () => { | ||
expect(marcFieldContent.findAllByCode('$a')).toEqual([{ code: '$a', value: 'a1' }, { code: '$a', value: 'a2' }]); | ||
}); | ||
}); | ||
|
||
describe('when using a subfield getter', () => { | ||
it('should return an array of subfields values', () => { | ||
expect(marcFieldContent.$a).toEqual(['a1', 'a2']); | ||
}); | ||
}); | ||
}); |
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 @@ | ||
export * from './MarcFieldContent'; |
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 @@ | ||
export * from './MarcFieldContent'; |
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,2 @@ | ||
export * from './constants'; | ||
export * from './entities'; |
Oops, something went wrong.