Skip to content

Commit

Permalink
Implements YAML plugin (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
PipecraftNet authored Sep 20, 2021
1 parent 6e3bc87 commit b8db91d
Show file tree
Hide file tree
Showing 10 changed files with 3,440 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
._*
.env
*.log
8 changes: 8 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage/**
node_modules/**
dist/**
build/**
.nyc_output/**
LICENSE
*.html
*.lock
12 changes: 12 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"overrides": [
{
"files": ["**/*.js"],
"options": {
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true
}
}
]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 geneasy
Copyright (c) 2021 Pipecraft <[email protected]> (https://www.pipecraft.net)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
# geneasy-yaml

[![NPM Version][npm-version-image]][npm-url]
[![LICENSE][license-image]][license-url]
[![code style: prettier][code-style-prettier-image]][code-style-prettier-url]

A plugin that enables geneasy to use dafa files in YAML format.

## Installation

```sh
npm i -g geneasy-yaml
```

## Related

- [geneasy](https://github.com/geneasy/geneasy) - A command line tool that can easily generate HTML, Markdown documents, etc.
- [geneasy-handlebars](https://github.com/geneasy/geneasy-handlebars) - A plugin that enables geneasy to use the Handlebars templates.
- [js-yaml](https://github.com/nodeca/js-yaml) - JavaScript YAML parser and dumper. Very fast.

## License

Copyright (c) 2021 [Pipecraft][my-url]. Licensed under the [MIT license][license-url].

## >\_

[![Pipecraft](https://img.shields.io/badge/site-pipecraft-brightgreen)](https://www.pipecraft.net)
[![PZWD](https://img.shields.io/badge/site-pzwd-brightgreen)](https://pzwd.net)

[my-url]: https://www.pipecraft.net
[npm-url]: https://www.npmjs.com/package/geneasy-yaml
[license-url]: LICENSE
[code-style-prettier-url]: https://github.com/prettier/prettier
[npm-downloads-image]: https://img.shields.io/npm/dm/geneasy-yaml
[npm-version-image]: https://img.shields.io/npm/v/geneasy-yaml
[license-image]: https://img.shields.io/npm/l/geneasy-yaml
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
23 changes: 23 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import yaml from 'js-yaml';

export default function parse(data) {
if (!data) {
return {};
}

const result = yaml.load(data);
if (typeof result !== 'object') {
throw new TypeError('Invalid data format, requires YAML format.');
}

return result;
}

export function register(geneasy) {
geneasy.register({
parsingEngine: { type: 'yaml', engine: parse }
});
geneasy.register({
parsingEngine: { type: 'yml', engine: parse }
});
}
58 changes: 58 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "geneasy-yaml",
"version": "0.0.1",
"description": "A plugin that enables geneasy to use dafa files in YAML format.",
"type": "module",
"exports": "./lib/index.js",
"scripts": {
"lint": "prettier --write . && xo",
"lint:fix": "prettier --write . && xo --fix",
"test": "mocha test/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/geneasy/geneasy-handlebars.git"
},
"keywords": [
"geneasy",
"yaml",
"json"
],
"author": "Pipecraft <[email protected]> (https://www.pipecraft.net)",
"license": "MIT",
"bugs": {
"url": "https://github.com/geneasy/geneasy-yaml/issues"
},
"homepage": "https://github.com/geneasy/geneasy-yaml#readme",
"dependencies": {
"js-yaml": "^4.1.0"
},
"devDependencies": {
"chai": "^4.3.4",
"mocha": "^9.1.1",
"prettier": "^2.4.1",
"xo": "^0.44.0"
},
"files": [
"lib/",
"LICENSE",
"README.md"
],
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"xo": {
"space": 2,
"prettier": true,
"rules": {},
"overrides": [
{
"files": "test/*.js",
"envs": [
"node",
"mocha"
]
}
]
}
}
47 changes: 47 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect } from 'chai';
import parse, { register } from '../lib/index.js';

describe('geneasy-yaml', () => {
it('`parse` should to be a function', () => {
expect(parse).to.be.a('function');
});

it('should `parse` return empty object when data not provided', () => {
const result = parse();
// eslint-disable-next-line no-unused-expressions
expect(result).to.be.an('object').that.is.empty;
});

it('should `parse` return parsed object when a yaml data provided', () => {
const result = parse(`
---
YAML: YAML Ain't Markup Language™
What It Is:
YAML is a human friendly data serialization
language for all programming languages.
`);
expect(result).to.be.an('object');
expect(result).have.own.property('YAML');
expect(result).have.own.property('What It Is');
});

it('should `parse` return parsed object when a json data provided', () => {
const result = parse(`{"foo": "bar"}`);
expect(result).to.be.an('object');
expect(result).have.own.property('foo');
});

it('should `parse` throw an error when data with invalid format', () => {
try {
parse('123');
expect.fail('was not supposed to succeed');
} catch (error) {
expect(error).to.be.an('error');
}
});

it('`register` should to be a function', () => {
expect(register).to.be.a('function');
});
});
Loading

0 comments on commit b8db91d

Please sign in to comment.