Skip to content

Commit

Permalink
Add a class to replace tags
Browse files Browse the repository at this point in the history
Intended to replace lower case tags with their equivalent camel case version. If the specified tag replacement is null, remove the tag from the list
  • Loading branch information
techxplorer committed Sep 23, 2024
1 parent 827cb53 commit 7141641
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/lib/tag-replacer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @file The defintition of the TagReplacer class.
*/

import { lstatSync, readFileSync } from "node:fs";

import { parse } from "yaml";

/**
* Replace a list of known tags with alternates.
*/
class TagReplacer {

/**
* The path to the YAML file that contains the tag mapping list.
* @type {string}
*/
yamlFilePath = null;

/**
* The tag map list.
* @type {object}
*/
tagMappings = null;


/**
* A class to automate the replacement of known tags with alternatives.
* @param {string} yamlFilePath The path to a YAML file with the mapping list.
*/
constructor( yamlFilePath ) {

let syncStatus = null;

try {
syncStatus = lstatSync( yamlFilePath );
// eslint-disable-next-line no-unused-vars
} catch ( err ) {
throw new TypeError( "YAML file not found" );

}

if ( !syncStatus.isFile() ) {
throw new TypeError( "Path must be to a file" );
}

this.yamlFilePath = yamlFilePath;

}

/**
* Load the tag mapping list from the YAML file.
*/
loadMappingList() {

if ( this.tagMappings !== null ) {
return;
}

const yamlContent = readFileSync( this.yamlFilePath, "utf8" );

this.tagMappings = parse( yamlContent );

}

/**
* Count the number of keys in the mapping list.
* @returns {number} The number of keys in the mapping list.
*/
getMappingCount() {

this.loadMappingList();

return Object.keys( this.tagMappings ).length;
}

/**
* Find matching tags and replace them with new tags from the mapping list.
* @param {Array} originalTags An array containing a list of tags.
* @returns {Array} A list of tags with matching ones replaced.
*/
replaceTags( originalTags ) {

if ( !Array.isArray( originalTags ) ) {
throw new TypeError( "originalTags parameter must be an array" );
}

this.loadMappingList();

const newTags = [];
const keys = Object.keys( this.tagMappings );

for ( const tag of originalTags ) {
if ( keys.indexOf( tag ) !== -1 ) {
if ( this.tagMappings[ tag ] !== null ) {
newTags.push(
this.tagMappings[ tag ]
);
} else {
continue;
}
} else {
newTags.push( tag );
}
}

return newTags;

}

}

export default TagReplacer;
5 changes: 5 additions & 0 deletions tests/artefacts/tag-replacer/tag-mapping.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
flindersuniversity: "FlindersUniversity"
australiannatives: "AustralianNatives"
crossstitch: "CrossStitch"
teddybear: "TeddyBear"
weather: null
134 changes: 134 additions & 0 deletions tests/lib/tag-replacer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import assert from "node:assert/strict";
import path from "node:path";
import { describe, it } from "node:test";

import TagReplacer from "../../src/lib/tag-replacer.js";

const testFailPathOne = path.resolve( "tests/artefacts/not-found" );
const testFailPathTwo = path.resolve( "tests/artefacts/" );
const testPassPath = path.resolve( "tests/artefacts/tag-replacer/tag-mapping.yml" );
const testPassMappingCount = 5;

const testTagList = [
"australiannatives",
"teddybear",
"SouthAustralia",
"weather"
];

const expectedTagList = [
"AustralianNatives",
"TeddyBear",
"SouthAustralia"
];

describe( "TagReplacer", () => {

describe( "Constructor", () => {

it( "should throw a TypeError when the YAML file cannot be found", () => {

assert.throws(
() => {
new TagReplacer(
testFailPathOne
);
},
{
name: "TypeError",
message: /not found/
}
);

} );

it( "should throw a TypeError when the path is to a directory", () => {

assert.throws(
() => {
new TagReplacer(
testFailPathTwo
);
},
{
name: "TypeError",
message: /must .* a file/
}
);
} );

it( "should not throw a TypeError when the path valid", () => {

assert.doesNotThrow(
() => {
new TagReplacer(
testPassPath
);
}
);
} );

} );

describe( "loadMappingList", () => {

it( "should successfully load the list of mappings", () => {

const tagReplacer = new TagReplacer( testPassPath );

tagReplacer.loadMappingList();

} );
} );

describe( "getMappingCount", () => {

it( "should return the expected number of mappings", () => {

const tagReplacer = new TagReplacer( testPassPath );

const mappingCount = tagReplacer.getMappingCount();

assert.equal(
mappingCount,
testPassMappingCount
);

} );

} );

describe( "replaceTags", () => {

it( "should thrown a TypeError if the parameter is not an array", () => {

const tagReplacer = new TagReplacer( testPassPath );

assert.throws(
() => {
tagReplacer.replaceTags( "fail" );
},
{
name: "TypeError",
message: /parameter must .* an array/
}
);

} );

it( "should replace the required tags", () => {

const tagReplacer = new TagReplacer( testPassPath );

const actualTagList = tagReplacer.replaceTags( testTagList );

assert.deepEqual(
actualTagList,
expectedTagList
);

} );

} );

} );

0 comments on commit 7141641

Please sign in to comment.