Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/hooks
- Loading branch information
Showing
14 changed files
with
381 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { stringifyJson, parseJson } = require('./json.js'); | ||
const { stringifyMarkdown, parseMarkdown } = require('./markdown.js'); | ||
const { stringifyToml, parseToml } = require('./toml.js'); | ||
const { stringifyYaml, parseYaml } = require('./yaml.js'); | ||
|
||
const JSON = 'json'; | ||
const YAML = 'yaml'; | ||
const MARKDOWN = 'markdown'; | ||
const TOML = 'toml'; | ||
|
||
/** | ||
* stringify object to destination format | ||
* @param {Object} obj Source object | ||
* @param {String} format Destination format | ||
* @returns {String} | ||
*/ | ||
const stringify = (obj, format = 'yaml') => { | ||
switch (format) { | ||
case 'yml': | ||
case YAML: | ||
return stringifyYaml(obj); | ||
case 'md': | ||
case MARKDOWN: | ||
return stringifyMarkdown(obj); | ||
case JSON: | ||
return stringifyJson(obj); | ||
case TOML: | ||
return stringifyToml(obj); | ||
default: | ||
throw new Error(`Format "${format}" is not supported`); | ||
} | ||
}; | ||
|
||
/** | ||
* parse string to object | ||
* @param {String} string Source object | ||
* @param {String} format Destination format | ||
* @returns {Object} | ||
*/ | ||
const parse = (str, format = 'yaml') => { | ||
switch (format) { | ||
case 'yml': | ||
case YAML: | ||
return parseYaml(str); | ||
case 'md': | ||
case MARKDOWN: | ||
const { data } = parseMarkdown(str); | ||
return data; | ||
case JSON: | ||
return parseJson(str); | ||
case TOML: | ||
return parseToml(str); | ||
default: | ||
throw new Error(`Format "${format}" is not supported`); | ||
} | ||
}; | ||
|
||
module.exports.parse = parse; | ||
module.exports.stringify = stringify; | ||
module.exports.JSON = JSON; | ||
module.exports.YAML = YAML; | ||
module.exports.MARKDOWN = MARKDOWN; | ||
module.exports.TOML = TOML; |
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,20 @@ | ||
/** | ||
* Convert object to json | ||
* @param {Object} obj Source object | ||
* @returns {String} JSON representation of source object | ||
*/ | ||
const stringifyJson = (obj) => { | ||
return JSON.stringify(obj, null, ' '); | ||
}; | ||
|
||
/** | ||
* parse json to object | ||
* @param {String} string JSON string | ||
* @returns {Object} parsed object | ||
*/ | ||
const parseJson = (string) => { | ||
return JSON.parse(string); | ||
}; | ||
|
||
module.exports.stringifyJson = stringifyJson; | ||
module.exports.parseJson = parseJson; |
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 |
---|---|---|
@@ -1,16 +1,32 @@ | ||
const yaml = require('./yaml'); | ||
const { stringifyYaml, parseYaml } = require('./yaml'); | ||
const matter = require('gray-matter'); | ||
|
||
/** | ||
* Convert object to markdown | ||
* @param {Object} obj Source object | ||
* @returns {String} Markdown representation of source object | ||
*/ | ||
const convert = (obj) => { | ||
const stringifyMarkdown = (obj, content = '') => { | ||
let frontMatter = ''; | ||
frontMatter += `---\n`; | ||
frontMatter += yaml.convert(obj); | ||
frontMatter += `---\n`; | ||
frontMatter += stringifyYaml(obj); | ||
frontMatter += `---\n${content}`; | ||
return frontMatter; | ||
}; | ||
|
||
module.exports.convert = convert; | ||
/** | ||
* parse json to object | ||
* @param {String} string JSON string | ||
* @returns {Object} parsed object | ||
*/ | ||
const parseMarkdown = (string) => { | ||
const data = matter(string, { | ||
engines: { | ||
yaml: (string) => parseYaml(string), | ||
}, | ||
}); | ||
return data; | ||
}; | ||
|
||
module.exports.stringifyMarkdown = stringifyMarkdown; | ||
module.exports.parseMarkdown = parseMarkdown; |
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,22 @@ | ||
const TOML = require('@iarna/toml'); | ||
|
||
/** | ||
* Convert object to toml | ||
* @param {Object} obj Source object | ||
* @returns {String} toml representation of source object | ||
*/ | ||
const stringifyToml = (obj) => { | ||
return TOML.stringify(obj); | ||
}; | ||
|
||
/** | ||
* parse toml to object | ||
* @param {String} string toml string | ||
* @returns {Object} parsed object | ||
*/ | ||
const parseToml = (string) => { | ||
return TOML.parse(string); | ||
}; | ||
|
||
module.exports.stringifyToml = stringifyToml; | ||
module.exports.parseToml = parseToml; |
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
Oops, something went wrong.