Skip to content

Commit

Permalink
Type of material (#65) & Dependency update (#67) (#68)
Browse files Browse the repository at this point in the history
* Bump the development-dependencies group with 9 updates (#67)
Bumps the development-dependencies group with 9 updates:
| Package | From | To |
| --- | --- | --- |
| [@babel/core](https://github.com/babel/babel/tree/HEAD/packages/babel-core) | `7.23.6` | `7.23.7` |
| [@babel/plugin-transform-runtime](https://github.com/babel/babel/tree/HEAD/packages/babel-plugin-transform-runtime) | `7.23.6` | `7.23.7` |
| [@babel/preset-env](https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env) | `7.23.6` | `7.23.8` |
| [@babel/register](https://github.com/babel/babel/tree/HEAD/packages/babel-register) | `7.22.15` | `7.23.7` |
| [@natlibfi/fixugen](https://github.com/natlibfi/fixugen-js) | `2.0.3` | `2.0.4` |
| [@natlibfi/fixura](https://github.com/natlibfi/fixura-js) | `3.0.3` | `3.0.4` |
| [chai](https://github.com/chaijs/chai) | `4.3.10` | `4.4.1` |
| [eslint](https://github.com/eslint/eslint) | `8.55.0` | `8.56.0` |
| [nodemon](https://github.com/remy/nodemon) | `3.0.2` | `3.0.3` |

* Type of material (#65)
* Support type of material checks, such as isBK(), isCF()...
* Add getTypeOfMaterial()

* Update deps

* 8.1.0-alpha.3
  • Loading branch information
ammsalme authored Jan 17, 2024
1 parent 5717517 commit 85b90d5
Show file tree
Hide file tree
Showing 37 changed files with 1,140 additions and 273 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,22 @@ record.insertFields(fields)
// Result: Record tags: [001, 001, 002, 003, 003, 004, 005, 006]
```
### Type of Material detection
Type of material (BK/Book, CF/Computer File, CR/Continuing Resource, MP/Map, MU/Music, MX/Mixed Material, VM/Visual Material) of a given record can be queried in two ways:
```js
// 1) Ask if record belong to a certain type
if (record.isBK()) {
// Do something
}
// 2) Ask for record type:
if (record.getTypeOfMaterial() === 'MU') { // NB! Failure returns false
// Do something else
}
```
## Sorting fields
```js
Expand All @@ -319,6 +335,8 @@ record
// correct place. It is there just as an example.
```
## See also
To serialize and unserialize MARC records, see [marc-record-serializers](https://github.com/natlibfi/marc-record-serializers)
Expand Down
682 changes: 418 additions & 264 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"url": "git@github.com:natlibfi/marc-record-js.git"
},
"license": "MIT",
"version": "8.0.2",
"version": "8.1.0-alpha.3",
"main": "./dist/index.js",
"engines": {
"node": ">=14"
Expand All @@ -52,18 +52,18 @@
},
"devDependencies": {
"@babel/cli": "^7.23.0",
"@babel/core": "^7.23.2",
"@babel/core": "^7.23.7",
"@babel/node": "^7.22.19",
"@babel/plugin-transform-runtime": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@babel/register": "^7.22.15",
"@babel/plugin-transform-runtime": "^7.23.7",
"@babel/preset-env": "^7.23.8",
"@babel/register": "^7.23.7",
"@natlibfi/eslint-config-melinda-backend": "^3.0.2",
"@natlibfi/fixugen": "^2.0.2",
"@natlibfi/fixura": "^3.0.2",
"@natlibfi/fixugen": "^2.0.4",
"@natlibfi/fixura": "^3.0.4",
"babel-plugin-istanbul": "^6.1.1",
"chai": "^4.3.10",
"chai": "^4.4.0",
"cross-env": "^7.0.3",
"eslint": "^8.51.0",
"eslint": "^8.56.0",
"mocha": "^10.2.0",
"nodemon": "^3.0.1",
"nyc": "^15.1.0"
Expand Down
102 changes: 102 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,108 @@ export class MarcRecord {
return this.getFields(tag, query).length > 0;
}

getTypeOfRecord() {
return this.leader[6];
}

getBibliograpicLevel() {
return this.leader[7];
}

isBK() {
return ['a', 't'].includes(this.getTypeOfRecord()) && !this._bibliographicLevelIsBis();
}

isCF() {
if (this.getTypeOfRecord() !== 'm') {
return false;
}
if (!this._bibliographicLevelIsBis()) {
return true;
}

return this._containsField006FormOfMaterialS();
}

isCR() {
return ['a', 't'].includes(this.getTypeOfRecord()) && this._bibliographicLevelIsBis();
}

isMP() {
if (!['e', 'f'].includes(this.getTypeOfRecord())) {
return false;
}
if (!this._bibliographicLevelIsBis()) {
return true;
}
return this._containsField006FormOfMaterialS();
}

isMU() {
if (!['c', 'd', 'i', 'j'].includes(this.getTypeOfRecord())) {
return false;
}
if (!this._bibliographicLevelIsBis()) {
return true;
}
return this._containsField006FormOfMaterialS();
}

isMX() {
if (this.getTypeOfRecord() !== 'p') {
return false;
}
if (!this._bibliographicLevelIsBis(this)) {
return true;
}
return this._containsField006FormOfMaterialS();
}

isVM() {
if (!['g', 'k', 'o', 'r'].includes(this.getTypeOfRecord())) {
return false;
}

if (!this._bibliographicLevelIsBis()) {
return true;
}
return this._containsField006FormOfMaterialS();
}

getTypeOfMaterial() {
if (this.isBK()) {
return 'BK';
}
if (this.isCF()) {
return 'CF';
}
if (this.isCR()) {
return 'CR';
}
if (this.isMP()) {
return 'MP';
}
if (this.isMU()) {
return 'MU';
}
if (this.isMX()) {
return 'MX';
}
if (this.isVM()) {
return 'VM';
}
return false;
}

_bibliographicLevelIsBis() {
return ['b', 'i', 's'].includes(this.getBibliograpicLevel());
}

_containsField006FormOfMaterialS() {
const fields006 = this.get('006');
return fields006.some(field => field.value[0] === 's');
}

equalsTo(record) {
return MarcRecord.isEqual(this, record);
}
Expand Down
35 changes: 35 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,41 @@ describe('index', () => {
return fields;
}

//-------------------------------------------------------------------------
if (name === 'isTypeOfMaterial') {
const {target} = args;

// console.info(`TARGET: '${target}'\n${record.toString()}`); // eslint-disable-line no-console
if (target === 'BK') { // Book
return record.isBK();
}
if (target === 'CF') { // Computer File
return record.isCF();
}
if (target === 'CR') { // Continuing Resource
return record.isCR();
}
if (target === 'MP') { // Map
return record.isMP();
}
if (target === 'MU') { // Music
return record.isMU();
}
if (target === 'MX') { // Mixed
return record.isMX();
}
if (target === 'VM') { // Visual Material
return record.isVM();
}

return false;
}

//-------------------------------------------------------------------------
if (name === 'getTypeOfMaterial') {
return record.getTypeOfMaterial();
}

//-------------------------------------------------------------------------
throw new Error(`Invalid operation: ${name}`);
}
Expand Down
17 changes: 17 additions & 0 deletions test-fixtures/index/getTypeOfMaterial/01/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"description": "identify record as VM",
"skip": false,
"input": {
"leader": "01234ckm a22005894i 4500",
"fields":
[
{"tag": "001", "value": "bar"}
]
},
"operations": [
{ "name": "getTypeOfMaterial", "args": {"self": true} }
],
"returns": "VM",
"immutable": true,
"only": false
}
18 changes: 18 additions & 0 deletions test-fixtures/index/getTypeOfMaterial/02/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"description": "fail to determine the type of material (LDR/07+006/00 combo)",
"skip": false,
"input": {
"leader": "01234cki a22005894i 4500",
"fields":
[
{"tag": "001", "value": "bar"},
{"tag": "006", "value": "a|||||||||||||||||"}
]
},
"operations": [
{ "name": "getTypeOfMaterial", "args": {"self": true }}
],
"returns": false,
"immutable": true,
"only": false
}
18 changes: 18 additions & 0 deletions test-fixtures/index/getTypeOfMaterial/03/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"description": "identify record as MU",
"skip": false,
"input": {
"leader": "01234cji a22005894i 4500",
"fields":
[
{"tag": "001", "value": "bar"},
{"tag": "006", "value": "s|||||||||||||||||"}
]
},
"operations": [
{ "name": "getTypeOfMaterial", "args": {"self": true}}
],
"returns": "MU",
"immutable": true,
"only": false
}
18 changes: 18 additions & 0 deletions test-fixtures/index/getTypeOfMaterial/04/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"description": "fail to determine the type of material (illegal LDR/06)",
"skip": false,
"input": {
"leader": "01234czb a22005894i 4500",
"fields":
[
{"tag": "001", "value": "bar"},
{"tag": "006", "value": "s|||||||||||||||||"}
]
},
"operations": [
{ "name": "getTypeOfMaterial", "args": {"self": true}}
],
"returns": false,
"immutable": true,
"only": false
}
17 changes: 17 additions & 0 deletions test-fixtures/index/isBK/01/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"description": "isBK: true",
"skip": false,
"input": {
"leader": "01234cam a22005894i 4500",
"fields":
[
{"tag": "001", "value": "bar"}
]
},
"operations": [
{ "name": "isTypeOfMaterial", "args": {"self": true, "target": "BK"}}
],
"returns": true,
"immutable": true,
"only": false
}
17 changes: 17 additions & 0 deletions test-fixtures/index/isBK/02/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"description": "isBK: false since LDR/07",
"skip": false,
"input": {
"leader": "01234cab a22005894i 4500",
"fields":
[
{"tag": "001", "value": "bar"}
]
},
"operations": [
{ "name": "isTypeOfMaterial", "args": {"self": true, "target": "BK"}}
],
"returns": false,
"immutable": true,
"only": false
}
17 changes: 17 additions & 0 deletions test-fixtures/index/isBK/03/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"description": "isBK: false since LDR/06",
"skip": false,
"input": {
"leader": "01234cmm a22005894i 4500",
"fields":
[
{"tag": "001", "value": "bar"}
]
},
"operations": [
{ "name": "isTypeOfMaterial", "args": {"self": true, "target": "BK"}}
],
"returns": false,
"immutable": true,
"only": false
}
16 changes: 16 additions & 0 deletions test-fixtures/index/isBK/04/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"description": "isBK: false since no LDR",
"skip": false,
"input": {
"fields":
[
{"tag": "001", "value": "bar"}
]
},
"operations": [
{ "name": "isTypeOfMaterial", "args": {"self": true, "target": "BK"}}
],
"returns": false,
"immutable": true,
"only": false
}
17 changes: 17 additions & 0 deletions test-fixtures/index/isBK/05/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"description": "isBK: false since too short leader",
"skip": false,
"input": {
"leader": "01234",
"fields":
[
{"tag": "001", "value": "bar"}
]
},
"operations": [
{ "name": "isTypeOfMaterial", "args": {"self": true, "target": "BK"}}
],
"returns": false,
"immutable": true,
"only": false
}
Loading

0 comments on commit 85b90d5

Please sign in to comment.