-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add YAML and TOML configuration support * fix: tests
- Loading branch information
Showing
9 changed files
with
314 additions
and
10 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,62 @@ | ||
import * as TOML from 'smol-toml'; | ||
import * as YAML from 'yaml'; | ||
|
||
/** | ||
* Configuration parser class that provides methods to parse and stringify configuration data. | ||
* | ||
* @class Parser | ||
* @param {string} format - The format of the configuration data. | ||
* @property {string} format - The format of the configuration data. | ||
* @property {Object} tokenizer - The tokenizer instance for the specified format. | ||
*/ | ||
export default class Parser { | ||
/** | ||
* Constructor for the Parser class. | ||
* Initializes the class with the provided format and sets up the tokenizer instance. | ||
* | ||
* @param {string} format - The format of the configuration data. | ||
*/ | ||
constructor(format) { | ||
this.format = format; | ||
this.tokenizer = this.getTokenizer(); | ||
} | ||
|
||
/** | ||
* Obtain the tokenizer instance for the specified format. | ||
* | ||
* @returns {{parse: Function, stringify: Function}} - The tokenizer instance for the specified format. | ||
* @throws {Error} - Unsupported format error. | ||
*/ | ||
getTokenizer() { | ||
switch (this.format) { | ||
case 'toml': | ||
return TOML; | ||
case 'yaml': | ||
return YAML; | ||
case 'json': | ||
return JSON; | ||
default: | ||
throw new Error(`Unsupported format: ${this.format}`); | ||
} | ||
} | ||
|
||
/** | ||
* Parse the provided data using the tokenizer instance. | ||
* | ||
* @param {string} data - The configuration data to be parsed. | ||
* @returns {Object} - The parsed configuration data. | ||
*/ | ||
parse(data) { | ||
return this.tokenizer.parse(data); | ||
} | ||
|
||
/** | ||
* Stringify the provided data using the tokenizer instance. | ||
* | ||
* @param {Object} data - The configuration data to be stringified. | ||
* @returns {string} - The stringified configuration data. | ||
*/ | ||
stringify(data) { | ||
return this.tokenizer.stringify(data); | ||
} | ||
} |
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,112 @@ | ||
import { expect } from 'chai'; | ||
import * as TOML from 'smol-toml'; | ||
import * as YAML from 'yaml'; | ||
|
||
import Parser from '../../src/parser/index.js'; | ||
|
||
describe('Parser Class', () => { | ||
describe('Constructor', () => { | ||
it('should initialize with format toml and set the correct tokenizer', () => { | ||
const parser = new Parser('toml'); | ||
expect(parser.format).to.equal('toml'); | ||
expect(parser.tokenizer).to.equal(TOML); | ||
}); | ||
|
||
it('should initialize with format yaml and set the correct tokenizer', () => { | ||
const parser = new Parser('yaml'); | ||
expect(parser.format).to.equal('yaml'); | ||
expect(parser.tokenizer).to.equal(YAML); | ||
}); | ||
|
||
it('should initialize with format json and set the correct tokenizer', () => { | ||
const parser = new Parser('json'); | ||
expect(parser.format).to.equal('json'); | ||
expect(parser.tokenizer).to.equal(JSON); | ||
}); | ||
|
||
it('should throw an error for unsupported format', () => { | ||
expect(() => new Parser('xml')).to.throw('Unsupported format: xml'); | ||
}); | ||
}); | ||
|
||
describe('getTokenizer method', () => { | ||
it('should return TOML tokenizer for toml format', () => { | ||
const parser = new Parser('toml'); | ||
expect(parser.getTokenizer()).to.equal(TOML); | ||
}); | ||
|
||
it('should return YAML tokenizer for yaml format', () => { | ||
const parser = new Parser('yaml'); | ||
expect(parser.getTokenizer()).to.equal(YAML); | ||
}); | ||
|
||
it('should return JSON tokenizer for json format', () => { | ||
const parser = new Parser('json'); | ||
expect(parser.getTokenizer()).to.equal(JSON); | ||
}); | ||
|
||
it('should throw an error for unsupported format', () => { | ||
expect(() => { | ||
new Parser('xml'); | ||
}).to.throw('Unsupported format: xml'); | ||
}); | ||
}); | ||
|
||
describe('parse method', () => { | ||
it('should correctly parse TOML data', () => { | ||
const parser = new Parser('toml'); | ||
const tomlData = 'key = "value"'; | ||
const expectedData = { key: 'value' }; | ||
|
||
const result = parser.parse(tomlData); | ||
expect(result).to.deep.equal(expectedData); | ||
}); | ||
|
||
it('should correctly parse YAML data', () => { | ||
const parser = new Parser('yaml'); | ||
const yamlData = 'key: value'; | ||
const expectedData = { key: 'value' }; | ||
|
||
const result = parser.parse(yamlData); | ||
expect(result).to.deep.equal(expectedData); | ||
}); | ||
|
||
it('should correctly parse JSON data', () => { | ||
const parser = new Parser('json'); | ||
const jsonData = '{"key": "value"}'; | ||
const expectedData = { key: 'value' }; | ||
|
||
const result = parser.parse(jsonData); | ||
expect(result).to.deep.equal(expectedData); | ||
}); | ||
}); | ||
|
||
describe('stringify method', () => { | ||
it('should correctly stringify TOML data', () => { | ||
const parser = new Parser('toml'); | ||
const data = { key: 'value' }; | ||
const expectedTomlData = 'key = "value"'; | ||
|
||
const result = parser.stringify(data); | ||
expect(result).to.equal(expectedTomlData); | ||
}); | ||
|
||
it('should correctly stringify YAML data', () => { | ||
const parser = new Parser('yaml'); | ||
const data = { key: 'value' }; | ||
const expectedYamlData = 'key: value\n'; | ||
|
||
const result = parser.stringify(data); | ||
expect(result).to.equal(expectedYamlData); | ||
}); | ||
|
||
it('should correctly stringify JSON data', () => { | ||
const parser = new Parser('json'); | ||
const data = { key: 'value' }; | ||
const expectedJsonData = '{"key":"value"}'; | ||
|
||
const result = parser.stringify(data); | ||
expect(result).to.equal(expectedJsonData); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.